Reputation: 43
I'm trying to send emails using the following function using express js server. It seems to be working fine. But when I send email it’s not visible in my gmail inbox or spams.
Function i used:
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
auth: {
user: '[email protected]',
pass: 'BJkKV41tNZNBmMkkCw'
}
});
function SendEmail(email,subject,text,html) {
return new Promise(async (resolve, reject) => {
try {
let info = await transporter.sendMail({
from: "[email protected]",
to: email,
subject: subject,
text: text,
html:html
})
resolve(info)
} catch (error) {
reject(error)
}
})
}
Response received:
{
"accepted": [
"[email protected]"
],
"rejected": [],
"envelopeTime": 759,
"messageTime": 506,
"messageSize": 596,
"response": "250 Accepted [STATUS=new MSGID=YpXX5N-8J14cOJVHYpXjvw93heVfMHMGAAAAA4okSTkzId5ci.tBPvM2244]",
"envelope": {
"from": "[email protected]",
"to": [
"[email protected]"
]
},
"messageId": "<[email protected]>"
}
But not delivered to gmail. How to fix and explain to me the reason ?
Upvotes: 2
Views: 3490
Reputation: 43
Ethereal is a fake SMTP service, mostly aimed at Nodemailer and EmailEngine users (but not limited to). It's a completely free anti-transactional email service where messages never get delivered.
Instead, you can generate a vanity email account right from Nodemailer, send an email using that account just as you would with any other SMTP provider and finally preview the sent message here as no emails are actually delivered.
just as stated in home page of ethereal, it does not actually deliver mail. you can check on this link. link: https://ethereal.email/
Upvotes: 1
Reputation: 1
I have some issues with Ethereal too , but using a Gmail account work ok!. Remember use a 2FA authentication, here is the doc:
https://nodemailer.com/usage/using-gmail/
import nodemailer from "nodemailer";
const TEST_EMAIL = "thegmailaccount@com"
const TEST_PASSWORD = "thegmailpwgenerated";
const CLIENT = "[email protected]";
const transporter = nodemailer.createTransport({
service: 'gmail',
port: 587,
auth: {
user: TEST_EMAIL,
pass: TEST_PASSWORD,
}
});
const message = {
from: 'Dev 🥰<[email protected]>',
to: `"Dear client!" ${CLIENT}`,
subject: "Hi Sir!",
text: "Text if the HTML dont load",
html: <h1 style="color: red; ">This is a example</h1>,
};
try {
const info = await transporter.sendMail(message);
console.log(info);
} catch (error) {
console.log(error);
}
Upvotes: 0
Reputation: 11
Not sure you can use it to deliver mails. It's kinda testnet stuff.
Upvotes: -1
Reputation: 1
This is the way:
const nodemailer = require("nodemailer");
nodemailer.createTestAccount((err, account) => {
if (err) {
console.error("Failed to create a testing account. " + err.message);
return process.exit(1);
}
console.log("Credentials obtained, sending message...");
let transporter = nodemailer.createTransport({
host: account.smtp.host,
port: account.smtp.port,
secure: account.smtp.secure,
auth: {
user: account.user,
pass: account.pass,
},
});
let message = {
from: "Sender Name <[email protected]>",
to: "Recipient <[email protected]>",
subject: "Nodemailer is unicode friendly ✔",
text: "Hello to myself!",
html: "<p><b>Hello</b> to myself!</p>",
};
transporter.sendMail(message, (err, info) => {
if (err) {
console.log("Error occurred. " + err.message);
return process.exit(1);
}
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
});
Upvotes: 0