Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change mail configuration before sending a mail in the controller using Laravel?

Tags:

php

email

laravel

I'm using Laravel 4, I would like to change the mail configuration (like driver/host/port/...) in the controller as I would like to save profiles in databases with different mail configuration. This is the basic send mail using configuration from config/mail.php

Mail::send(
    'emails.responsable.password_lost',
    array(),
    function($message) use ($responsable){
        $message->to($responsable->email, $responsable->getName());
        $message->subject(Lang::get('email.password_lost'));
    });

I've tried to put something like but it didn't work

 $message->port('587');

Thanks for your support!

Jean

like image 895
jibe Avatar asked May 02 '14 23:05

jibe


People also ask

Can we send mail from localhost in Laravel?

Laravel provides drivers for SMTP, Mailgun, Mandrill, SparkPost, Amazon SES, PHP's mail function, and sendmail , allowing you to quickly get started sending mail through a local or cloud based service of your choice.


1 Answers

You can set/change any configuration on the fly using Config::set:

Config::set('key', 'value');

So, to set/change the port in mail.php you may try this:

Config::set('mail.port', 587); // default

Note: Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests. Read more.

Update: A hack for saving the config at runtime.

like image 97
The Alpha Avatar answered Oct 24 '22 00:10

The Alpha