# How to send SMTP emails in background

**URL:** <https://www.ruby-forum.com/t/how-to-send-smtp-emails-in-background/131971>\
**Category:** Rails\
**Created:** [March 14, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/how-to-send-smtp-emails-in-background/131971 "2008-03-14T20:21:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/how-to-send-smtp-emails-in-background/131971/1 "2008-03-14T20:21:55Z")

</div>

My application sends emails with SMTP but I have a problem: It is very  
slow, so every time my application sends an email it goes very slow.

I wonder if there is any kind of mode to do all the SMTP in background.  
I mean, I would like to continue sending SMTP emails but I would like my  
app not to go slow every time it sends an email. The app should do all  
the business about sending the email in background.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/how-to-send-smtp-emails-in-background/131971/2 "2008-03-14T20:32:31Z")

</div>

you’ve got at least two choices:

1. send your mail in a background process using backgroundrb  
([http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/))
2. use ar\_mailer  
([http://blog.segment7.net/articles/2006/08/15/ar\_mailer](http://blog.segment7.net/articles/2006/08/15/ar_mailer))

backgroundrb requires you to run a separate backgroundrb process,  
ar\_mailer requires you to set up another database table for queueing  
the mail, and then running a cron job at a specified interval to send  
all the queued mail

Mike

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2008, 9:36pm UTC](https://www.ruby-forum.com/t/how-to-send-smtp-emails-in-background/131971/3 "2008-03-14T21:36:57Z")

</div>

Another choice is to use the Ruby Thread. You can read up on it here

[http://www.rubycentral.com/pickaxe/ref\_c\_thread.html](http://www.rubycentral.com/pickaxe/ref_c_thread.html)

I’ve had great success using it with all my background processing tasks.  
I just haven’t found a  
situation where I needed backgroundrb, yet.

– Long  
[http://FATdrive.tv/wall/trakb/10-Long](http://FATdrive.tv/wall/trakb/10-Long)  
[http://FATdrive.tv/](http://FATdrive.tv/) - store, play, share

----- Original Message -----  
From: “Mike G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, March 14, 2008 2:31 PM  
Subject: [Rails] Re: How to send SMTP emails in background

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 15, 2008, 2:49pm UTC](https://www.ruby-forum.com/t/how-to-send-smtp-emails-in-background/131971/4 "2008-03-15T14:49:04Z")

</div>

In my case i used the Thread method.  
In this example i use msmtp, and send my email via GMail.

In environment.rb

ActionMailer::Base.delivery\_method = :msmtp

module ActionMailer  
class Base  
def perform\_delivery\_msmtp(mail)  
thread = Thread.new do  
IO.popen("/usr/bin/msmtp -t -C /etc/.msmtprc -a gmail --",  
“w”) do  
|sm|  
sm.puts(mail.encoded.gsub(/\r/, ‘’))  
sm.flush  
end  
end  
thread.run  
end  
end  
end
