Php 将新的传输驱动程序添加到Laravel';s邮递员
我需要在Laravel的邮件包中添加一个新的传输驱动程序,这样我就可以通过默认不支持的外部服务(Mailjet)发送电子邮件 编写运输驱动程序不会是一个问题,但我找不到一种方法来连接并添加一个新的驱动程序,这样我就可以继续正常使用Laravel的mailer。我找不到任何关于扩展邮件程序的文档 我能想到的唯一方法是用我自己的服务提供商替换Laravel的Php 将新的传输驱动程序添加到Laravel';s邮递员,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,我需要在Laravel的邮件包中添加一个新的传输驱动程序,这样我就可以通过默认不支持的外部服务(Mailjet)发送电子邮件 编写运输驱动程序不会是一个问题,但我找不到一种方法来连接并添加一个新的驱动程序,这样我就可以继续正常使用Laravel的mailer。我找不到任何关于扩展邮件程序的文档 我能想到的唯一方法是用我自己的服务提供商替换Laravel的MailServiceProvider在config/app.php中被引用的位置,然后我可以用它注册我自己的TransportManager和
MailServiceProvider
在config/app.php
中被引用的位置,然后我可以用它注册我自己的TransportManager
和我自己的传输驱动程序
有没有更好的方法来添加另一个传输驱动程序?我已经按照我在问题中建议的方式(通过编写我自己的
ServiceProvider
和TransportManager
来允许我提供驱动程序)使它工作。这就是我为任何可能遇到这种情况的人所做的:
config/app.php-将Laravel的MailServiceProvider
替换为我自己的
// ...
'providers' => [
// ...
// Illuminate\Mail\MailServiceProvider::class,
App\MyMailer\MailServiceProvider::class,
// ...
app/MyMailer/MailServiceProvider.php-创建一个服务提供商,扩展Laravel的MailServiceProvider
,并覆盖registerSwiftTransport()
方法
<?php
namespace App\MyMailer;
class MailServiceProvider extends \Illuminate\Mail\MailServiceProvider
{
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/Transport/mailjetttransport.php-我自己的传输驱动程序,通过Mailjet发送电子邮件
已更新以包含Mailjet传输驱动程序的我的实现。用作基础的
…这使我能够正常使用Laravel的mailer包(通过Facade或依赖项注入):
\Mail::send('view', [], function($message) {
$message->to('me@domain.com');
$message->subject('Test');
});
是的,您必须创建自己的
Mailer
类。最好,你的类应该实现和接口。谢谢你的回复。仅仅为了能够为另一项服务添加我自己的驱动程序,这似乎有点过头了。我宁愿尽可能多地使用Laravel的功能。请参阅下面我的答案,了解到目前为止我是如何实现它的。最后,您让它工作了吗?也许使用MailJetAPImailjetttransport::send()
我还没有完成它的实现,但我以前已经实现了mailjetapi,所以我很有信心完成后它会工作。我会用Guzzle来达到他们的发送
终点。谢谢你的提示,我也会试试。没问题。我可以在完成后更新我的答案。看看Laravel附带的MailgunTransport
或MandrillTransport
,它们会让您知道需要做什么。那太好了,我已经用过Guzzle,但任何帮助都可能对这里的每个人都有好处:)
<?php
namespace App\MyMailer\Transport;
use GuzzleHttp\ClientInterface;
use Illuminate\Mail\Transport\Transport;
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 \GuzzleHttp\ClientInterface $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);
}
}
}
}
MAIL_DRIVER=mailjet
\Mail::send('view', [], function($message) {
$message->to('me@domain.com');
$message->subject('Test');
});