0

Is is possible to have multiple mail drivers on laravel 5?

the reason I'm asking is because I'm building an app in Laravel 5 and I'm using mandrill. I want when users contact me via the website to use my hosting smtp, and when I send emails to my members to use mandrill. So far mandrill is being used in both ways because it is the default driver in my application.

Thank you

rafiaTech
  • 433
  • 1
  • 6
  • 15
  • Why dont you just use Mandrill for everything? Would seem to be significantly easier and lets mucking around...? – Laurence Mar 12 '15 at 10:29
  • Mandrill is not free. They only allow 12000 emails for free. That's why I only want to use Mandrill when I send emails, but when visitors send emails to me or to each other, I want to use free service. Anyway I found a solution – rafiaTech Mar 12 '15 at 15:02
  • Possible duplicate of [multiple mail configurations](https://stackoverflow.com/questions/26546824/multiple-mail-configurations) – Adam Jul 09 '19 at 17:13

3 Answers3

2

I found a solution. I built two classes called Mailer and secondaryMailer. The mailer class use the default laravel mail that I'm using (mandrill). The secondaryMailer class, uses gmail or whatever driver you want to use. This is the code for the secondaryMailer class

 use Illuminate\Support\Facades\Mail;
 use Swift_Mailer;

 class SecondaryMailer {

public function Send($view, array $data, array $params)
{
    // Backup your default mailer
    $backup = Mail::getSwiftMailer();

    // Setup your gmail mailer
    $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls');
    $transport->setUsername('Your-gmail-username');
    $transport->setPassword('Your-Password');
    // Any other mailer configuration stuff needed...
    $gmail = new Swift_Mailer($transport);

    // Set the mailer as gmail
    Mail::setSwiftMailer($gmail);

    // Send your message
    Mail::send($view, $data, function($message) use($params)
    {
        $message->from($params['email'])->to($params['toEmail'])->subject($params['subject']);
    });

    // Restore your original mailer
    Mail::setSwiftMailer($backup);

}

}

This is the post where I found the solution. Thanks to Bogdan multiple mail configurations

Community
  • 1
  • 1
rafiaTech
  • 433
  • 1
  • 6
  • 15
  • 1
    Your method is great, but doesn't work for `Mail::queue($mail)`, do you know why? – Suge Nov 14 '16 at 14:46
2

From Laravel version 7.x docs: section "Sending Mail Via A Specific Mailer", you can now state the mail driver to use while sending an email. All you need to configure all your connections & credentials properly in app/config/mail.php. Once configured, you can specify the name of the driver via mailer() function as below:

Mail::mailer('postmark')
    ->to($request->user())
    ->send(new OrderShipped($order));

I hope it helps someone.

Choxx
  • 945
  • 1
  • 24
  • 46
0

Please look at this project: https://github.com/kevinzheng/laravel-switchable-mail , just install and config it, it will automatically keep and switch different mail drivers at runtime.

Suge
  • 2,808
  • 3
  • 48
  • 79