Thursday 20 April 2017

Problems setting up Nodemailer on NodeJS

I am trying to set up Nodemailer for a simple website that is built on the Node JS framework. The site has one simple index.html file in which I have simple form element with name, email, and message inputs. I have already downloaded nodemailer to my node_modules directory using $nmp install nodemailer. Following the instructions at the above hyperlink, the next step is to create a transport object using SMTP or another transport mechanism. However, I am not sure where to create this transport object. Do I create a new file? Under what directory? What kind of file (I assume a .js file). Or do I place the object in an existing file? It then says to simply set up the message options in the object and call the message object using the sendMail() method. Again, I am not sure where to use the sendMail() method. Do I just scrap my html form and call the method in <script> or from a .js file somewhere. Below is my app directory tree, followed by the sample transport object code from the nodemaile instructions. Can anyone provide guidance on how to set this up? Note: I am using node v6.10.

enter image description here

'use strict';
const nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'gmail.user@gmail.com',
    pass: 'yourpass'
  }
});

// setup email data with unicode symbols
let mailOptions = {
  from: '"Fred Foo 👻" <foo@blurdybloop.com>', // sender address
  to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers
  subject: 'Hello ✔', // Subject line
  text: 'Hello world ?', // plain text body
  html: '<b>Hello world ?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('Message %s sent: %s', info.messageId, info.response);
});



via jcbridwe

No comments:

Post a Comment