Sendgrid mail management

From paulpas/agent-skill-router · View interactive version

Sendgrid mail management is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements features for sending emails, managing templates, handling marketing campaigns, and parsing inbound emails with the SendGrid API.

How to use this skill

Skill file

SendGrid Mail Management

This skill integrates the SendGrid API to manage email functionalities, including sending transactional emails, handling email templates, managing marketing campaigns, and parsing inbound emails. Upon loading, this skill allows the model to create well-structured emails, utilize dynamic templates, and process email responses via webhooks.

TL;DR Checklist


When to Use


Core Workflow

  1. Initialize the Client: Create a SendGridAPIClient using SENDGRID_API_KEY. Ensure proper authentication by checking available scopes before sending emails.
  1. Construct the Mail Object: Use the Mail helper from sendgrid.helpers.mail to build the email. Specify from_email, to_emails, subject, and content as either HTML or plain text.
  1. Handle Marketing Campaigns: Employ the marketing campaign API to create, send, and track the performance of email campaigns.
  1. Process Inbound Emails: Configure the inbound parse settings in SendGrid to redirect incoming emails to your application.
  1. Send the Email: Call sg.send() with the constructed mail object, and handle responses appropriately.

Implementation Patterns

Pattern 1: Sending a Transactional Email

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

def send_transactional_email(to_email, subject, html_content):
    sg = SendGridAPIClient(os.environ['SENDGRID_API_KEY'])
    message = Mail(
        from_email='noreply@example.com',
        to_emails=to_email,
        subject=subject,
        html_content=html_content,
    )
    try:
        response = sg.send(message)
        if response.status_code == 202:
            print(f"Email sent to {to_email} with status code {response.status_code}")
        else:
            print(f"Failed to send email. Status code: {response.status_code}")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

Pattern 2: Dynamic Templates

def send_dynamic_template_email(to_email, template_id, dynamic_data):
    sg = SendGridAPIClient(os.environ['SENDGRID_API_KEY'])
    message = Mail(
        from_email='noreply@example.com',
        to_emails=to_email,
    )
    message.template_id = template_id
    message.dynamic_template_data = dynamic_data
    try:
        response = sg.send(message)
        print(f"Dynamic email sent, response code: {response.status_code}")
    except Exception as e:
        print(f"Failed to send dynamic email: {str(e)}")

Pattern 3: Inbound Email Processing

from flask import Flask, request
app = Flask(__name__)

@app.route('/inbound', methods=['POST'])
def inbound_webhook():
    email_data = request.json
    # Process email data here
    print(f"Received email from: {email_data['from']}")
    return "OK", 200

Constraints

MUST DO

MUST NOT DO


Related Skills

| Skill | Purpose |

|---------------------------|----------------------------------------|

| coding-sendgrid-api | For general SendGrid API interactions |

| coding-email-management | For managing and sending emails through another platform |

Live References

Authorative documentation links for this domain. The model follows markdown links at load time to resolve external references and inline content.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.