Multiple emails from one SMTP connection?

Hi all. I’m running a nightly job that is creating a CSV file and
sending to member via SMTP. I got the following error after about 20
emails or so: Net::SMTPServerBusy Too many connections from IP…

Is there a way (using ActionMailer) to open a connection and send
multiple emails. Or could I make sure the connection is closed before
opening another? The only other option is to send some type of wait
signal if I catch that error. Any other ideas?

Thanks for the help!

–Ryan

Ryan,

if you’re not customizing the email on a per-recipient basis,
definitely go for sending you email in a single SMTP session. I
believe the ActionMailer API doesn’t let you do that as-is.
ActionMailer uses the TMail library internally though, so you might be
able to intercept the TMail instance generated by ActionMailer, and
then send it to multiple recipients, taking away the SMTP portion of
the work from ActionMailer and doing it yourself.

In pseudo-brief, somewhat like this

Net::SMTP.start(server, port) do |sender|
recipients.each do |recipient|
tmail.to = recipient
sender.sendmail tmail.encoded, tmail.from, [recipient]
end
end

Exactly where to intercept the Tmail object created by ActionMailer,
you’d have to dig to find out.

cheers
Gerret

On 4-jan-2006, at 17:04, Ryan W. wrote:

Is there a way (using ActionMailer) to open a connection and send
multiple emails. Or could I make sure the connection is closed before
opening another? The only other option is to send some type of wait
signal if I catch that error. Any other ideas?

Talk directly to sendmail (or qmail-inject, or whatever)?


Regards, Charles.

I wanted to do the same thing and have written a patch,
http://dev.rubyonrails.org/ticket/3307. It allows you to pass an array
of messages to ActionMailer::Base.deliver which will all be delivered
with one SMTP connection. It works ok for me, apart from the error
handling (see ticket).

You’d use it like…

emails = []
emails << MyMailer.create_message
emails << MyMailer.create_message

MyMailer.deliver(emails)

Cheers,
Jonathan

Ryan W. wrote:

Hi all. I’m running a nightly job that is creating a CSV file and
sending to member via SMTP. I got the following error after about 20
emails or so: Net::SMTPServerBusy Too many connections from IP…

Is there a way (using ActionMailer) to open a connection and send
multiple emails. Or could I make sure the connection is closed before
opening another? The only other option is to send some type of wait
signal if I catch that error. Any other ideas?

Thanks for the help!

–Ryan

Thanks a lot. I’ll give that a shot. In the meantime, I have my host
adjust their spam controls to allow more connections which has been
temporary fix.

–Ryan