【问题标题】:Set SendGrid category in Laravel's mail library在 Laravel 的邮件库中设置 SendGrid 类别
【发布时间】:2016-01-22 02:59:51
【问题描述】:

我正在使用 SendGrid's docs 中建议的配置通过 SendGrid 在 Laravel 中发送电子邮件。

只是提供一个例子来说明它现在的样子:

Mail::send('emails.demo', $data, function($message)
{
    $message->to('jane@example.com', 'Jane Doe')->subject('This is a demo!');
});

电子邮件本身工作正常,但我想添加一个 SendGrid 类别。我在过去的非 Laravel 项目中使用 this repo 中的 addCategory() 方法完成了这一点。

我的问题:有没有一种简单的方法可以仅使用 Laravel 邮件库来添加 SendGrid 类别,还是仅使用 SendGrid PHP 库更有意义?

【问题讨论】:

    标签: php email laravel laravel-5 sendgrid


    【解决方案1】:

    根据你的MAIL_MAILER(或 Laravel 中的MAIL_DRIVER

    MAIL_DRIVER=smtp:

        public function build()
        {
            return $this->view('mail')
                ->from('')
                ->subject('')
                ->withSwiftMessage(function($message) {
                    $message->getHeaders()->addTextHeader(
                        'X-SMTPAPI', json_encode(['category' => 'testing category'])
                    );
                });
        }
    

    阅读有关smtp 驱动程序的更多信息:https://docs.sendgrid.com/for-developers/sending-email/laravel#adding-a-category-or-custom-field

    MAIL_DRIVER=sendgrid:

       use Sichikawa\LaravelSendgridDriver\SendGrid;
    
        public function build()
        {
            return $this->view('mail')
                ->from('')
                ->subject('')
                ->sendgrid(['category' => 'testing category']);
        }
    

    阅读有关sendgrid 驱动程序的更多信息:https://github.com/s-ichikawa/laravel-sendgrid-driver

    【讨论】:

    【解决方案2】:

    您可以通过添加包含类别的 X-SMTPAPI 标头来做到这一点,但 Laravel 不会公开自定义标头,因此您必须直接下拉到 SwiftMailer。举个例子,看看this thread

    【讨论】:

      【解决方案3】:

      我只会使用这个库,即使它不是那么漂亮。

      【讨论】:

      • 这就是我最终的结果。意识到我向后弯腰只是为了像 3 行代码一样拯救自己。
      • 对于那些不想单独为此功能添加库的人,如果您正在使用 SMTP,请参阅此内容:stackoverflow.com/a/42780012/1849081
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 2019-10-24
      相关资源
      最近更新 更多