Skip to content

Commit

Permalink
Fix sendgrid v2 api attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lidbetter committed Aug 12, 2018
1 parent c987ddb commit 2628545
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.DS_store
.idea/*
.idea/*
vendor
composer.lock
47 changes: 37 additions & 10 deletions src/Transport/SendgridTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SendgridTransport extends Transport
protected $client;
protected $options;

protected $incrementer = 0;

public function __construct(ClientInterface $client, $api_key)
{
$this->client = $client;
Expand All @@ -39,6 +41,7 @@ public function __construct(ClientInterface $client, $api_key)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);

list($from, $fromName) = $this->getFromAddresses($message);
$payload = $this->options;
$data = [
Expand All @@ -47,22 +50,28 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
'subject' => $message->getSubject(),
'html' => $message->getBody()
];

$this->setTo($data, $message);
$this->setCc($data, $message);
$this->setBcc($data, $message);
$this->setText($data, $message);
$this->setReplyTo($data, $message);
$this->setAttachment($data, $message);
$this->setSmtpApi($data, $message);

if (version_compare(ClientInterface::VERSION, '6') === 1) {
$payload += ['form_params' => $data];
} else {
$payload += ['body' => $data];
}

$response = $this->client->post('https://api.sendgrid.com/api/mail.send.json', $payload);

$this->sendPerformed($message);

return $response;
}

/**
* @param $data
* @param Swift_Mime_SimpleMessage $message
Expand All @@ -74,6 +83,7 @@ protected function setTo(&$data, Swift_Mime_SimpleMessage $message)
$data['toname'] = array_values($to);
}
}

/**
* @param $data
* @param Swift_Mime_SimpleMessage $message
Expand All @@ -85,6 +95,7 @@ protected function setCc(&$data, Swift_Mime_SimpleMessage $message)
$data['ccname'] = array_values($cc);
}
}

/**
* @param $data
* @param Swift_Mime_SimpleMessage $message
Expand All @@ -96,6 +107,7 @@ protected function setBcc(&$data, Swift_Mime_SimpleMessage $message)
$data['bccname'] = array_values($bcc);
}
}

/**
* @param $data
* @param Swift_Mime_SimpleMessage $message
Expand All @@ -106,7 +118,7 @@ protected function setReplyTo(&$data, Swift_Mime_SimpleMessage $message)
$data['replyto'] = array_keys($replyTo);
}
}

/**
* Get From Addresses.
*
Expand All @@ -115,13 +127,15 @@ protected function setReplyTo(&$data, Swift_Mime_SimpleMessage $message)
*/
protected function getFromAddresses(Swift_Mime_SimpleMessage $message)
{
if ($message->getFrom()) {
foreach ($message->getFrom() as $address => $name) {
if ($from = $message->getFrom()) {
foreach ($from as $address => $name) {
return [$address, $name];
}
}

return [];
}

/**
* Set text contents.
*
Expand All @@ -137,6 +151,7 @@ protected function setText(&$data, Swift_Mime_SimpleMessage $message)
$data['text'] = $attachment->getBody();
}
}

/**
* Set Attachment Files.
*
Expand All @@ -145,15 +160,27 @@ protected function setText(&$data, Swift_Mime_SimpleMessage $message)
*/
protected function setAttachment(&$data, Swift_Mime_SimpleMessage $message)
{
foreach ($message->getChildren() as $attachment) {
if (!$attachment instanceof Swift_Attachment || !strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) {
$attachments = $message->getChildren();

if (!isset($data['files']) && count($attachments) > 0) {
$data['files'] = [];
}

foreach ($attachments as $attachment) {
if (!$attachment instanceof Swift_Attachment || strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) {
continue;
}
$attachmentName = $attachment->getFilename();
if (isset($data['files'][$attachmentName])) {
$attachmentName .= ++$this->incrementer;
}

$handler = tmpfile();
fwrite($handler, $attachment->getBody());
$data['files[' . $attachment->getFilename() . ']'] = $handler;
$data['files'][$attachmentName] = $handler;
}
}

/**
* Set Sendgrid SMTP API
*
Expand All @@ -163,12 +190,12 @@ protected function setAttachment(&$data, Swift_Mime_SimpleMessage $message)
protected function setSmtpApi(&$data, Swift_Mime_SimpleMessage $message)
{
foreach ($message->getChildren() as $attachment) {
if (!$attachment instanceof Swift_Image
|| !in_array(self::SMTP_API_NAME, [$attachment->getFilename(), $attachment->getContentType()])
) {
if (!$attachment instanceof Swift_Attachment || $attachment instanceof Swift_Image) {
continue;
}
$data['x-smtpapi'] = json_encode($attachment->getBody());
if (in_array(self::SMTP_API_NAME, [$attachment->getFilename(), $attachment->getContentType()])) {
$data['x-smtpapi'] = $attachment->getBody();
}
}
}
}

0 comments on commit 2628545

Please sign in to comment.