Build Generative AI Email Experiences using SendGrid Inbound Parse

September 22, 2025
Written by
Reviewed by
Paul Kamp
Twilion

Agentic Email with Twilio SendGrid? Yes, Please! Part 2: Process Inbound Emails with AI

In this post, I’ll show you how to process inbound emails you gather with Twilio SendGrid’s Inbound Parse and respond with Generative AI, letting you build an agentic experience all over email.

In Part 1 of this series, we covered how to receive and process inbound emails (and attachments) with SendGrid’s Inbound Parse. At the end of that tutorial, you had the components of your email in an S3 bucket, and had triggered an SNS message with the details needed to find that bucket. In this post, I’ll show you how to take those components and formulate and send a message with Generative AI.

Let’s get started.

GenAI email agent experience architecture

If you didn’t follow along with part 1 of the series, this solution is broken into two parts:

  1. Inbound Parse with Signature Verification
  2. Process Inbound Emails with Generative AI

In Part 2, here is what you’ll be building:

Flowchart demonstrating EventBridge routing events to different business logic triggers based on summarized attachments.

Let’s start from the top…

  1. Input: The down arrow is an SNS message that gets published once an inbound email has been completely checked and processed by the systems deployed in Part 1. This message lets downstream processors know that there is more work to do.
  2. First Pass: This lambda receives the SNS message and retrieves the content of the email from the S3 bucket using the messageId as the key. The content is then sent to an LLM model via AWS Bedrock. The prompt asks the model to summarize the email and categorize it as Sales, Support, Account, Inquiry, or Conversation. The results are saved in the JSON file, and the lambda publishes an event to an EventBus to route to the next step.
  3. If there are Attachments: If there are attachments, the event is delivered to a lambda that can analyze different types of files. The lambda loads the attachments from the S3 bucket, then submits them to the LLM to generate a summary. The summary is added to the JSON file, and another event is published to the EventBus
  4. Category Handlers: The event is delivered to one of 5 category handlers depending on the category set in the First Pass lambda. Each of these lambdas have their own prompts and business logic. They are all basic, but you should be able to see how you could build specific handlers based on the available context.
  5. Trigger additional Events: The category handlers can trigger additional events (send a reply email, connect to a human agent, update a CRM or support ticket, …). For example, one trigger you could add is to send additional outbound emails in response to the inbound email. This repo attached to this blog post also contains a workflow that sends outbound emails using Twilio SendGrid.

Prerequisites

As said in part 1, this is not a beginner level build. Since you must complete Part 1 as a prerequisite to Part 2, you should have already completed all of the prerequisites and are ready to go!

Return to Part 1 if you have not deployed that yet… here are the services you’ll need to set up before you continue:

This project is a starter kit for an AWS serverless solution for securely processing inbound emails from SendGrid's Inbound Parse webhook. This solution is not production ready. You will need to customize your enterprise’s business logic, add production-ready features, add guardrails to the app, and integrate into existing systems before deploying to production.

Clone the repo and build the app

The instructions to spin up Part 2 are in the repo. Please proceed to the:

🚀 README for Part 2

Run and test the Generative AI email experience

Do you remember we tested our setup by sending an email in Part 1? Let’s send some test emails – or continue from the example in Part 1 – to explore how we can use Generative AI to craft an email experience, now that we know we can receive inbound email and attachments.

Example 1: Process an email inquiry (from Part 1)

Send an email

Remember the email that we sent in Part 1? If you copied my example, I sent the following message:

I would like to purchase one of your products for my dog. Can you make some suggestions? I attached a photo of my dog.

I then attached an (awesome) picture of my dog, Mabel.

Let’s see what part 2 did with it…

Email draft with a photo of a sitting dog on the beach attached, asking for product suggestions.

LLM response

This email was categorized as an inquiry. The inquiry handler took it and crafted a response that included the image attachment. The prompt used to summarize and categorize inbound emails can be viewed in the repo. The prompt is general but will give you a place to start. In production, you will of course enter your own rules to match your business requirements and to anticipate the type of emails you expect to receive and how to handle and identify edge cases.

Here is the JSON:

{
"messageId": "4412b523-3121-45fa-8a66-35772d757b53",
"category": "inquiry",
"confidence": 0.9,
"response": "Dear Dan,\n\nThank you for reaching out to us about product recommendations for your adorable dog! We truly appreciate you taking the time to share a photo of your furry friend enjoying the beach. What a beautiful picture of Mabel (I'm assuming that's her name based on your email subject) sitting so attentively by the ocean!\n\nBased on the image you've shared, it looks like Mabel is a medium to large-sized dog with a dark coat. Given her beach-loving nature, I'd be happy to suggest some products that might be perfect for her:\n\n1. Waterproof collar: Since Mabel seems to enjoy the beach, a waterproof collar could be a great option to withstand sand and saltwater.\n\n2. Beach-friendly toys: Floating toys or frisbees designed for water play could provide hours of fun for Mabel during your beach outings.\n\n3. Cooling mat: For after-beach relaxation, a cooling mat can help Mabel regulate her body temperature, especially if she has a dark coat that absorbs heat.\n\n4. Paw balm: To protect Mabel's paws from hot sand and salty water, a good quality paw balm could be beneficial.\n\n5. Portable water bowl: Staying hydrated at the beach is crucial, so a collapsible water bowl could be a handy accessory.\n\n6. Dog sunscreen: If Mabel has any areas with thinner fur, a pet-safe sunscreen can protect her skin during long beach days.\n\nThese are just a few suggestions based on what I can see from the lovely photo you've shared. If you'd like more specific recommendations or have any questions about these products, please don't hesitate to ask. We're always here to help ensure Mabel has everything she needs for her beach adventures!\n\nIs there anything else you'd like to know about our pet products or services? Feel free to reply to this email or call us at 1-800-PET-LOVE for more personalized assistance.\n\nBest regards,\n[Your Name]\nCustomer Service Representative",
"timestamp": "2025-09-11T05:57:24.019Z",
"handler": "inquiry-handler"
}

