-
Sending Email with Attachments
To send email with Node.js, you can use the Nodemailer module, it makes it easy to send emails from your computer.
You need an account to an email provider, for example GMail which is free with a limit of maximum 500 emails per day.
Sending email with Nodemailer module
1. First, install the Nodemailer module with NPM; in command prompt interface:
npm install --save nodemailer
- After you have downloaded the Nodemailer module, you can include the module in any application:
const nodemailer = require('nodemailer');
2. Now, to send emails from your Node.js server, write a code that uses the
username and password from your selected email provider.
Here is a simple script to send email with GMail.
- Copy and save the following code in a "
sendmail.js" file in the folder with your Node.js project:
const nodemailer = require('nodemailer');
const trans = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'password'
}
});
let mail_op ={
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
html: 'That was easy!'
};
trans.sendMail(mail_op, (err, info)=>{
if(err){
console.log(err);
} else {
console.log('Email sent: ' + info.response);
}
});
- To send message with html tags, replace the "text" property with "html" in email options:
let mail_op ={
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
html: '<h1>Welcome</h1><p>That was easy!</p>'
}
And that's it! Now, run the "sendmail.js" file in command line interface, and your server should send the email via GMail.
node test/sendmail.js
Using SMTP
You can also use SMTP server to send the email:
const trans = nodemailer.createTransport({
host: 'smtp.gmail.com', // hostname
port: 465, // secure:true for port 465, secure:false for port 587
secure: true, // port for secure SMTP
auth: {
user: 'youremail@gmail.com',
pass: 'password'
}
});
Sending Email to Multiple Receivers
To send an email to more than one receiver, add them to the "
to" property of the mail options object (mail_op), separated by a comma:
let mail_op ={
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com, myotherfriend@yahoo.com',
subject: 'Sending Email using Node.js',
html: '<h3>The life is Happiness.</h3>'
}
- Also, you can add the "cc", and "bcc" properties with comma separated list of recipients email addresses that will appear on the
Cc, respectively
Bcc fields.
Sending Email with Attachments
To add one or more attachments to your email, add an "
attachments" property in the message object, that contains an array of attachment objects.
You can add an utf-8 string as an attachment, a stream, a local file, or even a file from an URL address. For more details, see the documentation from:
Nodemailer Attachments.
- Here is a code example for sending email with Nodemailer, containing two attachments: a local file (file.zip), and a PDF file from an URL address:
const nodemailer = require('nodemailer');
const trans = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'password'
}
});
let mail_op ={
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'You are Blessed with Miracles',
html: '<h1>Welcome</h1><p>Have a Happy Life with everyone and with yourself.</p>',
attachments: [
{ //using a local file
path: __dirname+'/file.zip'
},
{ //using URL as an attachment
filename: 'art_of_believing.pdf',
path: 'https://coursesweb.net/blog/dwl/prayer_the_art_of_believing.pdf'
},
]
};
trans.sendMail(mail_op, (err, info)=>{
if(err){
console.log(err);
} else {
console.log('Email sent: ' + info.response);
}
});