gpt4 book ai didi

php - 在 Laravel 4.2 中重写 Mail MailgunTransport 类

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:12 26 4
gpt4 key购买 nike

使用 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 classmap 加载了新文件的 app/custom/extensions 目录...

{
...
"autoload": {
"classmap": [
...
"app/custom/extensions"
],
},
...
}

我已经将原来的 'Illuminate\Mail\MailServiceProvider' 换成了 'custom\extensions\CustomMailServiceProvider'...

'providers' => array(
...
//'Illuminate\Mail\MailServiceProvider',
'custom\extensions\CustomMailServiceProvider',
...
),

但是,此时我不知道如何调用邮件功能。如果我尝试使用 Mail facade,它会使用来自 MailgunTransport.php 的原始代码

我需要创建自定义外观吗?如果是这样......我该怎么做?还是上面的代码有问题?有什么方法可以只扩展 MailgunTransport.php 而无需创建 CustomMailServiceProvider?

最佳答案

我通过在 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),
],
]);
}
}

关于php - 在 Laravel 4.2 中重写 Mail MailgunTransport 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39836236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com