Created
August 12, 2022 16:54
-
-
Save scarstens/8b34187e52042cbd4991916f94c5ca65 to your computer and use it in GitHub Desktop.
Nodemailer SMTP connection test
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
// Make sure you run this file from the project root | |
// or use `npm run email` | |
import nodemailer from "nodemailer"; | |
import * as dotenv from "dotenv"; | |
import path from "path"; | |
dotenv.config({ | |
// path: path.resolve(path.basename(path.dirname(process.cwd())), ".env"), | |
}); | |
console.log( | |
"Setup email config: ", | |
process.env.EMAIL_SERVER_HOST, | |
process.env.EMAIL_SERVER_PORT, | |
process.env.EMAIL_SERVER_USER | |
); | |
let transport = nodemailer.createTransport({ | |
host: process.env.EMAIL_SERVER_HOST, | |
port: process.env.EMAIL_SERVER_PORT, | |
// secure: true, | |
auth: { | |
user: process.env.EMAIL_SERVER_USER, | |
pass: process.env.EMAIL_SERVER_PASSWORD, | |
}, | |
debug: true, | |
logger: true, | |
}); | |
let scrapeEmailMessage = { | |
from: process.env.EMAIL_FROM_ADDRESS, | |
to: process.env.EMAIL_TEST_TO_ADDRESS, | |
subject: "Nodemailer Healthcheck Success", | |
text: "This is a NodeJS App test email. It is used to confirm the healthcheck of nodemailer.", | |
}; | |
transport.sendMail(scrapeEmailMessage, function (err, data) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log( | |
"Email sent successfully to ", | |
process.env.EMAIL_TEST_TO_ADDRESS | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment