Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created August 12, 2022 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarstens/8b34187e52042cbd4991916f94c5ca65 to your computer and use it in GitHub Desktop.
Save scarstens/8b34187e52042cbd4991916f94c5ca65 to your computer and use it in GitHub Desktop.
Nodemailer SMTP connection test
// 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