【问题标题】:Laravel 9 dynamic Email configurationsLaravel 9 动态电子邮件配置
【发布时间】:2022-07-26 04:44:12
【问题描述】:

我来找你有一个问题,经过数小时的谷歌搜索,我在谷歌上找不到解决方案。

我希望能够使用不同的 SMTP 电子邮件配置发送电子邮件,我可以在运行时添加或更改这些配置。我正在建立一个网站,为很多客户托管很多项目,我们需要能够代表他们发送电子邮件。我知道我可以在 .env 文件中设置不同的配置,但该解决方案还不够好,因为我想将配置保留在数据库中,以便轻松查询/更新等。

一种解决方案是使用this tutorial 中的这种方法。它使用 Swift 邮件程序创建一个返回新邮件程序对象的方法,但这似乎在 Laravel 9 中不起作用。显然 Swift 邮件程序不再维护,并由 Symfony Mailer 继承。不幸的是,我找不到以我刚才描述的方式使用新 Symfony Mailer 的方法,尽管如果我能让它工作,我当然更喜欢它。

我想知道是否可以在 Symfony Mailer 中使用相同的方法?这是我使用与教程中相同的代码时遇到的错误:

Class "Swift_SmtpTransport" not found

我将类添加到命名空间,并将语法从 new Swift_SmtpTransport 更改为 \Swift_SmtpTransport::newInstance,但这并没有解决错误。

如果有人有任何想法/建议,我将不胜感激!真没想到这么简单的事情竟然这么难。

【问题讨论】:

    标签: laravel symfony email dynamic laravel-9


    【解决方案1】:

    Laravel 9.x 正在使用symfony/mailer,这可能会导致此冲突。相反,您可以使用mailer() 选项即时更改邮件驱动程序。

    Mail::mailer('postmark')
            ->to($request->user())
            ->send(new OrderShipped($order));
    

    您可以在documentation找到详细信息。

    更新

    我检查了symfony/mailer documentation,我想这就是你想要的。您也可以使用动态 SMTP 详细信息即时构建自己的传输。

    use Symfony\Component\Mailer\Transport;
    use Symfony\Component\Mailer\Mailer;
    use Symfony\Component\Mime\Email;
    
    $transport = Transport::fromDsn('smtp://localhost');
    $mailer = new Mailer($transport);
    
    $email = (new Email())
        ->from('hello@example.com')
        ->to('you@example.com')
        //->cc('cc@example.com')
        //->bcc('bcc@example.com')
        //->replyTo('fabien@example.com')
        //->priority(Email::PRIORITY_HIGH)
        ->subject('Time for Symfony Mailer!')
        ->text('Sending emails is fun again!')
        ->html('<p>See Twig integration for better HTML integration!</p>');
    
    $mailer->send($email);
    

    【讨论】:

    • 谢谢,我确实阅读了文档,如果我理解正确,那么每次我想编辑电子邮件配置时,我都必须编辑 mail.php 文件。我希望我错了,但这个解决方案对我的用例来说不是很动态。
    • @Innit2 请检查更新的答案,也许这可以帮助您制作更动态的邮件。
    • 不,抱歉这对我没有帮助,不过还是谢谢!我确实找到了另一个解决方案,我很快就会回答我自己的问题。
    【解决方案2】:

    这对我有用...

    在您的 config/mail.php 文件中,您可以如下定义不同的帐户:

    'mailers' => [
            'smtp' => [
                'transport' => 'smtp',
                'host' => 'smtp.google',
                'port' => 465,
                'encryption' => 'ssl',
                'username' => 'youraccount@something.com',
                'password' => 'password',
                'timeout' => null,
                'local_domain' => env('MAIL_EHLO_DOMAIN'),
            ],
            'OtherMailer' => [
                'transport' => 'smtp',
                'host' => 'smtp.gmail.com',
                'port' => '465',
                'encryption' => 'ssl',
                'username' => 'otheraccount@something.com',
                'password' => 'password',
                'timeout' => null,
                'local_domain' => env('MAIL_EHLO_DOMAIN'),
            ],
    

    接下来,在您的控制器或模型中,您可以设置要使用的邮件程序。

    Mail::mailer('otherMailer')->to('person@something.com')->send(new SendLetter($sendMail));
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2022-11-12
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      相关资源
      最近更新 更多