Alex McCabe February 2016

Laravel update email config on the fly

I need to be able to send email using Mailgun from one domain, switch settings, then send another from another domain. However, updating the settings before sending the second email does not work, the second email still gets sent using the first emails settings.

Initial settings are set in Config.mail and Config.services, this all works fine.

// send first email
try { 
    Mail::send(--stuff here--) 
} catch (Exception $e) { ... }

// update config using the sandbox account details
Config::set('mail.username', 'postmaster@secret.mailgun.org');
Config::set('mail.password', 'password');
Config::set('services.mailgun.domain', 'domain.mailgun.org'); 
Config::set('services.mailgun.secret', 'key-secret');

// send second email
try { 
    Mail::send(--stuff here--) 
} catch (Exception $e) { ... }
// Second email has now been sent using the first emails config settings 

If I comment out the first email send, then change the settings as above, the second email get's sent correctly from the sandbox account. If I leave the first email in, it gets sent from a domain I have on MailGun.

Does anyone have any experience with this?

Answers


Олег Ильин February 2016

    config([
        'mail.host' => 'smtp.yandex.ru',
        'mail.port' => 465,
        'mail.encryption' =>'ssl',
        'mail.username' => 'username',
        'mail.password' => 'password'
    ]);

    $app = App::getInstance();

    $app['swift.transport'] = $app->share(function ($app) {
        return new TransportManager($app);
    });

    $mailer = new \Swift_Mailer($app['swift.transport']->driver());
    Mail::setSwiftMailer($mailer);

    $msg = Mail::send('mail.view', ['key' => 'value'], function(Message $message) {
        $message
            ->to('user@mail.com', 'Name')
            ->subject('Subject');
    });

Post Status

Asked in February 2016
Viewed 3,995 times
Voted 12
Answered 1 times

Search




Leave an answer