Skip to content

Instantly share code, notes, and snippets.

@adamelliotfields
Last active March 24, 2019 21:01
Show Gist options
  • Save adamelliotfields/5afbc4503319e4ff03d38e74d90750d5 to your computer and use it in GitHub Desktop.
Save adamelliotfields/5afbc4503319e4ff03d38e74d90750d5 to your computer and use it in GitHub Desktop.
Nodemailer v0.7 Gmail Example
const nodemailer = require('nodemailer');
const transport = nodemailer.createTransport('smtp', {
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS,
},
});
transport.sendMail({
from: process.env.GMAIL_USER,
to: process.env.GMAIL_USER,
generateTextFromHTML: true,
encoding: 'base64',
subject: 'Hello world',
html: '<b>Hello world</b>',
}, (err) => {
if (err) {
console.error('Error:', err.message);
}
transport.close();
});
@adamelliotfields
Copy link
Author

adamelliotfields commented Mar 24, 2019

Ghost uses Nodemailer v0.7, so this is a quick smoke test to check settings.

You need to enable 2-Step Verification and then create an App Password.

If auth.user includes @gmail.com, then Nodemailer will set service, host, port, and secureConnection automatically.

Usage:

echo '{"private":true,"dependencies":{"nodemailer":"0.7.1"}}' | tee package.json

npm install

wget https://gist.github.com/adamelliotfields/5afbc4503319e4ff03d38e74d90750d5/raw/95918e10235e759ffaaff3ca49563a3eaf6623ce/index.js

GMAIL_USER='your_gmail_username@gmail.com' GMAIL_PASS='your_app_password' node index.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment