Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 23, 2022 03:50 am GMT

How to send email attachments using nodemailer

Nodemailer is a popular library in Node.js to send emails.

Features of nodemailer are

  • It has zero dependencies
  • Secure email delivery with SSL/TLS support
  • Supports email transport via popular providers like Amazon SES, Sendgrid etc.
  • Easy to add attachments unlike other libraries.
  • In this tutorial well learn how to send email attachments using nodemailer.

1. First, add nodemailer to our project.
npm i nodemailer

2. Now lets create a transport

Like I mentioned above, nodemailer supports other thirdparty providers to be used as transport like Amazon SES, Sendgrid, Mailgun, SMTP etc. In this example, well use SMTP transport.

Ill be using Typescript for this example.

import nodemailer from "nodemailer";const smtpTransport = nodemailer.createTransport({  host: "smtp.example.com",  port: 587,  secure: false,  auth: {    user: "username",    pass: "password",  },});

3. Construct the email body

We can create the email body as an object with from, to, subject and html.

const mailBody = {  from: "[email protected]",  to: "[email protected]",  subject: "Sample email sent using nodemailer",  html: "<h1>Hello World!</h1>"};

4. Lets add our attachment

Nodemailer supports different types of attachments like base64, file path, stream etc. To get the complete list of attachment supported by nodemailer, refer their documentation.

In this example, well be using file path. So, lets modify our mailBody.

const mailBody = {  from: "[email protected]",  to: "[email protected]",  subject: "Sample email sent using nodemailer",  html: "<h1>Hello World!</h1>"  attachments: [        {            filename: 'sample_attachment.png',            path: __dirname + '/sample_attachment.png',        }    ]};

5. Send the email

To send the email, use the transport.sendMail() function.

smtpTransport.sendMail(mailBody, function (error, info) {   if (error) {      console.log(error);   }   console.log("Email with attachment delivered successfully")});

Final code to send email attachments using Nodemailer.

import nodemailer from "nodemailer";const smtpTransport = nodemailer.createTransport({  host: "smtp.example.com",  port: 587,  secure: false,  auth: {    user: "username",    pass: "password",  },});const mailBody = {  from: "[email protected]",  to: "[email protected]",  subject: "Sample email sent using nodemailer",  html: "<h1>Hello World!</h1>"  attachments: [        {            filename: 'sample_attachment.png',            path: __dirname + '/sample_attachment.png',        }    ]};smtpTransport.sendMail(mailBody, function (error, info) {    if (error) {        console.log(error);    }    console.log("Email with attachment delivered successfully")});

Simple! Hope this tutorial has helped you to understand how to send attachments using nodemailer in your email notifications.


Original Link: https://dev.to/anandrmedia/how-to-send-email-attachments-using-nodemailer-2n5e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To