Triggered event

The inquiry handler triggers an event to send an email reply via SendGrid.

Based on the JSON in the above step, here is what was sent:

Email from a company providing dog product recommendations for beach activities.

Pretty awesome! The AI response seemed to do a great job recommending products to a user. Now, let’s try handling something a little more difficult, and reach out with a problem.

Example 2: Open a support ticket

Let’s say that you finally saved up to purchase an action figure that you’ve always wanted. But when it arrived it was broken!

Let’s test the setup by emailing and asking for help with a broken action figure.

Initial email

Craft a message to support stating the issue you experienced. Like with our first example, let’s test the system by again sending an attachment to the AI.

Here is my email to support you can copy:

I need help! I purchased this from you guys and when it arrived it was broken! What should I do? Photo below…

Headless action figure in blue and red suit inside a brown box next to a detached head.

Again, send this email to the domain you configured with Inbound Parse. This email will go to SendGrid, and be routed to your Inbound Parse webhook. The security signature will then be checked, and the email contents stored (like with our product inquiry email), then summarized and routed. It should ultimately end up at the specialized Support Handler where next steps are determined and executed.

Reply back from Support

Screenshot of an email response from customer support regarding a damaged superhero action figure.

The steps here are similar to the LLM response and Triggered event step above, except you can see they were handled with a different route.

Notice that the photo attachment was analyzed so the support handler was able to pass context about the contents of the photo – it knew that it contained a broken action figure.

In addition, the other red arrow shows how the support handler injected a ticket number in the reply. Let’s exercise the workflow and reply to this message and build the support case.

Reply back to the GenAI support response

You want to be sure that you do not have to pay for any of this… right? Send an email back clarifying how the replacement shipment will be handled.

To copy my message, here is what I wrote:

Ok – will you pay for everything? I want to be sure I don’t have to pay any more for this item. I was really looking forward to having this today.

Reply, and let’s trace what happens next.

Screenshot of an email draft asking for payment confirmation and expressing eagerness for the item's arrival.

Receive a threaded response back from support

If everything went right, the support handler will get this reply because it noticed the ticket number in the subject line. The full context is used for the reply back.

Email detailing support response for a product replacement request.

This example introduced a method for tracking email threads by using a ticket number in the subject line. Here are a few ways you can ensure continuity of messages:

Use a Reference Number in the Subject Line: This method is relatively simple (and this example shows how well it works!). It supports additional people to be added via cc. However, the ticket number in the subject line is visible to the customer and it could be overwritten, which means a production solution will need to be coded defensively.

Using a Dynamic Reply-To: Instead of injecting an ID into the subject line, some of our customers choose to set the REPLY-TO address in their email to something that includes a unique ID. For example, support_12345ABCD@your-domain.com. This is easy to implement and scalable, and more robust to being overwritten – if a user changes the address, the email won’t be delivered, and they should double check the address

Using References in the HTML Content: IDs can be “hidden” in the body of the message and retrieved via text search. This is likely less reliable (consider that a user might “Select all” and delete) but could be used as an additional method of tagging an email to a specific ID.

Internally Tracking: Some of our customers build their own systems to track email threads by using TO, FROM, SUBJECT, and TIMESTAMP to generate a unique key. How you implement this will depend on your business requirements and how you and your customers interact over email.

Use multiple methods: Many of our customers will use more than one of the methods listed above to ensure.

Other examples

We have covered a general inquiry and a support use case. This repository also has 3 other handler types.

  • Sales → Will be triggered if the inbound message is related to purchasing.
  • Account → Will be triggered if the inbound message wants to make changes to their account.
  • Conversation → This use case just shows how it is possible to have a conversation over email. The conversation messages are stored in S3 to persist the chat history.

These use cases are basic and intended to give you any idea of what you could build! You should be able to see how you could customize all parts of the process to meet your specific business needs.

Deploy to production

While you can get this system working pretty quickly, it is not ready for your production environment. This blog post and repo is intended to inspire and help you start building awesome applications.

Conclusion

In the two steps of this tutorial, you’ve seen how to receive emails (and attachments) with SendGrid’s Inbound Parse, then send Generative AI-driven responses using AWS Bedrock. You can now build the Agentic Email experience of your – and your customers’ – dreams.

You now have a plan to help you start building! Let us know what you build so we can try it out.


Dan Bartlett has been building web applications since the first dotcom wave. The core principles from those days remain the same but these days you can build cooler things faster. He can be reached at dbartlett [at] twilio.com.