Send email without password using nodemailer over a zimbra smtp - node.js

im using nodemailer to send emails in a web app using keystonejs as cms. The web app is stored in a server and the email server in other, but SMTP communications between servers does not require password. Now, i need to send emails to other peoples when required using a generic account without the password field because is not neccesary.
This is my nodemailer config:
var selfSignedConfig = {
host: 'smtp.abc.cu',
port: 25,
secure: false, // use TLS
auth: {
user: email.email,
pass: email.password //NOT REQUIRED
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
};
var transporter = nodemailer.createTransport(selfSignedConfig);
// verify connection configuration
and:
"email": {
"email": "abcde#abc.cu",
"password": ""
}
I'm stuck on this, I have tried with "password": "" and "password": " " and nothing works. The email server is Zimbra.
This gave me the following error:
*-------------------------*
The server IS NOT READY to take the messages: Error: Invalid login: 535 5.7.8 Error: authentication failed: authentication failure
*-------------------------*
Greetings...

In our case we removed tls, auth and secure fields and it worked on our SMTP server.
The from, to options were set from mail options separately.
You might give the following a try:
var nodemailer = require ('nodemailer'),
_ = require ('lodash')
var selfSignedConfig = {
host: 'smtp.abc.cu',
port: 25
};
var transporter = nodemailer.createTransport(selfSignedConfig);
var attachFiles = attachments?attachments:[];
var attachObjArray = [];
_.forEach(
attachFiles,
filePath=>attachObjArray.push({path:filePath})
);
var mailOptions = {
from: fromEmail, // sender address (who sends)
to: toEmail, // list of receivers (who receives)
subject: subject, // Subject line
html: body, // html body
attachments:attachObjArray //attachment path with file name
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info) {
if(error){
return console.log(error);
} else {
console.log('Message sent: ' + info.response);
}
done();
});

Related

Nodemailer is not sending mails to valid emailids if array contains invalid emailids

I have tried to send multiple emails with nodemailer-smtp-transport in nodejs but if multiple recipients list contains valid and invalid email ids then mails are not going to valid emails ,always it is going to error callback.
My sample code:-
var testemails=["name.#gmail.com,santosh#gmail.com"];
var mailOptions = {
from:config.email_from_addr,
//bcc :receiver_email,
bcc:testemails,
subject :subject,
html:html
};
var transporter = nodemailer.createTransport(smtpTransport({
host:config.email_host_name,
port: 25
}));
transporter.sendMail(mailOptions, function(error, info){
console.log("error: "+error);
console.log("info: "+info);
if(error){
//console.log("Rejected: "+info.rejected);
console.log("error",'Failed to send: '+subject+' ; Error: '+error);
return callback(null);
}else{
console.log("info","Promotion_EmailFurnished: "+subject+" : "+receiver_email);
return callback(null);
}
});
//here Invalid emaild: name.#gmail.com and
valid emailId: santosh#gmail.com
and how to collect if mails are failed to send due to invalid emails?
Any help?
Thanks is advance.
Your array is wrong,
var testemails=["name.#gmail.com,santosh#gmail.com"];
Should be,
var testemails=["name.#gmail.com","santosh#gmail.com"];
Update
Above is just the first thing wrong with you code,
Second
in mailOptions you forgot to put the to address, bcc might work for nodemailer but without a to address it will fail from smtp endpoint.
var mailOptions = {
from:config.email_from_addr,
to : testemails,
bcc:testemails,
subject :subject,
html:html
};
Third,
Where is the password field for the transporter ?
sample for latest node mailer :
var transport = nodemailer.createTransport("SMTP", {
host: "smtp.gmail.com", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: "gmail.user#gmail.com",
pass: "userpass"
}
});
Please use this for more info as well : Docs

How to make nodemailer with SMTP work?

