Hey, fams! today we are going to learn how to send e-mails, right from our IDE using NodeJS. The module of interest is called Nodemailer.
Prerequisites
π NodeJs
π Nodemailer
π Email account
π― Steps
Open editor (VSCode π), initialize your project with the command below
npm init -y
This command initiates a package.json
, package.json.lock
, and index.js
(main entry file). The index.js
will house all our logic.
Dependencies
πInstall Nodemailer
npm i nodemailer
π Import the package inside index.js
const nodemailer = require('nodemailer');
π¨π½βπ« For security reasons, make sure you install and use dot.env package to prevent your password from being exposed or pushed to GitHub.
Install dotenv
npm i dotenv -S
Require dotenv in your index.js
file. I didn't require it in this project because I am using dummy data.
require('dotenv').config();
Then, create a .env
file your email and password
Email= ***********@gmail.com
Password= ******
Logic
π― Your auth logic in index.js
with dotenv
// Gmail account info
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
π― Your auth logic in index.js
without dotenv
. Write the logic below and of course change the email to your own and the password to yours too.
// Gmail account info
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'dsimple@gmail.com',
pass: 'ilovemymama'
}
});
π― Next use the mailOption to send your message.
// Email info
const mailOptions = {
from: 'dsimple@gmail.com',
to: 'fams@gmail.com',
subject: 'How to send emails using NodeJS',
text: 'Follow the instructions and you will be fine'
};
π― Lastly, write:
// Send email π§ and retrieve server response
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
When done properly, you should have the following logic in your index.js
. That is if you choose not to use the dotenv
To run: type ππΌ in your terminal
node index
Note: On your Gmail, do not forget to accept and allow the "Less secure apps" access to use your scripts with your Gmail SMTP connection. Gmail will alert you with an error if this option is off, you need to turn it on.
Multiple emails, CC and BCC
const mailOptions = {
from: 'dsimple@gmail.com',
to: 'fams@gmail.com,myrealfams@gmail.com',
cc: 'lexus@gmail.com',
bcc: 'sugar@gmail.com',
subject: 'How to send emails using NodeJS',
text: 'Follow the instructions and you will be fine'
};
Send attachment
const mailOptions = {
from: 'dsimple@gmail.com',
to: 'fams@gmail.com,myrealfams@gmail.com',
cc: 'lexus@gmail.com',
bcc: 'sugar@gmail.com',
subject: 'How to send emails using NodeJS',
text: 'Follow the instructions and you will be fine',
attachments: [{
filename: "robocop.jpg", path: "./img/robocop.jpg"}]
};
drsimplegraffiti / drsimplegraffiti
Config files for my GitHub profile.
Hi π, I'm Abayomi.
A Software Engineer interested in Backend
Software Engineer
-
π I regularly write articles on https://dev.to/drsimplegraffiti
-
π My Portfolio Click Here
-
π« How to reach me abayomiogunnusi@gmail.com
-
π Know about my experiences https://www.linkedin.com/in/abayomi-ogunnusi-974826141/
Languages and Tools:
Dev.to Post
π Improve your Github Profile
π Download Browser Page as PDF
π Expose a local web server to the internet
π Web scraping using Node Js
π Bash Terminal Guide
π Best Practices: Node JS Security
π Postman Hacks
π Time Zone: Nodejs
Discuss
What other email π¬ services can you use apart from Gmail without toggling off the Less Secure App setting?
Top comments (20)
Thank you so much.
But I am facing difficulty in attachment sending part, I have tried 3 times but couldn't send only text sent!
@Suchitra try adding an 's' to attachment:
attachments: [{
I ran into the same thing with just text showing up and checked the nodemailer documentation on attachements. That little mispelling fix should do the trick.
Also, if you want to learn how to do this with a React frontend and Google OAuth2, check out my nodemailer article as well :)
Thank you so much
It's working fine now:)
@Suchitra
Do you have a folder where your image is stored?
Example:
attachment: [{
filename: "robocop.jpg", path: "./img/robocop.jpg"}]
};
I kept image file in same folder where my index.js is present.
So in this case I just wrote:
attachment: [{
filename: "robocop.jpg", path: "robocop.jpg"}]
};
But it is not working:(
shouldn't it be
@xchavez94x i have not used sendgrid before but I will look into it too...thanks for your contribution
Good content...Thanks
@brandonwallace Thanks
Nice article! Thanks for posting it.
@Reaper thanks for the contribution
Or you could use mailer.reaper.im to avoid setting this up in your code and instead let it do all the lifting.
Parabens pelo conteΓΊdo.
obrigado muito apreciado
Kudos for teaching dotenv at the very beginning! I often see people pushing sensitive data into a public repo just because they treated environment security as an afterthought.
@njokdan You are welcome π