如何将Paubox与Laravel集成?

时间:2019-04-17 05:49:48

标签: laravel laravel-5

我尝试将Paubox与Laravel 5集成为自定义邮件提供程序。 我怎样才能做到最好? 我安装了https://github.com/Paubox/paubox-php机器人,但没有说明如何将其与Laravel连接。

1 个答案:

答案 0 :(得分:0)

我通过创建新的服务提供商和Transport类来解决此问题。

  1. 在config / app.php中更改默认邮件服务提供商:
// Illuminate\Mail\MailServiceProvider::class,
App\Providers\PauboxServiceProvider::class,
  1. 将凭据添加到config / mail.php:
'pauboxApiKey' => env('PAUBOX_API_KEY'),
'pauboxApiUser' => env('PAUBOX_API_USER')
  1. .env中的更改
MAIL_DRIVER=paubox
PAUBOX_API_KEY=
PAUBOX_API_USER=
  1. 创建新的服务提供商app / 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);
        });   
    } 
}
  1. 创建运输经理app / Providers / PauboxTransportManager.php
<?php
namespace App\Providers;
use Illuminate\Mail\TransportManager;
class PauboxTransportManager extends TransportManager {
    protected function createPauboxDriver() {
        return new PauboxTransport;
    }
}
  1. 创建运输应用/Providers/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;
    }
}
  1. 我将https://github.com/Paubox/paubox-php复制到了App \ Vendors \ Paubox。我必须这样做,因为与作曲家一起安装的paubox无法读取我的.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;

A还必须安装composer require nategood/httpful