我需要在Laravel的邮件包中添加一个新的传输驱动程序,这样我就可以通过默认不支持的外部服务(Mailjet)发送电子邮件。
编写传输驱动程序不会是一个问题,但我找不到一种方法来挂钩并添加一个新的,所以我可以继续正常使用Laravel的邮件器。我找不到任何关于扩展Mailer的文档。
我能想到的唯一方法是用我自己的服务提供商替换Laravel的MailServiceProvider
在config/app.php
中被引用的地方,然后我可以用它来注册我自己的TransportManager
和我自己的运输驱动程序。
是否有更好的方法来添加另一个运输驱动程序?
嗯,我已经设法让它在我的问题中建议的方式工作(通过编写我自己的ServiceProvider
和TransportManager
,让我提供一个驱动程序)。这是我为可能遇到这种情况的人所做的:
config/app.php -用我自己的
替换Laravel的MailServiceProvider
// ...
'providers' => [
// ...
// IlluminateMailMailServiceProvider::class,
AppMyMailerMailServiceProvider::class,
// ...
app/MyMailer/MailServiceProvider.php -创建一个扩展Laravel的MailServiceProvider
和覆盖registerSwiftTransport()
方法的服务提供商
<?php
namespace AppMyMailer;
class MailServiceProvider extends IlluminateMailMailServiceProvider
{
public function registerSwiftTransport()
{
$this->app['swift.transport'] = $this->app->share(function ($app) {
// Note: This is my own implementation of transport manager as shown below
return new TransportManager($app);
});
}
}
app/MyMailer/TransportManager.php -添加一个createMailjetDriver
方法,使我的MailjetTransport
驱动程序可用于Laravel的Mailer
<?php
namespace AppMyMailer;
use AppMyMailerTransportMailjetTransport;
class TransportManager extends IlluminateMailTransportManager
{
protected function createMailjetDriver()
{
$config = $this->app['config']->get('services.mailjet', []);
return new MailjetTransport(
$this->getHttpClient($config),
$config['api_key'],
$config['secret_key']
);
}
}
app/MyMailer/Transport/MailjetTransport.php -我自己的Transport驱动程序,通过Mailjet发送电子邮件。
已更新以包含我的Mailjet传输驱动程序的实现。使用Mailjet指南通过他们的API发送基本的电子邮件作为基础。
<?php
namespace AppMyMailerTransport;
use GuzzleHttpClientInterface;
use IlluminateMailTransportTransport;
use Swift_Mime_Message;
class MailjetTransport extends Transport
{
/**
* Guzzle HTTP client.
*
* @var ClientInterface
*/
protected $client;
/**
* The Mailjet "API key" which can be found at https://app.mailjet.com/transactional
*
* @var string
*/
protected $apiKey;
/**
* The Mailjet "Secret key" which can be found at https://app.mailjet.com/transactional
*
* @var string
*/
protected $secretKey;
/**
* The Mailjet end point we're using to send the message.
*
* @var string
*/
protected $endPoint = 'https://api.mailjet.com/v3/send';
/**
* Create a new Mailjet transport instance.
*
* @param GuzzleHttpClientInterface $client
* @param $apiKey
* @param $secretKey
*/
public function __construct(ClientInterface $client, $apiKey, $secretKey)
{
$this->client = $client;
$this->apiKey = $apiKey;
$this->secretKey = $secretKey;
}
/**
* Send the given Message.
*
* Recipient/sender data will be retrieved from the Message API.
* The return value is the number of recipients who were accepted for delivery.
*
* @param Swift_Mime_Message $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$payload = [
'header' => ['Content-Type', 'application/json'],
'auth' => [$this->apiKey, $this->secretKey],
'json' => []
];
$this->addFrom($message, $payload);
$this->addSubject($message, $payload);
$this->addContent($message, $payload);
$this->addRecipients($message, $payload);
return $this->client->post($this->endPoint, $payload);
}
/**
* Add the from email and from name (If provided) to the payload.
*
* @param Swift_Mime_Message $message
* @param array $payload
*/
protected function addFrom(Swift_Mime_Message $message, &$payload)
{
$from = $message->getFrom();
$fromAddress = key($from);
if ($fromAddress) {
$payload['json']['FromEmail'] = $fromAddress;
$fromName = $from[$fromAddress] ?: null;
if ($fromName) {
$payload['json']['FromName'] = $fromName;
}
}
}
/**
* Add the subject of the email (If provided) to the payload.
*
* @param Swift_Mime_Message $message
* @param array $payload
*/
protected function addSubject(Swift_Mime_Message $message, &$payload)
{
$subject = $message->getSubject();
if ($subject) {
$payload['json']['Subject'] = $subject;
}
}
/**
* Add the content/body to the payload based upon the content type provided in the message object. In the unlikely
* event that a content type isn't provided, we can guess it based on the existence of HTML tags in the body.
*
* @param Swift_Mime_Message $message
* @param array $payload
*/
protected function addContent(Swift_Mime_Message $message, &$payload)
{
$contentType = $message->getContentType();
$body = $message->getBody();
if (!in_array($contentType, ['text/html', 'text/plain'])) {
$contentType = strip_tags($body) != $body ? 'text/html' : 'text/plain';
}
$payload['json'][$contentType == 'text/html' ? 'Html-part' : 'Text-part'] = $message->getBody();
}
/**
* Add to, cc and bcc recipients to the payload.
*
* @param Swift_Mime_Message $message
* @param array $payload
*/
protected function addRecipients(Swift_Mime_Message $message, &$payload)
{
foreach (['To', 'Cc', 'Bcc'] as $field) {
$formatted = [];
$method = 'get' . $field;
$contacts = (array) $message->$method();
foreach ($contacts as $address => $display) {
$formatted[] = $display ? $display . " <$address>" : $address;
}
if (count($formatted) > 0) {
$payload['json'][$field] = implode(', ', $formatted);
}
}
}
}
在我的.env
文件中,我有:
MAIL_DRIVER=mailjet
. .它允许我正常使用Laravel的邮件包(通过Facade或依赖注入):
Mail::send('view', [], function($message) {
$message->to('me@domain.com');
$message->subject('Test');
});