nodemailer not sending mail, giving 250 ok - node.js

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

Related

Nodemailer taking too much time to send an e-mail

I'm building a notification server, that sends emails.
I'm using the node mailer library, but when I try to send an e-mail, it takes almost 5 minutes to receive the content in my inbox
I already searched why this happens and the only answer I found was to response content to tell the browser that the request has finished. Probably something like that: res.send(xxx).
But I'm using sockets to send e-mails automatically, what do I need to improve my server? My code:
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '...',
pass: '...'
}
});
...
io.on('connection', function(socket){
socket.on('notify', function(object) {
var mailOptions = {
from: '...',
to: object.message.sendTo,
subject: object.message.subject,
html: object.template
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
socket.emit("error", {data : "Error!"})
} else {
socket.emit("sent", {data : "Email successfully sent!"})
}
});
});
});
What do I need to change on my code?
Check the headers of the emails when you receive them to figure out where the email is getting "stuck". It might be in the original server, but it might be farther along (e.g., a spam filter or a forwarder or just a server somewhere in the middle). If it is anywhere except the first hop then there may be nothing you can do in your code to actually speed things up.

Nodemailer not able to create transport / 502 error

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.

Nodemailer fails on live server but works on local machine

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.

nodemailer not sending and returning error

I'm trying to setup a simple contact form using nodemailer. All i'm trying to do is send an email to my email from the contact form. My email is "myemail#mac.com"
When I do my axios post I get an error in the data object saying: 550 5.7.0 From address is not one of your addresses. but yet the status:200 and statusText:"OK"
When I use the same from email as the icloud email "myemail#mac.com" then it works ? I dont see anything where it says you have to use the same from address as the service address ?
Axios post:
const request = axios.post('http://localhost:3002/send', {'name':John Doe,'email':meme#gmail.com'});
request.then((result)=>{
console.log("request = ", result);
});
Error message from console.log("request = ", result);
error:{
code:"EMESSAGE",
command:"DATA",
response:"550 5.7.0 From address is not one of your addresses.",
responseCode:550
}
nodemailer is my node.js
const transporter = nodemailer.createTransport({
service: "iCloud",
auth: {
user: "myemail#mac.com",
pass: "myemailpassword"
}
})
app.use('/send', function(req, res){
var message = {
from: req.body.email,
to: 'myemail#mac.com',
subject: 'Message From Portfolio Contact Form',
//text: 'Plaintext version of the message',
html: '<p>'+req.body.description+'</p>'
};
transporter.sendMail(message, function(error, info){
if(error){
res.json({error: error});
}else{
res.json({success: info.response});
};
});
})
You configure nodemailer transport with iCloud Service and you are trying to send a mail with a gmail adresse.
from: req.body.email, // meme#gmail.com
to: 'myemail#mac.com'
Which logically produces the error:
response:"550 5.7.0 From address is not one of your addresses."
You probably want to do the inverse.

How can I create a custom smtp server to send out notification emails in Nodejs?

My requirement is to send a notification email from my application to any email id , eg: a gmail address. I went through some modules like the smtp-server ,smtp-connection and emailjs
This is what I have got till now.
var SMTPServer = require('smtp-server').SMTPServer
var server = new SMTPServer({
name: 'testDomain.com',
authOptional: true,
onAuth: function (auth, session, callback) {
callback(null, {user: 'sample-user'})
}
})
server.on('error', function (err) {
console.log('Error %s', err.message)
})
var port = 1234
server.listen(port, function () {
console.log('SERVER: Listening on port: ' + port)
var opts = {
host: '127.0.0.1',
port: port,
username: 'testUser',
password: 'testUser123',
to: 'someUser#gmail.com'
}
sendEmail(opts,function (err, message) {
server.close()
})
})
where sendEmail is a function using emailjs.
function sendEmail(opts,callback) {
var server = email.server.connect({
user: opts.username || '',
password: opts.password || '',
host: opts.host,
ssl: false
})
server.send({
text: 'i hope this works',
from: 'you <'+opts.username+'#testDomain.com>',
to: ' <'+opts.to+'>',
subject: 'testing emailjs'
}, function (err, message) {
console.log(err || message);
callback(err, message)
})
}
But it seems that the client is not able to connect to the server. It is hanging.
I tried smtp-connection like this initially:
var connection = new SMTPConnection({
port: port,
host: '127.0.0.1',
ignoreTLS: true
})
connection.connect(function () {
var envelope = {
from: opts.username+'#testDomain.com',
to: opts.to
}
var message = "Hello!!!"
connection.send(envelope, message, function(err,message){
callback(err,message)
connection.quit()
})
This seems to work but gives this output
response: '250 OK: message queued'
the smtp-connection documentation says it only queues the messages doesnt deliver it to the recipient.
How can I achieve my requirement? I am attempting to send the notification from a custom mail server because I want to avoid adding the user credentials of an email account in the code in plaintext. I am looking for a simple mailserver which can be spun up when the notification needs to be sent and then shut down.
Am I completely offtrack, not understanding how mail servers work?? Please give some feedback and a best approach to solve this.
Just my opinion but I think its better to take a separate mail server.
like the example from nodemailer:
var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass#smtp.gmail.com');
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Fred Foo ?" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plaintext body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
For the security:
You can use separate file for storing the username / password.
Use can use a Token based authentication. So you don't need to save the password. An example of this is OAuth. Instead of the password you authenticate with a token. This token u get from the mailserver provider (like gmail).
An example use of oauth and nodemailer you can find here.

Resources