MarcosBL

Aprendiz de todo, maestro de nada

Laravel email with multiple server configurations

Change email configuration on the fly.

use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;

// Setup a new SmtpTransport instance for Gmail
$transport = SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('you@gmail.com');
$transport->setPassword('yourpassword123');

// Assign a new SmtpTransport to SwiftMailer
$gmail = new Swift_Mailer($transport);

// Assign it to the Laravel Mailer
Mail::setSwiftMailer($gmail);

// Send your message
Mail::send('emails.folder.template', ['data' => $data], function($message) use($extradata) {
    $message->from([$extradata["MailFromEmail"] => $extradata["MailFromName"]]);
    $message->to($extradata["MailToEmail"], $extradata["MailToEmail"])->subject('Subject');
});