Skip to main content

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:

ParameterTypeRequiredDescription
tostringYesRecipient email address
subjectstringYesEmail subject line
contentstringYesEmail body content
contentTypestringNoContent type (default: "text/plain")

Returns: { status: number, body: string }

Content Types:

TypeDescription
text/plainPlain text email (default)
text/htmlHTML formatted email

Environment Variables

VariableRequiredDescription
SENDGRID_API_KEYYesSendGrid API key
SENDGRID_FROM_EMAILYesSender 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_KEY is not set
  • SENDGRID_FROM_EMAIL is 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);
}