I am using the nodemailer module to send emails through my gmail account.
The problem i am facing is that they are sent from my account but never received by the other mail.
Whats more confusing to me is that when I send the mail to the same mail as the sender than it works, but that is of course not what I want.
Any one knows how to fix this?
Here is what i tried:
const transporter = nodemailer.createTransport({
name: "smtp.gmail.com",
host: "smtp.gmail.com",
service: 'gmail',
auth: {
user: 'email',
pass: 'pass'
},
secure: false,
logger: true,
debug: true
});
const mailOptions = {
from: '...',
to: '...',
subject: '...',
html: '...'
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
No errors and when i look in my mail account it shows that it was sent
Related
I'm trying to setup ZOHO mail with Nodemailer. The mail is configured correctly and I'm using following code to send the mail, but still getting error in sending mail:
const nodemailer = require('nodemailer');
let from = `Company Name <contact#company.com>`
let transporter = nodemailer.createTransport({
// host: "smtp-mail.gmail.com",
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: "contact#company.com",
pass: "mypassword"
}
});
// Mail response to User
const mailResponse = {
from: from,
to: `userName`,
subject: "📞 Thanks For Connecting With Company Name",
html: // mail body
}
try {
await transporter.sendMail(mailResponse);
res.status(200).json({ message: "Message Sent" });
} catch (err) {
res.status(400).json({ message: "Unexpected Error!!! Please try again" });
}
Please let me know how can I fix this issue. i have tried every possible solution given on website.
Try this -
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "username#company.com",
pass: "yourpassword"
}
At the place of user put your email and in pass but your password or you can get these from env to be safe.
I was trying to send a test email using SMTP on node mailer but it says connection timed out. the snippet I was using is down below.
const nodemailer = require("nodemailer");
async function main() {
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
pool:true,
host: '213.55.96.132',
port: 25,
auth: {
user: "user#ethionet.et",
pass: "drafgthsjaid321##"
},
pool: true,
logger :true,
debug:true,
secure: false,
})
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take our messages');
}
});
let mailOptions = {
from: "user#ethionet.et",
to: ["someemail#gmail.com",],
subject: 'Test email',
text: `Hello world`
};
transporter.sendMail(mailOptions, function(err, data) {
if (err) {
console.log("Error " + err);
} else {
console.log("Email sent successfully");
}
});
}
main().catch(console.error);
I don't mind leaking the credentials and it works when i try and send emails through SMTP from here.
why is this faliing?
You need to read a little more than the first page of documentation :)
Create your message
let message = {
...,
from: 'mailer#nodemailer.com', // listed in rfc822 message header
to: 'daemon#nodemailer.com', // listed in rfc822 message header
envelope: {
from: 'Daemon <deamon#nodemailer.com>', // used as MAIL FROM: address for SMTP
to: 'mailer#nodemailer.com, Mailer <mailer2#nodemailer.com>' // used as RCPT TO: address for SMTP
}
}
Send the message through the transporter
transporter.sendMail(...).then(info=>{
console.log('Preview URL: ' + nodemailer.getTestMessageUrl(info));
});
Turns out the problem was that my ISP blocks port 25.
I'm trying to send email using node.js and nodemailer but when I'm pressing the submit button its just loading and loading and in the end gives me a "504 Gateway Timeout Error" in the server, and "page is not working" locally.
I'm using the following code:
app.post("/postmail", function (req, res) {
var transporter = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
secure: false,
debug: true,
auth: {
user: "xxx",
password: "xxx",
},
});
var message = {
from: "a#b", // Sender address
to: "b#c", // List of recipients
subject: "Design Your Model S | Tesla", // Subject line
text: "Have the most fun you can in a car. Get your Tesla today!", // Plain text body
};
transporter.sendMail(message, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
res.render("landing");
}
});
});
I must also mention that, I tried multiple smtp servers with multiple ports and configuration.
does someone know what to do?
thanks
cuase all of the smtp providers use tls/ssl for secuirity reasons. try to use secure configuration:
var transporter = nodemailer.createTransport({
host: 'smtp.mailtrap.io',
port: 465,
secure: true,
debug: true,
auth: {
user: 'xxx',
password: 'xxx'
}
});
When trying to send email within Node using Nodemailer (https://github.com/nodemailer/nodemailer), the call to the sendMail of the Nodemailer transporter is raising the error Greeting never received when using in conjunction with an Ethereal test email account.
I have tried using both a "callback approach" and also an "async/await" approach, but the same error is thrown in both scenarios. Both examples are pretty much straight from the working examples in the Nodemailer documentation. Maybe I'm missing something simple? :)
Here is the "callback approach" code that is producing the error:
it('can send email with a dynamic test account', done => {
nodemailer.createTestAccount((err, account) => {
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
const mailOptions = {
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: 'bar#example.com, baz#example.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
done();
});
});
}).timeout(10000);
And here is the stacktrace of the error:
{ Error: Greeting never received
at SMTPConnection._formatError (/Users/<username>/projects/personal/learning-tests/javascript/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
at SMTPConnection._onError (/Users/<username>/projects/personal/learning-tests/javascript/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:579:20)
at Timeout._greetingTimeout.setTimeout (/Users/<username>/projects/personal/learning-tests/javascript/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:520:22)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5) code: 'ETIMEDOUT', command: 'CONN' }
And some additional info:
node version: 8.11.2
nodemailer version: 4.6.4
operating system: OSX version 10.12.6
In my case I needed to set the secure key to true on the transporter object and then it worked.
let transporter = nodemailer.createTransport({
host: "mail.hostname.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'user#hostname.com', // generated ethereal user
pass: 'password', // generated ethereal password
}
});
In my case, when I have changed port 586 to 587, then it worked.
Check your internet connection probably its down .
below is an example with Etheral Email with typescript
import * as nodemailer from "nodemailer";
export const sendEmail = async (recipient: string, url: string, linkText: string) => {
nodemailer.createTestAccount((err, account) => {
if (err) {
console.log(err);
}
const transporter = nodemailer.createTransport({
host: account.smtp.host,
port: account.smtp.port,
secure: account.smtp.secure,
auth: {
user: account.user,
pass: account.pass
}
});
const message = {
from: "Sender Name <sender#example.com>",
to: `Recipient <${recipient}>`,
subject: "Nodemailer is unicode friendly ✔",
text: "Hello to myself!",
html: `
<html>
<body>
<p>Testing sparkpost API</p>
${linkText}
</body>
</html>`
};
transporter.sendMail(message, (err, info) => {
if (err) {
console.log("Error occurred. " + err.message);
}
console.log("Message sent: %s", info.messageId);
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
});
};
In my case the smtpd_recipient_restrictions in /etc/postfix/main.cf was causing this issue.
Changed it to:
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
check_policy_service unix:private/policyd-spf
and now it works!
const transporter = nodemailer.createTransport({
service: 'config.mail.service',
port: 8000,
auth: {
user: 'config.mail.username',
pass: 'config.mail.password'
}
});
module.exports = {
activationsMail: function (req) {
// setup email data with unicode symbols
const mailOptions = {
from: '"Ecommerce" <noreply#ecommerce.com>', // sender address
to: req.body.email, // list of receivers
subject: 'Ecommerce Account Activate', // Subject line
html: '<div>Please click here to active your account.</div>' // html body
};
console.log('PORT', req.headers.host);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log('Email Error', error);
} else {
console.log('Email sent: ' + info.response);
}
})
}
};
const transporter = nodemailer.createTransport({
service: config.mail.service,
port: 8000,
auth: {
user: config.mail.username,
pass: config.mail.password
}
});
module.exports = {
activationsMail: function (req, data) {
// setup email data with unicode symbols
const link = 'http://' + req.headers.host + '/user/activate/' + data.verifyCode;
console.log('CODE :', data.verifyCode);
const mailOptions = {
from: '"Ecommerce" <noreply#ecommerce.com>', // sender address
to: req.body.email, // list of receivers
subject: 'Please confirm your Email account', // Subject line
html: '\n\n' + 'Please Click here to verify <a href=' + link + '> Click here</a>'
};
//console.log('PORT', req.headers.host);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log('Email Error', error);
} else {
// callback(true);
console.log('Email sent: ' + info.response);
}
})
};
I am using nodemail npm package.
I configured the options like this:
function feedback(req, res, next){
console.log('feed back given....', req.body);
smtpTrans = nodemailer.createTransport('SMTP', {
service: 'Gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
ignoreTLS: false,
tls: { rejectUnauthorized: true },
debug: false,
auth: {
user: "xxxxxx#gmail.com",
pass: "xxxxxx"
}
});
//Mail options
mailOpts = {
from: from: req.body.email,
to: 'xxxxxx#gmail.com',
subject: 'EMAIL FROM Rsc-student: ' + req.body.subject,
text: req.body.message
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
res.send(error);
console.log('error sending mail');
}
else {
res.send(response);
console.log('success sending mail');
}
});
}
If I am not wrong, I configured correctly but still unable to send mail. Its printing the error case
Remove extra from: from mailOpts.
Follow this 3 steps:
Login to your Gmail account.
Follow this link allow gmail to send mail over less secure app.
Select on option.
It works for me. I hope it will help you.