From 5bf670c0ecf914814ebc25079557041b9438dad4 Mon Sep 17 00:00:00 2001 From: Adam Witeszczak Date: Thu, 24 Mar 2022 11:03:21 +0000 Subject: [PATCH 1/4] Added ability to add attachments to emails --- lib/Contracts/Mailable.php | 18 ++++++++++++ lib/Mail/Concerns/HasAttachment.php | 43 +++++++++++++++++++++++++++++ lib/Mail/Mail.php | 6 ++++ 3 files changed, 67 insertions(+) create mode 100644 lib/Mail/Concerns/HasAttachment.php diff --git a/lib/Contracts/Mailable.php b/lib/Contracts/Mailable.php index ad3ce64..2d12b69 100644 --- a/lib/Contracts/Mailable.php +++ b/lib/Contracts/Mailable.php @@ -219,4 +219,22 @@ public function validateSubject(); * @return string API data */ public function buildSubject(): array; + + /** + * Set Attachment + * + * @param $attachment string attachment file object + * @param $mime_type string attachment mime type + * @param $name string attachment filename + * + * @return mixed + */ + public function setAttachment(string $attachment, string $mime_type, string $name); + + /** + * Build Attachment + * + * @return array + */ + public function buildAttachment(); } diff --git a/lib/Mail/Concerns/HasAttachment.php b/lib/Mail/Concerns/HasAttachment.php new file mode 100644 index 0000000..f1a8f90 --- /dev/null +++ b/lib/Mail/Concerns/HasAttachment.php @@ -0,0 +1,43 @@ + + */ +trait HasAttachment +{ + protected $attachments = []; + + public function setAttachment($attachment, $mime_type, $name) + { + $toAttach = new \StdClass(); + $toAttach->content = base64_encode($attachment); + $toAttach->type = $mime_type; + $toAttach->filename = $name; + $toAttach->disposition = 'attachment'; + $this->attachments[] = $toAttach; + + return $this; + } + + public function hasAttachment(): bool + { + return count($this->attachments) > 0; + } + + /** + * Build the Attachment data for the API Request + * + * @return string API data + */ + public function buildAttachment(): array + { + return [ + 'attachments' => $this->attachments + ]; + } +} diff --git a/lib/Mail/Mail.php b/lib/Mail/Mail.php index 924521c..e804e06 100644 --- a/lib/Mail/Mail.php +++ b/lib/Mail/Mail.php @@ -9,6 +9,7 @@ use Xedi\SendGrid\Mail\Concerns\HasRecipients; use Xedi\SendGrid\Mail\Concerns\HasSender; use Xedi\SendGrid\Mail\Concerns\HasSubject; +use Xedi\SendGrid\Mail\Concerns\HasAttachment; /** * Class Mail @@ -18,6 +19,7 @@ */ class Mail implements Mailable { + use HasAttachment; use HasContent; use HasRecipients; use HasSender; @@ -55,6 +57,10 @@ public function send(Client $client): Response $this->buildSubject() ); + if ($this->hasAttachment()) { + $data = array_merge($data, $this->buildAttachment()); + } + return $client->post('v3/mail/send', $data); } } From d212048af4f99b13ce4f0717528f36345d61dd60 Mon Sep 17 00:00:00 2001 From: Adam Witeszczak Date: Thu, 24 Mar 2022 11:17:05 +0000 Subject: [PATCH 2/4] Added missing doc blocks --- lib/Mail/Concerns/HasAttachment.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Mail/Concerns/HasAttachment.php b/lib/Mail/Concerns/HasAttachment.php index f1a8f90..bfc3608 100644 --- a/lib/Mail/Concerns/HasAttachment.php +++ b/lib/Mail/Concerns/HasAttachment.php @@ -12,6 +12,15 @@ trait HasAttachment { protected $attachments = []; + /** + * Set Attachment + * + * @param $attachment string attachment file as string + * @param $mime_type string mime type + * @param $name string name + * + * @return $this + */ public function setAttachment($attachment, $mime_type, $name) { $toAttach = new \StdClass(); @@ -24,6 +33,11 @@ public function setAttachment($attachment, $mime_type, $name) return $this; } + /** + * Has Attachment + * + * @return bool + */ public function hasAttachment(): bool { return count($this->attachments) > 0; From 14746c3510a7dd80fc10405bc655398b77c85857 Mon Sep 17 00:00:00 2001 From: ETY Date: Thu, 24 Mar 2022 13:01:57 +0000 Subject: [PATCH 3/4] signed commit --- lib/Mail/Mail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Mail/Mail.php b/lib/Mail/Mail.php index e804e06..9c812cc 100644 --- a/lib/Mail/Mail.php +++ b/lib/Mail/Mail.php @@ -41,7 +41,7 @@ public function validate() /** * Send the Mailable * - * @param Client\Client $client Transmission Adapter + * @param Client\Client $client Transmission Adapters * * @return Response An implementation of the Response contract */ From d91ee7aeefb4492181efb3454b04580cdda964d0 Mon Sep 17 00:00:00 2001 From: ETY Date: Thu, 24 Mar 2022 13:08:24 +0000 Subject: [PATCH 4/4] signed commit --- lib/Mail/Mail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Mail/Mail.php b/lib/Mail/Mail.php index 9c812cc..e804e06 100644 --- a/lib/Mail/Mail.php +++ b/lib/Mail/Mail.php @@ -41,7 +41,7 @@ public function validate() /** * Send the Mailable * - * @param Client\Client $client Transmission Adapters + * @param Client\Client $client Transmission Adapter * * @return Response An implementation of the Response contract */