Logo

dev-resources.site

for different kinds of informations.

Send emails using Nodemailer (Typescript)

Published at
12/27/2024
Categories
node
express
typescript
nodemailer
Author
veektor_v
Author
9 person written this
veektor_v
open
Send emails using Nodemailer (Typescript)

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:

  1. VSCode
  2. NodeJS

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

After installing typescript, run the following to initialize typescript on your project:

tsc --init
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Also install the type declaration for nodemailer.

npm i --save-dev @types/nodemailer
Enter fullscreen mode Exit fullscreen mode

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");
})();

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The next step is to execute the index.ts file by running the command:

ts-node index.ts
Enter fullscreen mode Exit fullscreen mode

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.

nodemailer Article's
30 articles in total
Favicon
Brevo smtp emails to other gmail accounts silently failing , verified domain to the rescue
Favicon
Send emails using Nodemailer (Typescript)
Favicon
Simple Emails Sending from Node.js Using Nodemailer and SMTP
Favicon
Practical Guide to Send Emails from NodeJS/Express App using Gmail and Nodemailer (Screenshots and Code)
Favicon
Sending Emails in NextJs via Nodemailer
Favicon
Sending Emails in Node.js Using Nodemailer
Favicon
Sending e-mails with Sendgrid
Favicon
NestJS Emails with react-email and nodemailer
Favicon
nodeMailer after google security update
Favicon
Beginner’s Guide On Sending Automated Emails With Node.js, Nodemailer, and Cron Jobs
Favicon
Tracking Email Activity from AWS Simple Email Service (SES)
Favicon
Envio de email com NodeJS e Gmail
Favicon
Setting up Node.js Email Server with Nodemailer and Mailtrap
Favicon
Sending Emails from a Nodejs Application using Nodemailer
Favicon
Dynamic emails with handlebars and nodemailer
Favicon
Sending e-mails with Mailtrap
Favicon
Simple Next.JS Form to Email Using React-Hook-Form and Gmail
Favicon
How to send Email with NodeJS in 2022
Favicon
How to send email attachments using nodemailer
Favicon
Send emails from your website to any user super easily!
Favicon
How to Send an Email with Nodemailer
Favicon
Send email using next.js, react-hook-form, tailwindcss & nodemailer
Favicon
Nextjs - Nodemailer - React Hook Form - Tailwindcss
Favicon
How to send mail using Nodemailer?
Favicon
I created my own email server to send emails into my gmail for My Portfolio
Favicon
How to send mail using nodemailer in Angular 11?
Favicon
How I met your...Scraper?
Favicon
Отправка писем в NestJS используя nodemailer. Публикация скриптов.
Favicon
3 ways to send emails with only few lines of code and Gmail - Javascript - Part 1
Favicon
NodeJS – Send email by Nodemailer

Featured ones: