Forum: Ruby on Rails Multiple emails from one SMTP connection?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ryan W. (Guest)
on 2006-01-04 19:12
(Received via mailing list)
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
Gerret A. (Guest)
on 2006-01-04 19:14
(Received via mailing list)
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
Charles M. Gerungan (Guest)
on 2006-01-04 23:34
(Received via mailing list)
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.
Jonathan V. (Guest)
on 2006-01-05 14:00
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
Ryan W. (Guest)
on 2006-01-05 21:09
(Received via mailing list)
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
This topic is locked and can not be replied to.