dongyong8071 2019-06-17 11:22
浏览 386

Laravel多个邮件配置和多个并发用户

I am using Laravel 5.3 and I have a question regarding multiple mailers. My default mailer is set in the config. Following suggestions in other posts, I would use this script to change the default mailer when I want to send a mail from another server.

// 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);

So my question is one of clarification. Let's assume there are multiple users concurrently sending mails. If all of them are setting Mail::setSwiftMailer, this will only affect the respective instance of my application, not the whole application, right? I want to avoid that one user sends a mail from an account that is intended for another user.

UPDATE:

I would suspect that as long as I don't change the config values as below, Mail::setSwiftMailer only sets the Mailer for the current user. Is that correct?

$mail=DB::table('mail_settings')->first();
$config = array(
            'driver' => $mail->driver,
            'host' => $mail->host,
            'port' => $mail->port,
            'from' => array('address' => $mail->from_address, 'name' => $mail->from_name),
            'encryption' => $mail->encryption,
            'username' => $mail->username,
            'password' => $mail->password,
            'sendmail' => '/usr/sbin/sendmail -bs',
            'pretend' => false
        );
Config::set('mail',$config);
  • 写回答

1条回答 默认 最新

  • dongyiyu882684 2019-06-19 11:12
    关注

    The Mailer class is configured as a singleton. As far as I understood, this does not mean that the usual singleton characteristics apply http://blog.gordon-oheim.biz/2011-01-17-Why-Singletons-have-no-use-in-PHP/

    A Singleton created in one Request lives for exactly that request. A Singleton created in another Request done at the same time will still be a completely different instance. And it will occupy it’s own memory. Those instances are not linked to each other. They are completely isolated, because PHP is a Shared-Nothing architecture. You do not have one single unique instance, but many similar instances in parallel processes.

    So setting Mail::setSwiftMailer($gmail); should be fine. In any case - I found a solution that uses a new Mailer

    The link below describes the problem and shows a solution for laravel 5.8 https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel

    For Laravel 5.3, you need to create the mailer like so, because the Helper function app()->get('view') does not exist

    $mailer = new Mailer(app('view'), $swift_mailer, app('events'));

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?