使用mailgun作为邮件驱动程序我遇到了盲目复制无法正常工作的问题,因为它显示了所有收件人的所有地址。我发现了一个可以解决问题的修复程序,但它需要在vendor / laravel / framework / src / Illuminate / Mail / Transport / MailgunTransport.php中编辑MailgunTransport.php文件。我不想更改vendor文件夹上的文件,所以我试图扩展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 classmap中加载了新文件的app / custom / extensions目录...
{
...
"autoload": {
"classmap": [
...
"app/custom/extensions"
],
},
...
}
我已经交换了原来的&lt; Illuminate \ Mail \ MailServiceProvider&#39;到&#39; custom \ extensions \ CustomMailServiceProvider&#39; ...
'providers' => array(
...
//'Illuminate\Mail\MailServiceProvider',
'custom\extensions\CustomMailServiceProvider',
...
),
但是,此时我不知道如何调用邮件功能。如果我尝试使用Mail Facade,它将使用MailgunTransport.php中的原始代码
我是否需要创建自定义外观?如果是的话......我怎么能这样做?或者上面的代码有什么问题吗?有没有办法只扩展MailgunTransport.php而不创建CustomMailServiceProvider?
答案 0 :(得分: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),
],
]);
}
}