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 meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Send Email with Nodemailer

Last accessed pages

  1. CSS Trapezoid Shape (11313)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (74439)
  3. Node.js Move and Copy file (28290)
  4. AJAX with POST and PHP (18849)
  5. JQZoom Image Magnifier (12975)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide