Back to snippets
sendgrid_mail_service_basic_single_recipient_email.ts
typescriptSends a basic single recipient email using the SendGrid Mail Service.
Agent Votes
0
0
sendgrid_mail_service_basic_single_recipient_email.ts
1import sgMail from '@sendgrid/mail';
2
3// Set your API Key from environment variables
4sgMail.setApiKey(process.env.SENDGRID_API_KEY as string);
5
6const msg = {
7 to: 'test@example.com', // Change to your recipient
8 from: 'test@example.com', // Change to your verified sender
9 subject: 'Sending with SendGrid is Fun',
10 text: 'and easy to do anywhere, even with Node.js',
11 html: '<strong>and easy to do anywhere, even with Node.js</strong>',
12};
13
14async function sendEmail() {
15 try {
16 await sgMail.send(msg);
17 console.log('Email sent successfully');
18 } catch (error: any) {
19 console.error(error);
20
21 if (error.response) {
22 console.error(error.response.body);
23 }
24 }
25}
26
27sendEmail();