我用laravel 5.3.
我需要发送具有不同凭据(主机,端口,用户名,密码)的邮件.
我可以发送默认的laravel配置(.env).
但我需要动态级别的实现.
我制作配置数组,
// Pre-Mail Setup Config. $store_config = [ 'list' => //SET 1 ['from_name' => 'sender1', 'from_address' => 'from_adderss1', 'return_address' => 'reply1', 'subject' => 'subject1', 'host' => 'host1', 'port' => 'post1', 'authentication' => 'auth1', 'username' => 'uname1', 'password' => 'pass1'], //SET 2 [.........], //SET 3 [.........] ];
我尝试以下发送邮件,但它不起作用.
// Inside Foreach.$transporter = \Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl') ->setUsername($config['username']) ->setPassword($config['password']);$mailer = \Swift_Mailer::newInstance($transporter);$message->from($config['from_address'], $config['from_name']);$message->to('To_Email, 'Name') ->subject('My Subject') ->setBody('My Content', 'text/html');$mailer->send($message);
我的代码出了什么问题?
可能吗?
或任何其他解决方案?
解决方法:
最后我找到了解决这个问题的方法.
实际上Laravel 5并不完全支持这种多传输器配置.
所以我使用替代包来实现它.
我的代码是,
foreach ($store_configs['list'] as $store_config) { // Create Custom Mailer Instances. $mailer = new \YOzaz\LaravelSwiftmailer\Mailer(); $transport = \Swift_SmtpTransport::newInstance( $store_config['host'], $store_config['port'], $store_config['authentication']); // Assign Dynamic Username. $transport->setUsername($store_config['username']); // Assign Dynamic Password. $transport->setPassword($store_config['password']); $smtp = new \Swift_Mailer($transport); $mailer->setSwiftMailer($smtp); $mailer->send('template', ['data'], function ($message) use ($queue) { // Default Response goes here $message->from('From Address', 'From Name'); $message->to($email, 'Name')->subject('My Subject') ->setBody('My HTML', 'text/html'); $message->getSwiftMessage(); // }); }
它适用于多个动态运输车.
谢谢大家 !
来源:https://www.icode9.com/content-1-316501.html联系客服