首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多邮件配置

多邮件配置
EN

Stack Overflow用户
提问于 2014-10-24 11:15:14
回答 7查看 27K关注 0票数 31

我给laravel的邮件服务配置了芒钻驱动器。这里没问题!

现在,在我的应用程序中,我需要通过gmail发送一封邮件。

我做了这样的事:

代码语言:javascript
复制
// 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);

这是行不通的,拉里威尔总是使用芒钻的配置。看起来,他在脚本启动时启动了邮件服务,并忽略了您在执行过程中所做的任何事情。

在执行过程中,如何改变邮件服务的信任/行为?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-10-24 11:58:32

您可以创建一个新的Swift_Mailer实例并使用该实例:

代码语言:javascript
复制
// 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);
票数 59
EN

Stack Overflow用户

发布于 2017-09-09 23:13:00

派对有点晚了,但只是想延长已被接受的答案,并把我的2美分,以防它节省了别人的时间。在我的场景中,每个登录用户都有自己的SMTP设置,但我使用队列发送邮件,这导致设置后这些设置回到默认状态。它还制造了一些并发的电子邮件问题。简而言之,问题是

代码语言:javascript
复制
$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根本不影响它。它只是简单地选择默认设置。因此,配置更改必须发生在发送电子邮件的实际时刻,而不是在排队时。

我的解决方案是扩展邮件类,如下所示。

代码语言:javascript
复制
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,即Mail::queue(new CustomMail(Auth::user()))

虽然这可能不是理想的解决方案(例如,如果尝试发送大量电子邮件,最好只配置一次邮件,而不是每次发送的邮件),但我喜欢它的简单性,以及我们根本不需要更改全局MailConfig设置的事实,只有$mailer实例受到影响。

希望你能找到有用的东西!

票数 20
EN

Stack Overflow用户

发布于 2020-05-22 18:06:16

对于Laravel版本7.x和更高版本,您现在可以在发送电子邮件时声明要使用的邮件驱动程序。您需要在config/mail.php中正确配置所有连接和凭据。配置好之后,可以通过mailer()函数指定驱动程序的名称,如下所示:

代码语言:javascript
复制
Mail::mailer('postmark')
    ->to($request->user())
    ->send(new OrderShipped($order));

我希望它能帮到别人。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26546824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档