From 8eabb73759abc7d3814d13a58a9334c63272e4b5 Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Fri, 11 Nov 2016 09:54:11 -0600 Subject: [PATCH 1/2] Adding access to the Swift sendPerformed plugin function --- src/Illuminate/Mail/Transport/LogTransport.php | 2 ++ .../Mail/Transport/MailgunTransport.php | 2 ++ .../Mail/Transport/MandrillTransport.php | 2 ++ src/Illuminate/Mail/Transport/SesTransport.php | 2 ++ .../Mail/Transport/SparkPostTransport.php | 2 ++ src/Illuminate/Mail/Transport/Transport.php | 18 ++++++++++++++++++ 6 files changed, 28 insertions(+) diff --git a/src/Illuminate/Mail/Transport/LogTransport.php b/src/Illuminate/Mail/Transport/LogTransport.php index b7a3e063f1a4..26c2c220f0bc 100644 --- a/src/Illuminate/Mail/Transport/LogTransport.php +++ b/src/Illuminate/Mail/Transport/LogTransport.php @@ -35,6 +35,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) $this->logger->debug($this->getMimeEntityString($message)); + $this->sendPerformed($message); + return $this->numberOfRecipients($message); } diff --git a/src/Illuminate/Mail/Transport/MailgunTransport.php b/src/Illuminate/Mail/Transport/MailgunTransport.php index f8378322868d..f39a7ed8916e 100644 --- a/src/Illuminate/Mail/Transport/MailgunTransport.php +++ b/src/Illuminate/Mail/Transport/MailgunTransport.php @@ -78,6 +78,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) $this->client->post($this->url, $options); + $this->sendPerformed($message); + return $this->numberOfRecipients($message); } diff --git a/src/Illuminate/Mail/Transport/MandrillTransport.php b/src/Illuminate/Mail/Transport/MandrillTransport.php index f27a03e2934e..68b7bd63be8e 100644 --- a/src/Illuminate/Mail/Transport/MandrillTransport.php +++ b/src/Illuminate/Mail/Transport/MandrillTransport.php @@ -56,6 +56,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) $this->client->post('https://mandrillapp.com/api/1.0/messages/send-raw.json', $options); + $this->sendPerformed($message); + return $this->numberOfRecipients($message); } diff --git a/src/Illuminate/Mail/Transport/SesTransport.php b/src/Illuminate/Mail/Transport/SesTransport.php index 2132ac792afc..8c9237bfb994 100644 --- a/src/Illuminate/Mail/Transport/SesTransport.php +++ b/src/Illuminate/Mail/Transport/SesTransport.php @@ -39,6 +39,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) ], ]); + $this->sendPerformed($message); + return $this->numberOfRecipients($message); } } diff --git a/src/Illuminate/Mail/Transport/SparkPostTransport.php b/src/Illuminate/Mail/Transport/SparkPostTransport.php index b4b47b830760..aef3983cd9ef 100644 --- a/src/Illuminate/Mail/Transport/SparkPostTransport.php +++ b/src/Illuminate/Mail/Transport/SparkPostTransport.php @@ -72,6 +72,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options); + $this->sendPerformed($message); + return $this->numberOfRecipients($message); } diff --git a/src/Illuminate/Mail/Transport/Transport.php b/src/Illuminate/Mail/Transport/Transport.php index deaba986c5e1..9577aa584848 100644 --- a/src/Illuminate/Mail/Transport/Transport.php +++ b/src/Illuminate/Mail/Transport/Transport.php @@ -68,6 +68,24 @@ protected function beforeSendPerformed(Swift_Mime_Message $message) } } + /** + * Iterate through registered plugins and execute plugins' methods. + * + * @param \Swift_Mime-Message $message + * @return void + */ + protected function sendPerformed(Swift_Mime_Message $message) + { + + $event = new Swift_Events_SendEvent($this, $message); + + foreach ($this->plugins as $plugin) { + if (method_exists($plugin, 'sendPerformed')) { + $plugin->sendPerformed($event); + } + } + } + /** * Get the number of recipients. * From c15e1142e21a3e3046e6f74a5363f0f8db439149 Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Fri, 11 Nov 2016 10:03:55 -0600 Subject: [PATCH 2/2] Store the SES message ID in the header X-SES-Message-ID after the message has been sent. --- .../Mail/Transport/SesTransport.php | 7 +++-- tests/Mail/MailSesTransportTest.php | 28 ++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Mail/Transport/SesTransport.php b/src/Illuminate/Mail/Transport/SesTransport.php index 8c9237bfb994..71d22179f0a5 100644 --- a/src/Illuminate/Mail/Transport/SesTransport.php +++ b/src/Illuminate/Mail/Transport/SesTransport.php @@ -32,12 +32,15 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) { $this->beforeSendPerformed($message); - $this->ses->sendRawEmail([ + $headers = $message->getHeaders(); + + // Send the message to SES, storing the SES message id in X-SES-Message-ID + $headers->addTextHeader('X-SES-Message-ID',$this->ses->sendRawEmail([ 'Source' => key($message->getSender() ?: $message->getFrom()), 'RawMessage' => [ 'Data' => $message->toString(), ], - ]); + ])->get('MessageId')); $this->sendPerformed($message); diff --git a/tests/Mail/MailSesTransportTest.php b/tests/Mail/MailSesTransportTest.php index d10da2f70005..ff8adbd3cecd 100644 --- a/tests/Mail/MailSesTransportTest.php +++ b/tests/Mail/MailSesTransportTest.php @@ -45,13 +45,39 @@ public function testSend() ->getMock(); $transport = new SesTransport($client); + // Generate a messageId for our mock to return to ensure that the post-sent message + // has X-SES-Message-ID in its headers + $messageId = str_random(32); + $sendRawEmailMock = new sendRawEmailMock($messageId); $client->expects($this->once()) ->method('sendRawEmail') ->with($this->equalTo([ 'Source' => 'myself@example.com', 'RawMessage' => ['Data' => (string) $message], - ])); + ])) + ->willReturn($sendRawEmailMock); $transport->send($message); + $this->assertEquals($messageId, $message->getHeaders()->get('X-SES-Message-ID')->getFieldBody()); } } + +class sendRawEmailMock { + + protected $getResponse = null; + + public function __construct($responseValue) + { + $this->getResponse = $responseValue; + } + + /** + * Mock the get() call for the sendRawEmail response + * @param [type] $key [description] + * @return [type] [description] + */ + public function get($key) + { + return $this->getResponse; + } +} \ No newline at end of file