Last active
January 18, 2019 04:46
-
-
Save funador/e2746dd837dcc006339a47ae11cb1eb4 to your computer and use it in GitHub Desktop.
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') | |
// The credentials for the email account you want to send mail from. | |
const credentials = { | |
host: 'smtp.gmail.com', | |
port: 465, | |
secure: true, | |
auth: { | |
// These environment variables will be pulled from the .env file | |
user: process.env.MAIL_USER, | |
pass: process.env.MAIL_PASS | |
} | |
} | |
// Getting Nodemailer all setup with the credentials for when the 'sendEmail()' | |
// function is called. | |
const transporter = nodemailer.createTransport(credentials) | |
// exporting an 'async' function here allows 'await' to be used | |
// as the return value of this function. | |
module.exports = async (to, content) => { | |
// The from and to addresses for the email that is about to be sent. | |
const contacts = { | |
from: process.env.MAIL_USER, | |
to | |
} | |
// Combining the content and contacts into a single object that can | |
// be passed to Nodemailer. | |
const email = Object.assign({}, content, contacts) | |
// This file is imported into the controller as 'sendEmail'. Because | |
// 'transporter.sendMail()' below returns a promise we can write code like this | |
// in the contoller when we are using the sendEmail() function. | |
// | |
// sendEmail() | |
// .then(() => doSomethingElse()) | |
// | |
// If you are running into errors getting Nodemailer working, wrap the following | |
// line in a try/catch. Most likely is not loading the credentials properly in | |
// the .env file or failing to allow unsafe apps in your gmail settings. | |
await transporter.sendMail(email) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment