​How to Store the Contents of Email Sent via SendGrid

​How to Store the Contents of Email Sent via SendGrid

Product, Technical
TwitterFacebookLinkedIn

Whether you need to verify that a customer received a specific message, or you’re looking to see which events are tied to specific campaigns, storing the contents of your messages can be helpful for a lot of senders. I’ve come up with a few different ways you can store the email contents of your SendGrid sends for future reference:

Storing Metadata in SendGrid Logs

If you want to see additional metadata, e.g. from addresses and subject lines, about your emails in your Email Activity log or via the event webhook, you can use existing SendGrid features.

Showing subject line in Email Activity log

If multiple emails will have the same metadata, such as a batch of messages without a custom subject line, you can add a category that contains the subject line. If each email has a distinct subject line, you can add that as metadata using a unique argument. The same workflow works for anything you want to see in your logs, whether it’s a user-id from your system or a template version number.

Using the Inbound Parse Webhook

The Inbound Parse Webhook provides a means of capturing content you send. By configuring an Inbound Parse Webhook endpoint, you’ll be able to programmatically respond to emails sent to a domain you control. You can then add an email to your configured Inbound Parse Webhook domain to your sends, depending on your needs. You might want to send a BCC for every email sent via SendGrid, or you might just want to add an additional personalization block to each batch of messages you send. You can configure the Inbound Parse Webhook via your account settings or using the API.

Depending on what data you’d like to capture about the content you send, there are two ways to use SendGrid’s Inbound Parse Webhook.

multipart/form-data

By default, your endpoint will receive a multipart/form-data POST with parameters describing the email. If you only want to capture some data e.g. the from address, subject line or attachments; or you want to put the email data in a structured data store, use this method.

Raw MIME

You can also configure the parse webhook to send raw MIME instead of actually parsing the data. Think of this as an email pass-through; instead of SendGrid parsing and chopping up the email, you’ll get the whole thing to do with as you wish. If you want to store a complete, unfragmented copy of every email, you should use raw mode.

A screenshot of the parse webhook settings page.

Screenshot of the parse webhook settings page.

Send a Copy to Gmail

Gmail has a nice JSON-based API that you can use to search, retrieve, delete, and label emails that you receive. By sending a copy of the messages you want to store to a Gmail address, you get a robust system designed for handling email specifically with very little configuration needed. And if there are people on your team that may need to search the archives, they can use any email client they want.

Capture it Locally or via Proxy

Capturing your mail locally before sending it to SendGrid for processing has the advantage of not requiring any additional email credits to forward or BCC emails, but it has a few disadvantages. You will need to run a local service to capture email, and you won’t get all of the template processing or mail-merge features that happen on the SendGrid server side. Depending on your platform and needs, there are a variety of libraries and services that you can use. Check out mailcatcher, fakeSMTP, and papercut for examples. And if you’d like a hosted solution, you can try mailtrap.


Brandon West
More Posts by Brandon
As Director of Developer Relations for SendGrid, Brandon's focus is on empowering developers to build things, gathering feedback for new features and improvements, and fostering a cooperative developer community for anything that needs email integration.
Follow Brandon