- 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'
  }
});
If you get an error like this:
Invalid login: username and password not accepted
...
responseCode: 535,
command: 'AUTH PLAIN'
Login in to: //www.google.com/settings/security/lesssecureapps and TURN ON "Access for less secure apps".

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: 'http://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);
  }
});

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Send Email with Nodemailer

Last accessed pages

  1. Ajax-PHP Chat Script (31506)
  2. Qwop (30373)
  3. CSS Course - Free lessons (7754)
  4. JavaScript trim, rtrim and ltrim (3884)
  5. Zuma Deluxe (36444)

Popular pages this month

  1. Qwop (4647)
  2. Bubbles3 (3751)
  3. Butterfly Kyodai (3119)
  4. Drag Racer V3 (3078)
  5. Zuma Deluxe (2225)