Laravel覆盖邮件以自动添加纯文本版本 [英] Laravel override mail to automatically add plain text version

查看:62
本文介绍了Laravel覆盖邮件以自动添加纯文本版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel应用程序中进行一些更改,以将纯文本版本自动添加到电子邮件中.

https://packagist.org/packages/html2text/html2text

我将通过运行获取文本版本

 <代码> \ Html2Text \ Html2Text :: convert($ content) 

现在,我想重写laravels Mailable.php buildView()函数来自动生成文本.我的问题是:如何正确覆盖它?我在哪里可以重新声明呢?

解决方案

邮件程序由邮件程序服务提供商定义,您可以在 config/app.php 中的提供者" ,您将在其中看到以下内容:

  \ Illuminate \ Mail \ MailServiceProvider :: class, 

因此,您所需要做的就是删除MailServiceProvider注册,并根据您的更改创建您自己的Provider并注册您的

.

确保执行 Illuminate \ Contracts \ Mail \ Mailer 合同.

但是您不需要!

Laravel随附的邮件程序已经支持发送电子邮件的HTML和普通版本.

Mailer :: send()方法的第一个参数是 @param string | array $ view ,通常在其中发送电子邮件的HTML版本的视图名称,但是您可以改为发送这样的数组...

  Mailer :: send(['html'=>'my-mails.notification-in-html','文本'=>"my-mails.notification-in-text",],$ data,$ callback); 

您甚至可以在其中定义其他文本,并删除原本不会在纯文本版本中放置的内容,或调整在纯文本上看起来不错的其他签名,并设置不同的格式.

有关更多信息,请查看 Illuminate \ Mail \ Mailer 类中的 parseView().

所以,您有2种选择:

  • 创建您自己的邮件程序并注册它,而不是默认邮件程序
  • 或仅通过一系列视图调用邮件发件人.

I'm making some changes in my Laravel app to automatically add plain text version to my e-mails.. I'm doing that by using the library

https://packagist.org/packages/html2text/html2text

I would get the text version by running

\Html2Text\Html2Text::convert($content)

Now I want to override laravels Mailable.php buildView() function to automaticaly generate the text. My question is: how to properly override it? Where can I redeclare it?

解决方案

The Mailer is defined by the Mailer Service Provider, you can see the registration of it in your config/app.php under 'providers', where you will see this:

\Illuminate\Mail\MailServiceProvider::class,

So, all you need to do, is remove the MailServiceProvider registration and create your own Provider based on that one with your changes and register yours.

Make sure you implement the Illuminate\Contracts\Mail\Mailer contract.

But you don't need to!

The mailer that comes with Laravel already support sending the HTML and Plain versions of your email.

The Mailer::send() method's first argument is @param string|array $view, where you usually send the view name of the HTML version of your email, but, you can instead send an array like this...

Mailer::send([
    'html' => 'my-mails.notification-in-html',
    'text' => 'my-mails.notification-in-text',
  ], $data, $callback);

Where you could even define a different text and remove things that you would not put in your plain text version, or adjust a different signature that looks good on plain, and also format things different.

For more information you can look at the parseView() in Illuminate\Mail\Mailer class.

So, there you have it, 2 options:

  • Create your own Mailer and register it instead of the default one
  • or just call the mailer with an array of views.

这篇关于Laravel覆盖邮件以自动添加纯文本版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