I am trying to use Nodemailer to send an email from my Angular app using Node.js
I am able to capture data from the Angular app & pass it to the server, but when I try to send the data using Nodemailer the following error is logged to the console:
SENDING ERROR MESSAGE: connect ETIMEDOUT
UnhandledPromiseRejectionWarning: ReferenceError: info is not defined
The code is failing here:
let info = await transporter.sendMail(mailOptions);
And here is my full app.js code:
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
var nodemailer = require("nodemailer");
app.get("/", (req, res) => {
res.send("Welcome to Node API");
});
app.get("/getData", (req, res) => {
res.json({ message: "Hello World" });
});
app.post("/postData", bodyParser.json(), async (req, res) => {
const output = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Company: ${req.body.message}</li>
</ul>
`;
console.log("EMAIL DETAILS: " + output);
try {
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "myaddress@mail.com",
pass: "myPassword"
},
tls: {
rejectUnauthorized: false
}
});
} catch (err) {
console.log("TRANSPORTER ERROR MESSAGE: " + err.message);
}
const mailOptions = {
from: "myaddress@mail.com", // sender address
to: "myaddress@mail.com", // list of receivers
subject: "Test email", // Subject line
html: output // plain text body
};
try {
let info = await transporter.sendMail(mailOptions);
} catch (err1) {
console.log("SENDING ERROR MESSAGE: " + err1.message);
}
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
app.listen(3000, () => console.log("Example app listening on port 3000!"));
Can someone please give me some guidance as to why this is failing?
Also, I've tried other types of email addresses (hotmail, etc.) but they all give me the same error.