Adding a new module to Node.js

Here is a recipe to add a new module into Node.js. In this case, we'll use Nodemailer, a powerful and highly customizable API e-mail engine. We chose this module because we wanted to actually have the script do something interesting and not just spit out another onscreen print command. At the end of this recipe, you will be able to run a script that sends an e-mail to your inbox.

Getting ready

Open up LXTerminal. Alternatively, open up the Cloud9 IDE in the manner described in the previous section.

How to do it...

Create a directory for your projects using the following command:

$ mkdir projects

Perform the following steps after creating a directory for your project:

  1. Browse to this new directory and make another emailer directory using the following command:
    $ cd projects
    $ mkdir emailer
    
  2. Now, go to the new directory with the following command:
    $ cd emailer
    
  3. Although it's not mandatory, the following command is the proper first step to setting up your node environment:
    $ npm init
    

    You will see a series of prompts that you can fill out; you can skip them by hitting the return (Enter) key on your keyboard.

    What you will do with these prompts and inputs is create the metadata to populate a file called package.json. This file's purpose is to give information to npm, which allows it to identify the project as well as handle the project's dependencies.

  4. Next, we will use the following command to install the star of the current show: a node package called nodemailer:
    $ sudo npm install nodemailer --save
    
  5. Navigate to the new directory created by the installation and open up a nano window with a new filename, as described here:
    $ cd node_modules/nodemailer
    $ sudo nano nodemailer-test.js
    
  6. Copy and paste this code in the nano window:
    //This code has been modified from the nodemailer github example.
    
    var nodemailer = require('nodemailer');
    
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: '[email protected]',
            pass: 'user_password'
        }
    });
    
    // setup e-mail data with unicode symbols
    var mailOptions = {
        // sender address
        from: 'Ground Control <[email protected]>',
        // list of receivers
        to: 'sender_name1@some_domain.com, [email protected]',
        // Subject line
        subject: 'This is Ground Control to Major Tom',
        // plaintext body
        text: 'Can you hear me, Major Tom?',
         // html body
        html: '<b>Can you hear me Major Tom?</b>'
    };
    
    // send mail with defined transport object
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            return console.log(error);
        }
        console.log('Message sent: ' + info.response);
     
    });
    

    Note

    The preceding code is a modification of nodemailer's GitHub example. Also note: if you run into problems copying and pasting the code, you can download the code from https://github.com/HudsonWerks/Nodemailer/blob/master/examples/nodemailer-test.js.

    Finally, if you use Gmail for your account in this example and run into login errors, there are numerous troublehooting tips here: https://github.com/andris9/nodemailer-wellknown/issues/3.

  7. Save the new file using the keyboard command, Ctrl + x with nano, and then type Y for "yes".

    Then, press the return (Enter) key.

  8. Now, run the following command:
    $ node nodemailer-test.js
    
  9. Ta-da! If all went well, you should receive an e-mail from Ground Control in your inbox.

There's more…

See also

To explore this particular module, including nodemailer's wide variety of features and customization options, check out https://github.com/andris9/Nodemailer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.59.163