Created
February 19, 2019 12:28
-
-
Save Monehin/fc5bc8ca28656385dcb4459264490821 to your computer and use it in GitHub Desktop.
Using Nodemailer with Gmail SMTP[CODE SAMPLE]
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
/* | |
Reference => | |
https://stackoverflow.com/questions/45478293/username-and-password-not-accepted-when-using-nodemailer | |
- Log in to your Google account | |
- Go to My Account | |
- Security > App Passwords | |
- Scroll down to Select App (in the Password & sign-in method box) and choose Other (custom name) | |
- Give this app password a name, e.g. "nodemailer" | |
- Choose Generate | |
- Copy the long generated password and paste it into your Node.js script instead of your actual Gmail password. (You don't need the spaces.) | |
*/ | |
const express = require('express'); | |
const nodemailer = require('nodemailer'); | |
const app = express(); | |
const PORT = process.env.PORT | 3030; | |
var transporter = nodemailer.createTransport( | |
smtpTransport({ | |
service: 'gmail', | |
host: 'smtp.gmail.com', | |
auth: { | |
user: [USER_EMAIL], | |
pass: [GMAIL_GENERATED_PASSWORD] | |
} | |
}) | |
); | |
var mailOptions = { | |
from: [USER_EMAIL], | |
to: [RECEPIENT_EMAIL], | |
subject: 'Sending Email using Node.js[nodemailer]', | |
text: 'That was easy!' | |
}; | |
transporter.sendMail(mailOptions, function(error, info) { | |
if (error) { | |
console.log(error); | |
} else { | |
console.log('Email sent: ' + info.response); | |
} | |
}); | |
app.get('', (req, res) => { | |
res.send('Hello World'); | |
}); | |
app.listen(PORT, () => { | |
console.log(`App running on port ${PORT}`); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment