SendGrid
Using Sendgrid for sending Transactional Emails
Sign up for SendGrid
Account Creation: Sign up for a free SendGrid account to send up to 100 emails daily.
Enable Two-Factor Authentication (2FA): Secure your account with 2FA via SMS or Authy app.
Create API Key: Generate a SendGrid API key for limited actions.
Use "Restricted Access" with "Mail Send" permissions set to "Full Access".
Store the API key securely in an environment variable (e.g.,
SENDGRID_API_KEY
).
- Verify Your Sender Identity
Ensure legitimate sending behavior: Links for reference. https://docs.sendgrid.com/for-developers/sending-email/sender-identity https://docs.sendgrid.com/ui/account-and-settings/how-to-set-up-domain-authentication
Here's a sample code block for creating( sending ) a message:
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = { to: 'test@example.com', // Change to your recipient from: 'test@example.com', // Change to your verified sender subject: 'Sending with SendGrid is Fun', text: 'and easy to do anywhere, even with Node.js', html: '
and easy to do anywhere, even with Node.js
', };
sgMail .send(msg) .then((response) => { console.log(response[0].statusCode); console.log(response[0].headers); }) .catch((error) => { console.error(error); });
Run the index.js
file with Node.js. If you see a 202 status code printed, the message was sent successfully. Check the recipient's inbox for the demo message. If not received, check the spam folder.
For troubleshooting, refer to the API response message documentation. Responses are in JSON format, specified by the Content-Type header. The Web API v3 provides response codes, content-type headers, and pagination options for interpreting API responses.
Here are some Email Formats
Welcoming to our app
{ "to": "recipient@example.com", "from": "sender@example.com", "subject": "Welcome to Our App!", "text": "Hello there! Welcome to our mobile app. We're excited to have you on board.", "html": "Hello there!Welcome to our mobile app. We're excited to have you on board." }
Important Announcement
Informing about Recent Purchase
{ "to": "customer@example.com", "from": "support@example.com", "subject": "Your Recent Purchase", "text": "Hi there! We wanted to confirm that your recent purchase has been successfully processed. Thank you for choosing our service.", "html": "Hi there!We wanted to confirm that your recent purchase has been successfully processed. Thank you for choosing our service." }
// Password Reset Request { "to": "user456@example.com", "from": "noreply@example.com", "subject": "Password Reset Request", "text": "Dear User, we received a request to reset your password. If you did not initiate this request, please ignore this email. To reset your password, click on the following link: [Password Reset Link]", "html": "
Dear User,
We received a request to reset your password. If you did not initiate this request, please ignore this email.
To reset your password, click on the following link: <a href="[Password Reset Link]">Reset Password
" }
Last updated