From 8d50197efb6f217eb7c4618d6faaab910f8742de Mon Sep 17 00:00:00 2001 From: Joel <34149088+semijoelon@users.noreply.github.com> Date: Wed, 12 Aug 2020 10:52:07 -0400 Subject: [PATCH] docs: Added S3 attachment example (#729) --- USE_CASES.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/USE_CASES.md b/USE_CASES.md index 82cd41888..31d7bff2d 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -4,6 +4,7 @@ This documentation provides examples for specific use cases. Please [open an iss - [Table of Contents](#table-of-contents) - [Attachments](#attachments) - [Attaching a File from Box](#attaching-a-file-from-box) +- [Attaching a File from S3](#attaching-a-file-from-s3) - [Kitchen Sink - an example with all settings used](#kitchen-sink) - [Send an Email to a Single Recipient](#send-an-email-to-a-single-recipient) - [Send an Email to Multiple Recipients](#send-an-email-to-multiple-recipients) @@ -235,6 +236,66 @@ if (isset($fileId) && isset($userId)){ } ``` + +# Attaching a File from S3 + +You can attach a file from [Amazon S3 storage](https://aws.amazon.com/s3/) to your emails. In addition to sendgrid-php, this requires the [AWS SDK for PHP](https://aws.amazon.com/sdk-for-php/). Please follow the [Getting Started](https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_index.html) tutorial at Amazon if you haven’t set up your AWS SDK installation yet. + +```php + 'latest', + 'region' => 'us-east-1' // This should match the region of your S3 bucket. +]); +try { + // Get the object. + // See https://docs.aws.amazon.com/AmazonS3/latest/dev/RetrieveObjSingleOpPHP.html + $result = $s3->getObject([ + 'Bucket' => $bucket, + 'Key' => $keyname + ]); + $keyExplode = explode('/',$keyname); + $attachmentFilename = end($keyExplode); + $attachmentContent = base64_encode($result['Body']); + $attachmentContentType = $result['ContentType']; + + $email = new \SendGrid\Mail\Mail(); + $email->setFrom("test@example.com", "Example User"); + $email->setSubject("Attaching a File from S3"); + $email->addTo("test@example.com", "Example User"); + $email->addContent("text/plain", "See attached file from S3."); + $email->addContent( + "text/html", "See attached file from S3." + ); + + $attachment = new \SendGrid\Mail\Attachment(); + $attachment->setContent($attachmentContent); + $attachment->setType($attachmentContentType); + $attachment->setFilename($attachmentFilename); + $attachment->setDisposition("attachment"); + $attachment->setContentId($attachmentFilename); //Only used if disposition is set to inline + $email->addAttachment($attachment); + $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); + try { + $response = $sendgrid->send($email); + print $response->statusCode() . "\n"; + print_r($response->headers()); + print $response->body() . "\n"; + } catch (Exception $e) { + echo 'Caught exception: '. $e->getMessage() ."\n"; + } +} catch (S3Exception $e) { + echo 'Caught exception: '. $e->getMessage() . "\n"; +} +``` + # Kitchen Sink - an example with all settings used