# Handle mail server problems

**URL:** <https://www.ruby-forum.com/t/handle-mail-server-problems/112710>\
**Category:** Rails\
**Created:** [August 22, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710 "2007-08-22T13:09:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [August 22, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710/1 "2007-08-22T13:09:44Z")

</div>

I’m making a rails application that regularly sends out e-mails to  
users. While testing recently a controller got a timeout when trying  
to send an e-mail. =\> the mail server was down.

Is there an easy way to handle something like this? Or do I have to  
handle a timeout-exception whereever sending of mail is initiated?

I’m grateful for any help!

---

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [August 22, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710/2 "2007-08-22T16:12:26Z")

</div>

The best way is probably to have a queue that you store your emails in  
and then have a cron job to deliver them. I use railmail and modified  
it to work as a queue instead of immediately deliver the emails.

You can wrap it in a timeout block also. It goes something like

Timeout::timeout(10) do  
#send your email with a 10 second timeout  
end

rescue Timeout::Error  
#log the error

Thanks  
Fredrik

On Aug 22, 6:09 am, Eivind Løland-Andersen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [August 22, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710/3 "2007-08-22T19:07:56Z")

</div>

On Aug 22, 4:11 pm, Fredrik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rescue Timeout::Error  
> #log the error

Thanks! I think that timeout solution will do just fine. I agree that  
the best thing would be to put the e-mails into a queue, but in my app  
the e-mails are sendt as notifications, and must be sent immediately.

---

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [August 23, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710/4 "2007-08-23T17:54:03Z")

</div>

Eivind Løland-Andersen wrote:

> > #send your email with a 10 second timeout  
> > end
> > 
> > rescue Timeout::Error  
> > #log the error
> 
> Thanks! I think that timeout solution will do just fine. I agree that  
> the best thing would be to put the e-mails into a queue, but in my app  
> the e-mails are sendt as notifications, and must be sent immediately.

You can send them immediately in a background process that can try  
several times if a timeout occurs before giving up. Take a look at  
[http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails](http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails)

I’ve tried ap4r and backgroundrb. Both are not very well documented,  
however I found it easier to cope with ap4r.

–  
Sava C.

---

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [September 10, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710/5 "2007-09-10T17:47:56Z")

</div>

On Aug 22, 1:07 pm, Eivind Løland-Andersen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks! I think that timeout solution will do just fine. I agree that  
> the best thing would be to put the e-mails into a queue, but in my app  
> the e-mails are sendt as notifications, and must be sent immediately.

Even though most mail servers use queues that does not mean they don’t  
immediately try to deliver email. You really are better off letting  
the mail server handle it rather than write your own mail server in  
Ruby.

---

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [September 9, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/handle-mail-server-problems/112710/6 "2007-09-09T20:55:55Z")

</div>

btw - as an alternative, try activemessaging - I use it for exactly this  
problem:  
[http://code.google.com/p/activemessaging/](http://code.google.com/p/activemessaging/)  
Cheers,  
-A

On 8/23/07, Sava C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > You can wrap it in a timeout block also. It goes something like  
> > > the e-mails are sendt as notifications, and must be sent immediately.
> 
> >

–  
Andrew K.
