Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/SendGridMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SendGrid\Mail\From;
use SendGrid\Mail\Mail;
use SendGrid\Mail\ReplyTo;
use SendGrid\Mail\Attachment;

class SendGridMessage
{
Expand Down Expand Up @@ -42,6 +43,13 @@ class SendGridMessage
*/
public $payload = [];

/**
* An array of attachments for the message.
*
* @var array
*/
public $attachments = [];

/**
* The sandbox mode for SendGrid
*
Expand Down Expand Up @@ -103,6 +111,26 @@ public function payload($payload)
return $this;
}

public function attach($attachments)
{
/*
$attachments should be an array of individual attachments. content should be base64 encoded.

Example:
$attachments = array(
array(
'content' => base64_encode($content),
'type' => 'application/pdf',
'filename' => 'filename.pdf'
)
);
*/

$this->attachments = $attachments;

return $this;
}

/**
* @return Mail
*/
Expand All @@ -124,6 +152,24 @@ public function build(): Mail
$email->addDynamicTemplateData((string) $key, $value);
}

if (is_array($this->attachments) && !empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
$disposition = (isset($attachment['disposition'])) ? strtolower($attachment['disposition']) : "attachment";

$sgAttachment = new Attachment();
$sgAttachment->setType($attachment['type']);
$sgAttachment->setContent($attachment['content']);
$sgAttachment->setDisposition($disposition);
$sgAttachment->setFilename($attachment['filename']);

if ($disposition === "inline") {
$sgAttachment->setContentID($attachment['filename']);
}

$email->addAttachment($sgAttachment);
}
}

return $email;
}

Expand Down