Introduction
Have you wondered about how emails are sent from a piece of typescript file? It is possible to send emails using nodejs, typescript and nodemailer.
So, what is Nodemailer?
Nodemailer is simply a package or module that can be used to send emails via javascript/typescript.
Requirements
You will need to download and install the following:
How do we start sending emails using nodemailer?
Step 1: Create a folder
You'll navigate to your desktop (or an folder or location on your PC) and create a folder. I'm naming my folder "sendemail". After that, open the folder with VSCode.
Step 2: Initialize a package/project
On VSCode, open the terminal and run the following command command:
npm init -y
If you notice, a package.json file is created right? That package.json will be used to manage the packages/dependencies your project will use.
Step 3: Install and initialize typescript on your project
Next, you have to install typescript package globally if you've not installed it. You can install it by running the code:
npm install -g typescript
After installing typescript, run the following to initialize typescript on your project:
tsc --init
This will create a tsconfig.json file which you can use to configure how typescript should behave for your package/project.
Step 4: Install Nodemailer
After initializing the package, you'll then need to install nodemailer, you'll do so by running the command:
npm install nodemailer
Also install the type declaration for nodemailer.
npm i --save-dev @types/nodemailer
You're using the --save-dev flag because we won't be needing the type declaration dependency in our production enviroment in case you will want to deploy the script on a production server.
After running this command, if you check your package.json file you will notice that the dependencies object has "nodemailer" and the version of nodemailer you installed. You should also see "@types/nodemailer" in the dev dependencies object in the package.json file.
Step 4: Create an index.ts file
In your "sendemail" folder (where the package.json file is located) create a file named "index.ts"
There are platform like mailtrap or Google's gmail which can be used to send emails. For this example I'll be using Google's Gmail.
Head over to your gmail account (your gmail account needs to have 2 factor authentication switched on) and search "app password", then create an app password and use as the value of the pass property in the config below (the password should not contain space).
Make sure to keep your app password a secret, don't share it. Anyone else that knows your email and app password can use your account to send emails.
Inside the index.ts file, write the following:
import nodemailer from "nodemailer";
const transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "", // your email
pass: "" // the app password you generated, paste without spaces
},
secure: true,
port: 465
});
(async () => {
await transporter.sendMail({
from: "", // your email
to: "", // the email address you want to send an email to
subject: "", // The title or subject of the email
html: "" // I like sending my email as html, you can send \
// emails as html or as plain text
});
console.log("Email sent");
})();
Step 5: Execute the index.ts file
Before executing the node.ts file, install ts-node to enable you execute typesctipt files directly. Run the command:
npm install -g ts-node
The next step is to execute the index.ts file by running the command:
ts-node index.ts
Conclusion
There are lots of ways you can use the code. You can use this as a utility on your express.js project for sending emails, you can create html email templates that depend on the index.ts file for sending emails, etc. I hope with this article you have learnt about how to send emails using NodeJS, Typescript and Nodemailer.
Top comments (2)
Nice one bro.
Thanks a lot boss