Using nodemailer to send a registration confirmation email. Works fine on my local machine, but fails from live server (using DigitalOcean droplet & InMotionHosting email account).
I have tried as many things as I can think of to no avail, so I don't know what to do next. Any help is appreciated.
Here's the code:
let transporter = nodemailer.createTransport({
host: 'mail.domain.com',
port: 587,
secure: false,
auth: {
user: 'test#domain.com',
pass: '654cba'
},
// true for live server, false for local machine
tls: {
rejectUnauthorized: true
}
});
let mailOptions = {
from: '"Register" <test#domain.com>',
to: user.username, // email address from user object
subject: 'Registration',
html: '<h1>mail content</h1>'
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
return res.send('Error sending confirmation email to: ' + user.username);
}
console.log('Message sent: %s', info.messageId);
req.flash('success', 'Success! Welcome ' + user.fullname + '! Please check your email, and click the link to complete your registration.');
res.redirect('/');
});
I set the tls.rejectUnauthorized to 'false' for the local machine and it works as expected.
On the live server, I set it to 'true', but receive the error message ('Error sending confirmation email to: ' + user.username) after transporter.sendMail() is executed. I tried eliminating this among many other tests to no avail.
Thank you for any assistance.
Related
I'm working with NodeMailer trying to figure out why the emails are being sent, but not received. I can see they are being sent because I can see them in my sent folder of my Gmail account. Please let me know what I'm doing wrong.
exports.sendEmail = async(address, subject, html) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
auth: {
user: 'noreply#example.app',
pass: process.env.GMAIL_PASSWORD
}
});
let mailOptions = {
subject: subject,
to: address,
html: html,
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
Then I call the function here:
await utils.sendEmail(user.email, 'Example Password Reset', '<body><p>Click this link to reset your password: Password Reset Link</p><br><p>Or copy this address into your browser: https://example.app/forgotpassword?code=' + passwordResetCode + '</p></body>');
All the emails look normal in my sent folder, but none of them are received by the recipient. Could it be something weird with having the ".app" at the end of my domain? Or am I just doing something wrong in my code?
Thanks!
I am trying to send an email which includes HTML content with the help of nodemailer-express-handlebars. Every time my mail gets blocked by the Gmail which can be checked in sender's Gmail sent-box whereas I got success msg from nodemailer
Email sent: 250 2.0.0 OK 1595608108 i66sm6757247pfc.12 - gsmtp
I am unable to understand why this happening as when I send a mail with text, it gets delivered.
NODEMAILER CODE
sendMail=(email)=>{
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'emailId',
pass: 'password'
}
});
transporter.use('compile',hbs({
viewEngine:{
partialsDir:"./views/",
defaultLayout: "",
layoutsDir: "",
},
viewPath:"./views/",
extName:'.hbs',
}))
var mailOptions = {
from: '<xyz#gmail.com>',
to: email,
subject: 'Your order has been placed successfully.',
template:'mail',
context:{
name:"XYZ",
address:"133"
}
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
It's recommended to use OAuth2 with NodeMailer and Gmail. Using the plain username and password might be what's causing you problems.
I've been at this problem all afternoon, all ideas are welcome.
I'm currently running the following example code to locate the issue:
router.get('/', function(req, res, next) {
let transporter = nodemailer.createTransport({
host: "cpsrv14.misshosting.com",
port: 587,
secure: false,
auth: {
user: config.email.user,
pass: process.env.EMAIL_PASS
},
tls: {
rejectUnauthorized: false
}
});
// verify connection configuration
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: 'christopher.rosenvall#gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
}).then(resp => {
console.log('Message sent: %s', JSON.stringify(resp));
res.status(200).json({
success: true
})
}).catch( err => {
console.log(err);
res.status(400)
});
});
On request console gives me the following which seems ok to me:
Server is ready to take our messages
Message sent: {"accepted":["christopher.rosenvall#gmail.com"],"rejected":
[],"envelopeTime":84,"messageTime":403,"messageSize":603,"response":"250 OK id=1iFhm4-0004WG-
NM","envelope":{"from":"foo#example.com","to":["christopher.rosenvall#gmail.com"]},"messageId":"
<7f8f8aab-f900-1e8e-2cde-3da5ad2687ba#example.com>"}
GET / 200 762.254 ms - 16
I've tried messing around with running secure and not, with the tls setting and without - doesn't seem to make a difference as long as I can authenticate.
Problem is that the email is never sent, ive tried sending it to a bunch of different email addresses and not a single one has recieved the email.
I reinstalled the project and now everything is working, odd.
Double-check if you give from in your option. I faced this problem. in my case, I didn't provide from in the option field. But everything is working after being given from:fromemail#mail.com
Below is the sample code am using for sending mail.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
auth: {
user: 'me#myserver.com',
pass: 'mypassword'
}
});
var mailOptions = {
from: 'no-reply#myserver.com', //It will work if i give me#myserver.com but i need no-reply#myserver.com in from option.
to: 'someuser#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I will give me below error if i use no-reply#myserver.com in from option.
Error: Message failed: 554 5.2.0
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;
Failed to process message due to a permanent exception with message
Cannot submit message.
Some email servers do not accept changing FROM address. This is not about nodemailer. You need to check your email server configuration.
I am having contiunual woes with getting nodemailer to send. I have now found the lowest security email account I can to see if I can get the thing working with a view to increasing security once something is getting through. My code is:
app.post("/contact-us", function(req, res){
var mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: 'smtp.123-reg.co.uk',
port: 25,
secure: false, // upgrade later with STARTTLS
auth: {
user: 'enquiries#*********.co.uk',
pass: '**********'
}
});
mailOpts = {
from: "enquiries#*******.co.uk",
to: "**********#mac.com",
subject: 'Website contact form',
text: "test email"
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
console.log(error);
res.render('contact', { title: '********', msg: 'Error occured, message not sent.', err: true, page: 'contact-us' });
}
//Yay!! Email sent
else {
console.log("message sent");
res.render('/');
}
});
});
Neither of the console.logs are coming back, instead the request is met with a timeout & 502 message,
Thanks
Just in case anyone searches for this in the future; the problem was within cloud 9 which wouldn't allow access to the email account to prevent spam.