简体   繁体   中英

laravel send mail from multiple smtp emails

I need to send mails from multiple mails Ex. support@email.com , info@email.com

in .env file I entered my default settings

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mymail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

I use markdown email

public function build()
    {
        return $this->markdown('emails.users.resetpassword',[
            'url' => url(config('app.url').route('password.reset', $this->token, false)),
            'user' => $this->notifiable,
            ]);
    }

this works fine

I try to change sender mail on the 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']);

but it still sending from main account!!

how can I change the sender mail ? thanks in advance

You can override the config during runtime

use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;

$transport = (new SmtpTransport(config('mail.host_other'), config('mail.port_other'), config('mail.encryption_other')))->setUsername(config('mail.username_other'))->setPassword(config('mail.password_other'));

$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
Mail::send(...);

In your mail.php config file, add

<?php

return [
    ....
    ....
    'host_other' => env('HOST_OTHER'),
    'port_other' => env('PORT_OTHER'),
    'encryption_other' => env('MAIL_ENCRYPTION_OTHER'),
    'username_other' => env('MAIL_USERNAME_OTHER'),
    'password_other' => env('MAIL_PASSWORD_OTHER'),


    ....
    ....
]

Get more information here https://swiftmailer.symfony.com/docs/sending.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM