Created
October 20, 2020 16:40
-
-
Save dbasilioesp/9cd65895b9b41102c6d204de6b94b59a 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"); | |
const fs = require("fs"); | |
const readFile = (path, opts = "utf8") => { | |
return new Promise((resolve, reject) => { | |
fs.readFile(path, opts, (err, data) => { | |
if (err) reject(err); | |
else resolve(data); | |
}); | |
}); | |
}; | |
async function main() { | |
// Generate test SMTP service account from ethereal.email | |
// Only needed if you don't have a real mail account for testing | |
let html = await readFile("index.html"); | |
// create reusable transporter object using the default SMTP transport | |
var transport = nodemailer.createTransport({ | |
host: process.env.SMTP, | |
port: Number(process.env.PORT), | |
secure: false, // upgrade later with STARTTLS | |
auth: { | |
user: process.env.USER, | |
pass: process.env.PASSWORD | |
} | |
}); | |
// send mail with defined transport object | |
let info = await transport.sendMail({ | |
from: '"Fred Foo 👻" <[email protected]>', // sender address | |
to: "[email protected]", // mailtrap email | |
subject: "Hello ✔", // Subject line | |
text: "Hello world?", // plain text body | |
html: html, // html body | |
}); | |
console.log("Message sent: %s", info.messageId); | |
// Message sent: <[email protected]> | |
// Preview only available when sending through an Ethereal account | |
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); | |
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou... | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment