Skip to content

Commit

Permalink
[5.2] Sparkpost: fix attachments and plain version
Browse files Browse the repository at this point in the history
  • Loading branch information
billmn committed May 28, 2016
1 parent 5b88244 commit fea984d
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions src/Illuminate/Mail/Transport/SparkPostTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Illuminate\Mail\Transport;

use Swift_Encoding;
use Swift_MimePart;
use Swift_Attachment;
use Swift_Mime_Message;
use GuzzleHttp\ClientInterface;
use Illuminate\Support\Collection;

class SparkPostTransport extends Transport
{
Expand Down Expand Up @@ -54,21 +57,16 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
'recipients' => $recipients,
'content' => [
'html' => $message->getBody(),
'text' => $this->getPlainBody($message),
'from' => $this->getFrom($message),
'reply_to' => $this->getReplyTo($message),
'subject' => $message->getSubject(),
],
],
];

if ($attachments = $message->getChildren()) {
$options['json']['content']['attachments'] = array_map(function ($attachment) {
return [
'type' => $attachment->getContentType(),
'name' => $attachment->getFileName(),
'data' => Swift_Encoding::getBase64Encoding()->encodeString($attachment->getBody()),
];
}, $attachments);
if ($attachments = $this->getAttachments($message) and ! $attachments->isEmpty()) {
$options['json']['content']['attachments'] = $attachments->values();
}

return $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options);
Expand Down Expand Up @@ -108,7 +106,7 @@ protected function getRecipients(Swift_Mime_Message $message)
/**
* Get the "from" contacts in the format required by SparkPost.
*
* @param Swift_Mime_Message $message
* @param \Swift_Mime_Message $message
* @return array
*/
protected function getFrom(Swift_Mime_Message $message)
Expand All @@ -121,7 +119,7 @@ protected function getFrom(Swift_Mime_Message $message)
/**
* Get the 'reply_to' headers and format as required by SparkPost.
*
* @param Swift_Mime_Message $message
* @param \Swift_Mime_Message $message
* @return string
*/
protected function getReplyTo(Swift_Mime_Message $message)
Expand All @@ -131,6 +129,44 @@ protected function getReplyTo(Swift_Mime_Message $message)
}
}

/**
* Get the plain body content of this entity as a string.
*
* @param \Swift_Mime_Message $message
* @return string
*/
protected function getPlainBody(Swift_Mime_Message $message)
{
$parts = Collection::make($message->getChildren())
->filter(function ($item, $key) {
return $item instanceof Swift_MimePart;
})
->first();

return $parts ? $parts->getBody() : $message->getBody();
}

/**
* Get message attachments.
*
* @param \Swift_Mime_Message $message
* @return \Illuminate\Support\Collection
*/
protected function getAttachments(Swift_Mime_Message $message)
{
return Collection::make($message->getChildren())
->filter(function ($value, $key) {
return $value instanceof Swift_Attachment;
})
->map(function ($item, $key) {
return [
'type' => $item->getContentType(),
'name' => $item->getFileName(),
'data' => Swift_Encoding::getBase64Encoding()->encodeString($item->getBody()),
];
});
}

/**
* Get the API key being used by the transport.
*
Expand Down

0 comments on commit fea984d

Please sign in to comment.