gpt4 book ai didi

php - 向 Laravel 的 Mailer 添加新的传输驱动程序

转载 作者:行者123 更新时间:2023-12-03 23:14:47 24 4
gpt4 key购买 nike

我需要在 Laravel 的邮件包中添加一个新的传输驱动程序,以便我可以通过默认情况下不支持的外部服务 (Mailjet) 发送电子邮件。

编写传输驱动程序不会有问题,但我找不到一种方法来 Hook 并添加一个新的,所以我可以继续正常使用 Laravel 的邮件程序。我找不到任何关于扩展 Mailer 的文档。

我能想出的唯一方法是替换 Laravel 的 MailServiceProvider 的位置。在 config/app.php 中被引用与我自己的服务提供商,然后我可以用它来注册我自己的TransportManager和我自己的运输司机。

有没有更好的方法来添加另一个运输驱动程序?

最佳答案

好吧,我已经设法让它按照我在问题中建议的方式工作(通过编写我自己的 ServiceProviderTransportManager 以允许我提供驱动程序)。这是我为任何可能遇到此问题的人所做的:

配置/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);
});
}
}

应用程序/MyMailer/TransportManager.php - 添加 createMailjetDriver使我的 MailjetTransport 的方法Laravel 邮件程序可用的驱动程序
<?php

namespace App\MyMailer;

use App\MyMailer\Transport\MailjetTransport;

class TransportManager extends \Illuminate\Mail\TransportManager
{
protected function createMailjetDriver()
{
$config = $this->app['config']->get('services.mailjet', []);

return new MailjetTransport(
$this->getHttpClient($config),
$config['api_key'],
$config['secret_key']
);
}
}

应用程序/MyMailer/Transport/MailjetTransport.php - 我自己的通过 Mailjet 发送电子邮件的传输驱动程序。

更新以包含我对 Mailjet 传输驱动程序的实现。使用 the Mailjet guide for sending a basic e-mail through their API作为基础。
<?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);
}
}
}
}

在我的 .env我有文件:
MAIL_DRIVER=mailjet

..这让我可以正常使用 Laravel 的邮件程序包(通过 Facade 或依赖注入(inject)):
\Mail::send('view', [], function($message) {
$message->to('me@domain.com');
$message->subject('Test');
});

关于php - 向 Laravel 的 Mailer 添加新的传输驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102483/

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