right way to use nodemailer and nodemailer-smtp-pool

2,495 views
Skip to first unread message

craftygirl x

unread,
Aug 7, 2018, 12:35:22 PM8/7/18
to nodejs
I have case where I send multiple emails( with different contents and subjects), all in array , traversed in a loop to  deliver multiple receipients.

First I used simple nodemailer and i found more than 5 emails in a row, i get errors.

I have input data in a json form where I calculate how many emails and what contents will go. So senders list and contents are not static and calculated in same code before sending out emails.

so first case, 
1. Use simple nodemailer. Mail sending part is a promise. Works but not more than 5. Each email 's return messages etc can be tracked fine and final results can be returned since emails work sequential.

2. Use nodemailer-smtp-pool to increase number of emails.
Strange that emails do not work normal. 1 or 2 emails are sent and 3rd not. Then if I trigger email code multiple times, within seconds it works once or twice with error messages. 
"Mail Command Failed: 421 #4.x.2. too many messages for this session"

Code is:

var smtpPool = require('nodemailer-smtp-pool')
pooledTr =nodemailer.createTransport( smtpPool{ host: "", secure:false, port: 25, maxConnections:25, maxMessaes:50, rateLimit:10...})

then in a loop for all the emails,  
pooledTr.sendMail(mOptions , (err, callback)=>{....})

Now, we do not have promise anymore, as we do not want to wait till first email send and returns.

Can any one help if I need to use events in pooledtransported, like wait till connection is idle to send. Also If I need to add time wait or promise till it delivers?

I googled but do not what examples per my scenario.

Zlatko

unread,
Aug 8, 2018, 3:35:27 AM8/8/18
to nodejs
What errors do you get in the "simple nodemailer" case? The same one?

I seriously suspect your problem is in maxConnections and maxMessages. Reduce that to some small value like 5 each, or even less. From my sysadmin days, I remember some bigger, known domains even had these set to 1, so this is also per-domain configurable. These are the reasons services like mailgun do well - they gather a lot of specific knowledge about individual domains.

Zlatko


On Tuesday, August 7, 2018 at 6:35:22 PM UTC+2, sunshine o wrote:
"Mail Command Failed: 421 #4.x.2. too many messages for this session"
var smtpPool = require('nodemailer-smtp-pool')
 
pooledTr =nodemailer.createTransport( smtpPool{ host: "", secure:false, port: 25, maxConnections:25, maxMessaes:50, rateLimit:10...}).

sunshine o

unread,
Aug 8, 2018, 9:11:49 PM8/8/18
to nodejs
I have restriction from my company environment to use any other SMTP server which is why I think I can send 5, not more one time.

I tried playing with maxConnections etc properties increasing values but did not help.

So now, my approach is to live with number 5. But making it send multiple times. I split the emails data into batches of 5 and then send and delay/wait. Still first batch of 5 gets delivered,
at next i get error.

"Too many messages"
"Too many sessions".

So that means here problem is with my connection close etc. So when next batch starts, it should be new connection.  So I try to initiate the connection (createTransport())  with each batch. 
Please let me know if i am completely wrong approach.

Zlatko Đurić

unread,
Aug 16, 2018, 12:47:11 AM8/16/18
to nod...@googlegroups.com
Well some config issue is obvious: 1. either your client has too many open connections, or it sends too many messages on a single open connection, or it sends too many messages in a given time frame (some systems also limit # of messages in a time unit).
Maybe your app is using a cluster module so you open too many connections from the server, even though you think you limited to X?

So I'd suggest this:
1. instead of sending your messages from your app's other modules, have them stored on a message queue. Can be a database, like Redis, SQL, whatever, or it can be something like RabbitMQ - whatever you already use. Just put them away.
2. Have another subsystem in your app that periodically checks if there are messages on this queue to be sent.

This would be my approach regardless of the limits. That's how you decouple your app and the sending of messages. You can deal with multiple app servers, with recovering from crashes, many things. You can even deploy the whole messaging subsystem (sending mails) as a separate node app.

Now with the message sending sitting on it's own somewhere, you can do all this fun stuff about rate-limiting your message throughput. You can limit your system to do, e.g. max 1 mail per 5 seconds, or per 1 minute or similar. And to be certain, go to the mail server admins and ask them about the limits, then go bellow those limits.



--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/fc1xYKNdxFk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/119a2265-c244-4cc7-86ef-f56de0512634%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Zlatko
Reply all
Reply to author
Forward
0 new messages