jazzedge
7/28/2017 - 11:43 AM

Bot - Full Example: Sending email

Bot - Full Example: Sending email

//demo-email.js
// Demonstrates how to send an email from GMAIL to HOTMAIL.

//GMail security can cause a Nodemailer invalid login. 
// One reason could be the 'modern security standard' protection from Gmail.
//Check you gmail inbox for any new mail having subject "Google Account: sign-in attempt blocked"
//If yes, open the mail and click on the link https://www.google.com/settings/security/lesssecureapps
//set 'Access for less secure apps' to 'Turn on'. Try again, it should be working now.

//See: https://stackoverflow.com/questions/26948516/nodemailer-invalid-login


'use strict';
// 01. Include required files
require('dotenv-extended').load();
var restify = require('restify'); 
var builder = require('botbuilder');
var nodemailer = require('nodemailer');
// ---------------------------------------------------------------------------------------------------------------------
// 02. Setup Restify Server
var server = restify.createServer();

// 03. Configure listen for messages
server.listen(process.env.PORT || 3978, function() 
{
   console.log('%s listening to %s', server.name, server.url); 
});

// 04. Create chat bot
var connector = new builder.ChatConnector
 ({
     appId: process.env.MICROSOFT_APP_ID,   
     appPassword: process.env.MICROSOFT_APP_PASSWORD
 });

// 05. Listen for messages
server.post('/api/messages', connector.listen());

// 06. Serve static files
server.get(/.*/, restify.serveStatic({
    directory: __dirname,
    'default': 'index.html'
}));
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
    console.log(session);
    console.log("Channel:", session.message.address.channelId);
    var r = randomInt(1,10);
    console.log('Random n.' + r); 
    session.send ('Random n.'+ r); // Use the plus sign, not a comma.
    session.send ('Good ' + amPm());
    session.send("You said: %s", session.message.text);
    session.send("Session.message.address.user.id = %s", session.message.address.user.id);
    session.send("Session.message.address.user.name = %s", session.message.address.user.name);
    session.send("Sending email...");
    emailUser();
});

// ----------------------------------------------------------------------------------------------------
// Generate a random integer between low and high
function randomInt (low, high) {
    return Math.floor(Math.random() * (high - low) + low);
}
// ----------------------------------------------------------------------------------------------------
// Morning or afternoon
function amPm () {
    const date = new Date().getHours();
    console.log ('Date:', date);
    var period = 'evening';
    if (date < 12) {
        period = "morning";
    } else {
        if (date < 18){
            period = "afternoon";
        }
    }
    return period;
}

// ----------------------------------------------------------------------------------------------------
function emailUser () {
    // create reusable transporter object using SMTP transport
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: 'joe@gmail.com',
            pass: '...'
        }
    });

    // setup e-mail data with unicode symbols
    var mailOptions = {
    // sender address
            from: 'jazzedge24@gmail.com', 
    // list of receivers
            to: 'tazmoka@hotmail.com',  
    // Subject line
            subject: 'Testing test', 
    // plaintext body
            text: 'It works!',
    // rich text html body
            html: "<p>It works</p>",
    };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log(error);
        }else{
            console.log('Message sent: ' + info.response);
        }
    });

}