Multiple Email Configurations

I configured the laravel email service with the mandrill driver. No problem here!

Now, at some point in my application, I need to send mail via gmail.

I did something like:

// backup current mail configs
$backup = Config::get('mail');

// rewrite mail configs to gmail stmp
$new_configs = array(
    'driver' => 'smtp',
    // ... other configs here
);
Config::set('mail', $new_configs);

// send the email
Mail::send(...

// restore configs
Config::set('mail', $backup);

This does not work, laravel always uses mandrail configurations. It looks like it initiates the mail service when the script starts and ignores everything you do at runtime.

How do you change the configuration / behavior of mail services at runtime?

+21
source share
5 answers

You can create a new instance Swift_Mailerand use it:

// Backup your default mailer
$backup = Mail::getSwiftMailer();

// Setup your gmail mailer
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_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();

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

, 2 , - . SMTP, , . . ,

$transport = Swift_SmtpTransport::newInstance($user->getMailHost(), $user->getMailPort(), $user->getMailEncryption());
$transport->setUsername($user->getMailUser());
$transport->setPassword($user->getMailPassword());
$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
//until this line all good, here is where it gets tricky

Mail::send(new CustomMailable());//this works
Mail::queue(new CustomMailable());//this DOES NOT WORK

, , Mail:: setSwiftMailer . . , .

, Mailable .

app\Mail\ConfigurableMailable.php

<?php

namespace App\Mail;

use Illuminate\Container\Container;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Mailable;
use Swift_Mailer;
use Swift_SmtpTransport;

class ConfigurableMailable extends Mailable
{
    /**
     * Override Mailable functionality to support per-user mail settings
     *
     * @param  \Illuminate\Contracts\Mail\Mailer  $mailer
     * @return void
     */
    public function send(Mailer $mailer)
    {
        $host      = $this->user->getMailHost();//new method I added on User Model
        $port      = $this->user->getMailPort();//new method I added on User Model
        $security  = $this->user->getMailEncryption();//new method I added on User Model

        $transport = Swift_SmtpTransport::newInstance( $host, $port, $security);
        $transport->setUsername($this->user->getMailUser());//new method I added on User Model
        $transport->setPassword($this->user->getMailPassword());//new method I added on User Model
        $mailer->setSwiftMailer(new Swift_Mailer($transport));

        Container::getInstance()->call([$this, 'build']);
        $mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
            $this->buildFrom($message)
                 ->buildRecipients($message)
                 ->buildSubject($message)
                 ->buildAttachments($message)
                 ->runCallbacks($message);
        });
    }
}

CustomMail, ConfigurableMailable Mailable:

class CustomMail extends ConfigurableMailable {}

, Mail::queue(new CustomMail()) . , CustomMail - i.e Mail::queue(new CustomMail(Auth::user()))

(.. , ), , Mail Config , $mailer.

, !

+13

"fly fly":

Config::set('mail.encryption','ssl');
Config::set('mail.host','smtps.example.com');
Config::set('mail.port','465');
Config::set('mail.username','youraddress@example.com');
Config::set('mail.password','password');
Config::set('mail.from',  ['address' => 'youraddress@example.com' , 'name' => 'Your Name here']);

, config/customMail.php Config:: get ('customMail')

+3

setSwiftMailer, , , from adress config/mail.php. .

multiMail, .

// / .. /config/multimail.php, ,

\MultiMail::from('office@example.com')->send(new MailableDummy()));
\MultiMail::from('contact@otherdomain.de')->send(new MailableDummy()));

\MultiMail::from('office@example.com')->queue(new MailableDummy()));
+1

Even easier to execute the following code, just before sending the email, after you rewrote the mail configuration using config:

app()->forgetInstance('swift.transport');
app()->forgetInstance('swift.mailer');
app()->forgetInstance('mailer');
0
source

Source: https://habr.com/ru/post/1664886/


All Articles