Last active
November 10, 2020 06:24
-
-
Save jgngo/565ace90e2ab6dbb711ba8dc6f7b22ae to your computer and use it in GitHub Desktop.
send email using nodemailer
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
require('dotenv').config() | |
const nodemailer = require("nodemailer"); | |
// async..await is not allowed in global scope, must use a wrapper | |
async function main() { | |
let transporter = nodemailer.createTransport({ | |
host: process.env.SMTP_HOST, | |
port: process.env.SMTP_PORT, | |
secure: true, | |
auth: { | |
user: process.env.SMTP_USER, | |
pass: process.env.SMTP_PASS, | |
}, | |
}); | |
let info = await transporter.sendMail({ | |
from: `${process.env.SMTP_FROM_NAME} <${process.env.SMTP_FROM_EMAIL}>`, // sender address | |
to: process.env.SMTP_TO_EMAIL, | |
subject: "✔ Sending from Amazon SES ✔", // Subject line | |
text: "Hello world?", // plain text body | |
html: "<b>Hello world?</b>", // html body | |
}); | |
console.log("Message sent: %s", info.messageId); | |
} | |
main().catch(console.error); | |
/* | |
Create a .env file, replacing the values below | |
$ npm install nodemailer | |
$ npm install dotenv | |
$ node sendmail.js | |
SMTP_FROM_NAME=Mailer | |
SMTP_FROM_EMAIL=noreply@example.com | |
SMTP_USER=ASDWERWSDF | |
SMTP_PASS=SDFWRQWERWR | |
SMTP_HOST=email-smtp.ap-southeast-1.amazonaws.com | |
SMTP_PORT=465 | |
SMTP_TO_EMAIL=user1@example.com, user2@example.com | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment