Nodejs Course

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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Send Email with Nodemailer

Last accessed pages

  1. Blade Templates - Control Structures (725)
  2. Align DIVs on the same line (8470)
  3. Shape Tween - Flash Animation (6153)
  4. Classic Tween - Flash Animation (6146)
  5. The Stage, Panels and Tools in Flash (10359)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (583)
  2. The Mastery of Love (92)
  3. CSS cursor property - Custom Cursors (92)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (68)
  5. Read Excel file data in PHP - PhpExcelReader (54)