Sending Mails with Sendgrid and Node.js
Tags: sendgrid, node, express, programming
Published: 2019-12-18 11:13:46

---
title: Sending Mails with Sendgrid and Node.js
published: true
date: 2018-09-20 20:09:27 UTC
tags: sendgrid,node,expressjs,programming
canonical_url: 
---

Essentially in every web application, there's a need to send the user an email. In this article, I would love to show you how easy it is to setup Sendgrid and send emails easily to users. We will set up an easy module that will handle our mail sending.

![Image result for nodejs](https://cdn-images-1.medium.com/proxy/1*-8kIoJKyDn-j1WPgfTvmew.jpeg)

#### Installing SendGrid

Firstly, we need the official package for sending emails. Run the following command :

```
npm install @sendgrid/mail
```

Now we have the Sendgrid node package for the sending emails in your node application.

To use this package you need an API key which you will get [here](https://app.sendgrid.com/settings/api_keys) after you must have created an account.

#### Configuring Sendgrid

In my application, I like to create the email sending module in a separate file in a utils folder and name it emails.js

```
// emails.js

const SGmail = require('@sendgrid/mail')

SGmail.setApikey('xxxxx-xxxxx') // Input Api key or add to environment config
```

> I strongly advise you don’t hardcode your API key, use an environment variable instead, check out this awesome [post](https://medium.freecodecamp.org/heres-how-you-can-actually-use-node-environment-variables-8fdf98f53a0a) to get started if you don’t know how to use config vars in node.js

#### Mail Sending

Let’s create a simple function to send the email

```javascript
//emails.js

function newUserEmail(email, name){

const message = {

to : email, //email variable

from : { email : 'your email' , name: 'Name of user you want to send email as'},

message : `Hi there, ${name}`,

subject : "This is a test Email"

}

SGmail.send(message).then((sent) =\> {

// Awesome Logic to check if mail was sent

})

}

module.exports = {

newUserEmail
}
```

Let me explain each of the keys and values in the message object created above

⦁ to: The recipient’s email.

⦁ from: the from object contains the email key which would be used as the sender email and name which would be used as the sender name.

⦁ message: which is the content of your email. You can also use HTML tags, in which case the key wouldn’t be message, but html instead.

⦁ subject: The email subject.

#### Conclusion

I am sure you see how easy it is to send emails with Sendgrid. This can save a lot of development time and debugging time with minimal configurations. Now you can import the function anywhere in your code and send a mail, easy!

> Reach out to me via swithdevelop1@gmail.com and give me a follow on [twitter](https://twitter.com/feezyhendrix).

[![](https://cdn-images-1.medium.com/max/1024/1*i3hPOj27LTt0ZPn5TQuhZg.png)](http://bit.ly/codeburst)

> ✉️ _Subscribe to_ CodeBurst’s _once-weekly_ [**_Email Blast_**](http://bit.ly/codeburst-email) **_,_** 🐦 _Follow_ CodeBurst _on_ [**_Twitter_**](http://bit.ly/codeburst-twitter)_, view_ 🗺️ [**_The 2018 Web Developer Roadmap_**](http://bit.ly/2018-web-dev-roadmap)_, and_ 🕸️ [**_Learn Full Stack Web Development_**](http://bit.ly/learn-web-dev-codeburst)_._

* * *