-
-
Save isamarietr/4e71bc75c5f91ef55bcb748e8591426a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nodemailer = require('nodemailer'); | |
let smtpConfig = { | |
host: 'smtp.mail.yahoo.com', // you can also use smtp.gmail.com | |
port: 465, | |
secure: true, // use TLS | |
auth: { | |
user: 'YOUR-SENDER-EMAIL-ADDRESS-HERE', | |
pass: 'YOUR-EMAIL-PASSWORD-HERE' | |
} | |
}; | |
function main(params) { | |
return new Promise(function (resolve, reject) { | |
let response = { | |
code: 200, | |
msg: 'E-mail was sent successfully!' | |
}; | |
if (!params.reminder) { | |
response.msg = "Error: Reminder was not provided."; | |
response.code = 400; | |
} | |
else if (!params.email) { | |
response.msg = "Error: Destination e-mail was not provided."; | |
response.code = 400; | |
} | |
else if (!params.conversation_id) { | |
response.msg = "Error: Conversation id was not provided."; | |
response.code = 400; | |
} | |
if (response.code != 200) { | |
reject(response); | |
} | |
console.log(`Validation was successful, preparing to send email...`); | |
sendEmail(params, function (email_response) { | |
response.msg = email_response['msg']; | |
response.code = email_response['code']; | |
response.reason = email_response['reason']; | |
console.log(`Email delivery response: (${email_response['code']}) ${response.msg}`); | |
resolve(response); | |
}); | |
}); | |
} | |
function sendEmail(params, callback) { | |
let transporter = nodemailer.createTransport(smtpConfig); | |
let mailOptions = { | |
from: `Your Reminder Buddy <${smtpConfig.auth.user}>`, | |
to: params.email, | |
subject: `REMINDER: ${params.reminder}`, | |
text: `Do it!` | |
}; | |
transporter.sendMail(mailOptions, function (error, info) { | |
let email_response = { | |
code: 200, | |
msg: 'Email was sent successfully', | |
reason: 'Success' | |
}; | |
if (error) { | |
email_response.msg = 'Error'; | |
email_response.code = 500; | |
email_response.reason = error; | |
} | |
else { | |
email_response.msg = info.response; | |
email_response.code = 200; | |
email_response.reason = info.response; | |
} | |
callback(email_response); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks but please keep your medium article alive......