From 30d26d82c30299c75d8972c30ee16ddd76d6a2c5 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sun, 16 Jul 2017 13:47:06 +0200 Subject: [PATCH] Added nodemailer to send email of the requested movie --- src/plex/requestRepository.js | 59 ++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/plex/requestRepository.js b/src/plex/requestRepository.js index a4f1872..a15a8d5 100644 --- a/src/plex/requestRepository.js +++ b/src/plex/requestRepository.js @@ -8,7 +8,11 @@ const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); var Promise = require('bluebird'); var rp = require('request-promise'); +const MailTemplate = require('src/plex/mailTemplate') + var pythonShell = require('python-shell'); +const nodemailer = require('nodemailer'); + class RequestRepository { @@ -60,24 +64,57 @@ class RequestRepository { sendRequest(identifier) { // TODO try a cache hit on the movie item - console.log(identifier) tmdb.lookup(identifier).then(movie => { console.log(movie.title) - var options = { - args: [movie.title, movie.year, movie.poster] - } - pythonShell.run('sendRequest.py', options, function (err, results) { - if (err) throw err; - // TODO Add error handling!! RequestRepository.ERROR - // results is an array consisting of messages collected during execution + // create reusable transporter object using the default SMTP transport + let transporter = nodemailer.createTransport({ + host: configuration.get('mail', 'host'), + port: 26, + ignoreTLS: true, + tls :{rejectUnauthorized: false}, + secure: false, // secure:true for port 465, secure:false for port 587 + auth: { + user: configuration.get('mail', 'user'), + pass: configuration.get('mail', 'password') + } + }); - console.log('results: %j', results) - }) - return true; + const mailTemplate = new MailTemplate(movie) + + // setup email data with unicode symbols + let mailOptions = { + // TODO get the mail adr from global location (easy to add) + from: 'MovieRequester ', // sender address + to: 'kevin.midboe@gmail.com', // list of receivers + subject: 'Download request', // Subject line + text: mailTemplate.toText(), + html: mailTemplate.toHTML() + }; + + // 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); + }); + + // var options = { + // args: [movie.title, movie.year, movie.poster] + // } + + // pythonShell.run('sendRequest.py', options, function (err, results) { + // if (err) throw err; + // // TODO Add error handling!! RequestRepository.ERROR + // // results is an array consisting of messages collected during execution + + // console.log('results: %j', results) + // }) }) + return Promise.resolve(); }