Integrate Bolt.new AI with SendGrid in minutes using our step-by-step guide. Learn essential tips and best practices for seamless email automation.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
{
"dependencies": {
"@sendgrid/mail": "^7.7.0",
// ... any other dependencies
}
}
sendgridClient.ts
in the root directory of your Bolt.new AI project. This file will serve as a utility to configure SendGrid and send emails.sendgridClient.ts
:
// Import the SendGrid mail package
import sgMail from '@sendgrid/mail';
// Set your SendGrid API Key from environment variables
// Make sure to add your SENDGRIDAPIKEY in your Bolt.new project secrets
sgMail.setApiKey(process.env.SENDGRIDAPIKEY as string);
/**
- Function to send an email using SendGrid
- @param to Recipient email address
- @param subject Email subject line
- @param text Plain text content of the email
- @param html HTML content of the email
*/
export function sendEmail(to: string, subject: string, text: string, html: string) {
const msg = {
to,
from: process.env.SENDER_EMAIL as string, // Your verified sender email from SendGrid
subject,
text,
html,
};
return sgMail.send(msg);
}
SENDGRIDAPIKEY
: Your SendGrid API keySENDER_EMAIL
: The email address you have verified with SendGrid
main.ts
), import the sendEmail
function from your new utility file.
import { sendEmail } from './sendgridClient';
// Example function that sends an email when triggered
function notifyUser() {
const recipient = '[email protected]';
const subject = 'Welcome to Bolt.new AI!';
const textContent = 'Thank you for signing up.';
const htmlContent = 'Thank you for signing up.';
sendEmail(recipient, subject, textContent, htmlContent)
.then(() => {
console.log('Email sent successfully.');
})
.catch((error) => {
console.error('Error sending email:', error);
});
}
// Call the function where appropriate in your application workflow
notifyUser();
sendEmail
function and invoking it as needed.SENDGRIDAPIKEY
and SENDER_EMAIL
) are properly set in your project's environment variables or secrets settings.notifyUser()
function will send an email using SendGrid.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.