I've already lowered my gmail account's security here:
https://myaccount.google.com/lesssecureapps
I've tried to send mail with, and without smtp, but always get some kind of error.
Error: invalid login: 535.5.....
I checked my "user", and "from" parameters are the same. Here's a bit of my code. I'm working on localhost. May it be the problem?
var nodemailer = require("nodemailer");
var smtpTransport = require("nodemailer-smtp-transport");
var transporter = nodemailer.createTransport(smtpTransport({
service: "gmail",
auth: {
user: "seratothdevelop#gmail.com", // my mail
pass: "*********"
}
}));
console.log('SMTP Configured');
var message = "<b>Kedves Felhasználó!</b>"+
"<p>Sikeresen feliratkoztál a Kutyapplikáció hírlevelére, ahonnan friss információkkal látunk el hétről-hétre.</p>"+
"<br><p>Üdvözlettel, </p><p><i>sethdevelop</i></p>";
var mailOptions = {
from: "seratothdevelop#gmail.com", // sender address
to: request.body.subscribeduser, // list of receivers
subject: "Kutyapp feliratkozás", // Subject line
html: message // You can choose to send an HTML body instead
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Üzenet elküldve: ' + info.response);
}
transporter.close();
});

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.

Sending emails using Mailgun with NodeMailer package

A couple of days ago I realized that Google has changed the security of gmail accounts, particularly for the possibility of sending emails from applications. After Googling around for a while I couldn't find a fix for it.
So, I resorted to using Mailgun. I created an account and had it enabled with Business verification. However, I still can't send emails. I keep getting an error about the requested URL not being found.
I am suspecting that since I haven't set up a domain yet, it is not picking the mailgun domain it provided by default. Could someone show me how to test sending emails using Mailgun from NodeMailer indicating the sandbox name provided by mailgun.
thanks in advance
José
var nodemailer = require('nodemailer');
// send mail with password confirmation
var transporter = nodemailer.createTransport( {
service: 'Mailgun',
auth: {
user: 'postmaster#sandboxXXXXXXXXXXXXXXXX.mailgun.org',
pass: 'XXXXXXXXXXXXXXXX'
}
});
var mailOpts = {
from: 'office#yourdomain.com',
to: 'user#gmail.com',
subject: 'test subject',
text : 'test message form mailgun',
html : '<b>test message form mailgun</b>'
};
transporter.sendMail(mailOpts, function (err, response) {
if (err) {
//ret.message = "Mail error.";
} else {
//ret.message = "Mail send.";
}
});
I created the Nodemailer transport for mailgun.
Here it how it works.
You install the package with npm install as you would do with any package, then in an empty file:
var nodemailer = require('nodemailer');
var mg = require('nodemailer-mailgun-transport');
// This is your API key that you retrieve from www.mailgun.com/cp (free up to 10K monthly emails)
var auth = {
auth: {
api_key: 'key-1234123412341234',
domain: 'sandbox3249234.mailgun.org'
}
}
var nodemailerMailgun = nodemailer.createTransport(mg(auth));
nodemailerMailgun.sendMail({
from: 'myemail#example.com',
to: 'recipient#domain.com', // An array if you have multiple recipients.
subject: 'Hey you, awesome!',
text: 'Mailgun rocks, pow pow!',
}, function (err, info) {
if (err) {
console.log('Error: ' + err);
}
else {
console.log('Response: ' + info);
}
});
Replace your API key with yours and change the details and you're ready to go!
It worked me, when I added the domain also to the auth object (not only the api_key). Like this:
var auth = {
auth: {
api_key: 'key-12319312391',
domain: 'sandbox3249234.mailgun.org'
}
};

Nodemailer and GMail with access tokens

I try to use Nodemailer to send an email with my GMail account but it doesn't work, it works in local but on my remote server I recieve an email from Google "Someone is using your account...."
How can I do ?
exports.contact = function(req, res){
var name = req.body.name;
var from = req.body.from;
var message = req.body.message;
var to = '******#gmail.com';
var transport = nodemailer.createTransport("SMTP", {
service: 'Gmail',
auth: {
XOAuth2: {
user: "******#gmail.com",
clientId: "*****",
clientSecret: "******",
refreshToken: "******",
}
}
});
var options = {
from: from,
to: to,
subject: name,
text: message
}
transport.sendMail(options, function(error, response) {
if (error) {
console.log(error);
} else {
console.log(response);
}
transport.close();
});
}
Check out the solution from Unable to send email via google smtp on centos VPS:
In my case, my script is on a VPS so I don't have a way to load any url with a browser. What I did: Changed my gmail pw. Gmail > Settings > Accounts. Then in Google Accounts they listed suspicious logins that were blocked by google (these were my script's attempted logins). Then I clicked the option "Yes, that was me". After that, my script worked (using the new pw).

Resources