2016-08-23 2 views
0

기본적으로 지원되지 않는 외부 서비스 (Mailjet)를 통해 전자 메일을 보낼 수 있도록 Laravel의 메일 패키지에 새 전송 드라이버를 추가해야합니다.Laravel 메일러에 새 전송 드라이버 추가

전송 드라이버를 작성하는 것은 문제가되지 않지만 일반적인 방법으로 Laravel의 메일러를 계속 사용할 수 있도록 후크하고 추가하는 방법을 찾을 수 없습니다. 메일러를 확장하는 데 필요한 설명서를 찾을 수 없습니다. Laravel의 MailServiceProvider이 그때 내 자신의 TransportManager 내 자신의 전송 드라이버를 등록하는 데 사용할 수있는 내 자신의 서비스 제공 업체와 config/app.php에서 참조되는 위치 나는 것 가지고 올 수

유일한 방법은 대체.

다른 전송 드라이버를 추가하는 더 좋은 방법이 있습니까?

+1

예, 자신 만의'Mailer' 클래스를 만들어야합니다. 클래스는 [Mailer] (https://github.com/laravel/framework/blob/5.1/src/Illuminate/Contracts/Mail/Mailer.php)와 [MailQueue] (https://github.com)을 구현해야합니다. /laravel/framework/blob/5.1/src/Illuminate/Contracts/Mail/MailQueue.php) 인터페이스. – Skysplit

+0

답장을 보내 주셔서 감사합니다. 그저 다른 서비스에 대한 내 자신의 드라이버를 추가 할 수있는 것 같아요. 가능한 한 많은 Laravel의 기능에 관심을두고 있습니다. 지금까지 구현 한 방법에 대한 내 대답을 확인하십시오. – Jonathon

답변

5

글쎄, 내 질문에 제안한 방식대로 작동하도록했습니다. (필자 자신이 ServiceProviderTransportManager을 써서 드라이버를 제공 할 수있게했습니다.) 이것은 내가이 건너 올 수도 누군가를 위해 한 일입니다 :

설정/app.php - 교체 Laravel의 MailServiceProvider

// ... 
'providers' => [ 
    // ... 

    // Illuminate\Mail\MailServiceProvider::class, 
    App\MyMailer\MailServiceProvider::class, 

    // ... 

자신의

응용 프로그램/MyMailer/MailServiceProvider.php - registerSwiftTransport() 방법을 Laravel의 MailServiceProvider를 확장하는 서비스 제공을 만들고 우선

<?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는 - Laravel의 메일러 사용할 내 MailjetTransport 드라이버를 만드는 createMailjetDriver 방법을 추가

<?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/운송/MailjetTransport.php - 내 자신의 전송 드라이버는 전자 보냅니다 Mailjet을 통한 메일.

Mailjet 전송 드라이버 구현을 포함하도록 업데이트되었습니다. 을베이스로 사용. 내 .env 파일에서

<?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); 
      } 
     } 
    } 
} 

내가 가진 : 나를 정상적으로 (외관이나 의존성 주입을 통해) Laravel의 메일러 패키지를 사용할 수 있습니다 요식

MAIL_DRIVER=mailjet 

:

\Mail::send('view', [], function($message) { 
    $message->to('[email protected]'); 
    $message->subject('Test'); 
}); 
+0

끝까지 작동 시켰습니까? 아마도 Mailjet API를 사용하고 있을까요? 'MailjetTransport :: send()' –

+0

아직 구현이 완료되지 않았지만 이전에 Mailjet API를 구현 했으므로 완료되면 제대로 작동 할 것이라고 확신합니다. Guzzle을 사용하여 '보내기'엔드 포인트를 맞출 것입니다. – Jonathon

+0

팁 주셔서 감사합니다, 나는 그것을 또한 시도 할 것이다. –

관련 문제