Last active
October 27, 2020 22:53
-
-
Save gagregrog/9e71ef8d6ac1b52b3de1d1f3cb293f54 to your computer and use it in GitHub Desktop.
Send an email with Nodemailer
This file contains hidden or 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
const nodemailer = require('nodemailer'); | |
const smtpTransport = require('nodemailer-smtp-transport'); | |
const options = { | |
service: 'gmail', | |
host: 'smtp.gmail.com', | |
port: 465, | |
secure: true, | |
auth: { | |
user: process.env.FROM, | |
pass: process.env.PASS, | |
}, | |
}; | |
const mailOptions = { | |
from: process.env.FROM, | |
to: process.env.TO || process.env.FROM, | |
subject: 'Updated COVID results', | |
text: 'Updated results are available. Login to your portal to view them.' | |
} | |
const mailOK = process.env.FROM && process.env.PASS; | |
let transporter; | |
if (mailOK) { | |
transporter = nodemailer.createTransport(smtpTransport(options)); | |
} | |
module.exports = async () => { | |
if (mailOK) { | |
try { | |
await transporter.sendMail(mailOptions); | |
console.log('Email sent successfully\n'); | |
} catch (error) { | |
console.log('Error sending email...\n', error, '\n'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment