SendGrid Email
The SendGrid script module provides email sending capabilities using the SendGrid API.
Functions
sendEmailViaSendgrid
Send an email through the SendGrid API.
await sendEmailViaSendgrid(to, subject, content, contentType?)
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
to | string | Yes | Recipient email address |
subject | string | Yes | Email subject line |
content | string | Yes | Email body content |
contentType | string | No | Content type (default: "text/plain") |
Returns: { status: number, body: string }
Content Types:
| Type | Description |
|---|---|
text/plain | Plain text email (default) |
text/html | HTML formatted email |
Environment Variables
| Variable | Required | Description |
|---|---|---|
SENDGRID_API_KEY | Yes | SendGrid API key |
SENDGRID_FROM_EMAIL | Yes | Sender email address (must be verified in SendGrid) |
Examples
Send a plain text email
await sendEmailViaSendgrid(
'user@example.com',
'Welcome to RocketWave!',
'Thank you for signing up. Your account is now active.'
);
Send an HTML email
const html = `
<h1>Welcome!</h1>
<p>Your account has been activated.</p>
<a href="https://console.rocketwavelabs.io">Get Started</a>
`;
await sendEmailViaSendgrid(
'user@example.com',
'Welcome to RocketWave!',
html,
'text/html'
);
Send email with AI-generated content
// After a Prompt entity generates content
const content = await latestPromptResponse();
await sendEmailViaSendgrid(
'team@example.com',
'Daily AI Summary',
content
);
print('Summary email sent');
Error Handling
The function throws an error if:
SENDGRID_API_KEYis not setSENDGRID_FROM_EMAILis not set- Required parameters (to, subject, content) are missing
- The SendGrid API returns a non-200 response
try {
await sendEmailViaSendgrid('user@example.com', 'Test', 'Hello');
print('Email sent successfully');
} catch (error) {
print('Email failed:', error.message);
}
Related Topics
- Scripts Overview — All available script functions
- Variables — Configure SendGrid credentials