Why would you want to send emails via code?
I was recently working on a project where there was a requirement to send an email to a user when an update had been made to their account. I reviewed a number of different options but I found Sendgrid to be super easy to setup and the super generous free tier was suitable for the project. If you need to send an email in code, let me show you how to do that.
When would I want to send an email?
For me this email was to be sent when an update had been made to a users account access. This would need a link to verify an email address and also encourage them to login because they had now been granted access.
You might want to send an email at a different stage? Perhaps when a user has logged in for the first time or perhaps as a notification when something happens in your app/ website?
So how do we get started?
First let's set up a Sendgrid account.
Sendgrid has a free tier that enables 100 free emails a day. So let's sign up at https://sendgrid.com/.
Then in your code, install @sendgrid/mail
with npm i @sendgrid/mail
then include the package in a index.js.
Also make sure you have Node js installed on your computer.
Generate a SendGrid API key
To generate an API key, start by logging into your account and scroll to the settings on the right hand side (see above screenshot). And then click on the blue "Create API key" button in the top left hand corner. Give the API key a name and set the permissions to Full access for now (You can customise later) .
You will then be shown a screen with the API key (This will only be shown once).
Now in an .env file add export SENDGRID_API_KEY=<Replace with Your API Key>
Now we are setup how do I send an email?
First import the send grid package into your file then create a function that sets the api key, the HTML message and the message object that will be sent.
const sgMail = require("@sendgrid/mail");
const sendEmail = () => {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const HTMLmessage = `<h1>Test Email</h1>`
const msg = {
to: receiverEmailAddress, // Change to your recipient
from: "Your test Email <accounts@test.co.uk>", // Change to your outgoing email
subject: "This is a test email",
html: HTMLmessage,
};
await sgMail.send(msg);
}
But what if I want to use a custom domain for my FROM email address?
This is the great thing about Sendgrid, you don't need to setup an email server. You can just add some config to your Nameserver for your domain and send emails from any email with your domain name!!
Where do I get a Domain from?
You can buy domain names from many places, but one I recommend and have been using for the last couple of years is Namecheap.com
Sign up to Namecheap.com to get a domain name.
Send emails from my domain name
Under the Sendgrid settings there is an option called "Sender Authentication". Navigate to that page and begin authenticating your domain.
In Namecheap.com buy a domain and then navigate to Advanced DNS section.
Add a new record (Needs to be a Cname record) by adding the first value in the host as the Authenticating section on Send grid suggests.
Once you are setup you can then run your index.js by adding sendEmail()
at the end of the file and running node index.js
to send the email to yourself. (Set the email address in the msg object)
And we have lift off!
Already got a domain name? You can get 50% off a top domain transfers by using promo code TLD23TSAPR and to get up to 62% off shared hosting use HOST23TSAPR
Extra resources:
Quickstart to send emails using Nodejs
Top comments (0)