在 Laravel 4.2 中覆盖 Mail MailgunTransport 类
Posted
技术标签:
【中文标题】在 Laravel 4.2 中覆盖 Mail MailgunTransport 类【英文标题】:Override Mail MailgunTransport class in Laravel 4.2 【发布时间】:2017-02-11 16:06:51 【问题描述】:使用 mailgun 作为邮件驱动程序我面临的问题是密件抄送无法正常工作,因为它向所有收件人显示所有地址。我找到了一个可以解决问题的修复程序,但它需要在 vendor/laravel/framework/src/Illuminate/Mail/Transport/MailgunTransport.php 编辑 MailgunTransport.php 文件。我不想更改供应商文件夹中的文件,因此我正在尝试扩展 MailgunTransport 类...
我创建了一个名为 app/custom/extensions 的文件夹,其中包含两个文件 CustomMailServiceProvider...
<?php namespace custom\extensions;
use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use custom\extensions\CustomMailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;
class CustomMailServiceProvider extends \Illuminate\Mail\MailServiceProvider
...和 CustomMailgunTransport.php
<?php namespace custom\extensions;
class CustomMailgunTransport extends Illuminate\Mail\Transport\MailgunTransport
/**
* @inheritdoc
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
$client = $this->getHttpClient();
$to = $this->getTo($message);
$message->setBcc([]);
$client->post($this->url, ['auth' => ['api', $this->key],
'body' => [
'to' => $to,
'message' => new PostFile('message', (string) $message),
],
]);
CustomMailServiceProvider.php 没有重新定义任何原始方法,而是将调用从原始 MailgunTransport 更改为 custom\extensions\CustomMailgunTransport。
我已经在 composer.json 类映射中加载了新文件的 app/custom/extensions 目录...
...
"autoload":
"classmap": [
...
"app/custom/extensions"
],
,
...
我已经将原来的 'Illuminate\Mail\MailServiceProvider' 换成了 'custom\extensions\CustomMailServiceProvider'...
'providers' => array(
...
//'Illuminate\Mail\MailServiceProvider',
'custom\extensions\CustomMailServiceProvider',
...
),
但是,此时我不知道如何调用邮件功能。如果我尝试使用 Mail 门面,它会使用 MailgunTransport.php 中的原始代码
我需要创建自定义外观吗?如果是这样......我该怎么做?还是上面的代码有问题?有什么方法可以只扩展 MailgunTransport.php 而无需创建 CustomMailServiceProvider?
【问题讨论】:
【参考方案1】:我通过在 CustomMailServiceProvider 中包含 registerMailgunTransport 方法并在那里引用新的 CustomMailgunTransport 解决了这个问题。
CustomMailServiceProvider
<?php namespace custom\extensions;
use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use custom\extensions\CustomMailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;
class CustomMailServiceProvider extends \Illuminate\Mail\MailServiceProvider
/**
* Register the Mailgun Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerMailgunTransport($config)
$mailgun = $this->app['config']->get('services.mailgun', array());
$this->app->bindShared('swift.transport', function() use ($mailgun)
return new CustomMailgunTransport($mailgun['secret'], $mailgun['domain']);
);
CustomMailgunTransport
<?php namespace custom\extensions;
use Swift_Transport;
use GuzzleHttp\Client;
use Swift_Mime_Message;
use GuzzleHttp\Post\PostFile;
use Swift_Events_EventListener;
class CustomMailgunTransport extends \Illuminate\Mail\Transport\MailgunTransport
/**
* @inheritdoc
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
$client = $this->getHttpClient();
$to = $this->getTo($message);
$message->setBcc([]);
$client->post($this->url, ['auth' => ['api', $this->key],
'body' => [
'to' => $to,
'message' => new PostFile('message', (string) $message),
],
]);
【讨论】:
以上是关于在 Laravel 4.2 中覆盖 Mail MailgunTransport 类的主要内容,如果未能解决你的问题,请参考以下文章
如何在使用 Laravel 在控制器中发送邮件之前更改邮件配置?
Laravel 5.2 邮件发送中的 Swift_TransportException