Я решаю это путем создания нового поставщика услуг и класса транспорта.
- Изменить поставщика почтовых услуг по умолчанию в config / app.php:
// Illuminate\Mail\MailServiceProvider::class,
App\Providers\PauboxServiceProvider::class,
- Добавить учетные данные в config / mail.php:
'pauboxApiKey' => env('PAUBOX_API_KEY'),
'pauboxApiUser' => env('PAUBOX_API_USER')
- Изменения в .env
MAIL_DRIVER=paubox
PAUBOX_API_KEY=
PAUBOX_API_USER=
- Создание нового приложения поставщика услуг / Providers / PauboxServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Mail\MailServiceProvider;
class PauboxServiceProvider extends MailServiceProvider {
protected function registerSwiftTransport() {
$this->app->singleton('swift.transport', function ($app) {
return new PauboxTransportManager($app);
});
}
}
- Создание приложения менеджера транспорта / Поставщики / PauboxTransportManager.php
<?php
namespace App\Providers;
use Illuminate\Mail\TransportManager;
class PauboxTransportManager extends TransportManager {
protected function createPauboxDriver() {
return new PauboxTransport;
}
}
- Создание транспортного приложения / Поставщики / PauboxTransport.php
<?php
namespace App\Providers;
use Illuminate\Mail\Transport\Transport;
use Swift_Mime_SimpleMessage;
use App\Vendors\Paubox as Paubox;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Illuminate\Support\Facades\Log;
class PauboxTransport extends Transport {
protected $config;
public function __construct() {
$this->config = config('mail');
}
public function send(Swift_Mime_SimpleMessage $msg, &$failedRecipients = null) {
$paubox = new Paubox\Paubox($this->config['pauboxApiUser'], $this->config['pauboxApiKey']);
$message = new Paubox\Mail\Message();
$content = new Paubox\Mail\Content();
$header = new Paubox\Mail\Header();
$content->setHtmlText($msg->getBody());
$header->setSubject($msg->getSubject());
$header->setFrom('"' . $this->config['from']['name'] . '" <' . $this->config['from']['address'] . '>');
$header->setReplyTo($this->config['from']['address']);
$recipients = [];
foreach ($msg->getTo() as $to => $val) {
recipients[] = $to;
}
$message->setHeader($header);
$message->setContent($content);
$message->setRecipients($recipients);
$sendMessageResponse = new Paubox\Mail\SendMessageResponse();
$sendMessageResponse = $paubox->sendMessage($message);
$errorMsg = '';
if (isset($sendMessageResponse->errors)) {
foreach ($sendMessageResponse->errors as $error) {
$errorMsg .= json_encode($error);
}
Log::error(PHP_EOL . "Paubox: " . $errorMsg . PHP_EOL);
throw new UnprocessableEntityHttpException('Error occurred while sending email');
}
return $sendMessageResponse;
}
}
- Я скопировал https://github.com/Paubox/paubox-php в App \ Vendors \ Paubox. Я должен был сделать это, потому что paubox, установленный с composer, не прочитал мои .env данные После копирования мне пришлось изменить пространства имен во всех файлах и добавить конструктор Paubox для передачи api_key и api_user:
public function __construct($pauboxApiUser, $pauboxApiKey)
{
$this->pauboxApiUser = $pauboxApiUser;
$this->pauboxApiKey = $pauboxApiKey;
}
и ниже в коде:
изменить \getenv('PAUBOX_API_USER');
в $this->pauboxApiUser;
и изменить \getenv('PAUBOX_API_KEY');
в $this->pauboxApiKey;
А пришлось также установить composer require nategood/httpful