- 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 defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Send Email with Nodemailer

Last accessed pages

  1. Using file_put_contents, file_get_contents, readfile and file() (5388)
  2. HTML5 Quick Tutorial (3645)
  3. Create simple Website with PHP (15916)
  4. Mahjong Gardens (7308)
  5. Dolphin Olympics 2 (1830)

Popular pages this month

  1. Qwop (10555)
  2. Drag Racer V3 (6224)
  3. Flash Games - Free online Games (2162)
  4. List with All the Games (2073)
  5. Bubbles3 (1996)