Choice Email Setup with Merb

Good day. Here I describe a choice email setup with Merb. You should be using this setup too.

SMTP or Sendmail?

Use Google Apps SMTP to send your mail from your merb application. Sendmail has three serious limitations.

#1: Sendmail is specific to your server. When you move servers, you will have to reinstall sendmail. Your app should be as mobile and self-contained as possible. Merb does a great job of this with its bundling of gems, and you should continue that trend.

#2: Emails sent via sendmail on your server are sent with extra hidden data that you don't need. This isn't a problem when you are sending regular emails, but is is a problem when you start sending text messages via email and want your web application to receive the user's text reply back. I was using sendmail with Flossed Today to send out text messages, but subscribers using AT&T (and I imagine other cell providers as well), were unable to reply back to the message. The reply was deemed too large for a text message by AT&T's sms protocols. I determined this was because it was sending all the hidden sendmail data along with the text message. When I switched to Google Apps SMTP I no longer experienced this issue.

#3: Google Apps keeps a nice sent mail history. Sendmail's logs are just not the same as being able to search in Google Apps.

Won't SMTP's connection time lock up my app's response/request cycle?

No, it doesn't have to. Use Merb's run_later method and you are golden.

Many argue that using SMTP is a bad idea because sendmail makes your web app appear to run faster. They are right. With sendmail your application passes the email request to your local sendmail command. It doesn't have far to go so it is fast. With Google Apps SMTP your app has to connect to the remote SMTP server. It has a further distance to travel, and the connection time typically lasts 2-5 seconds. During this time your web app appears to be thinking to your visitor and they are left waiting an extra 2-5 seconds on top of whatever load time speeds your site already does.

Thankfully, Merb has a run_later method that allows you to pass this email connection time off to a separate thread. Your application then loads speedily, and your user doesn't have to wait an extra 2-5 seconds. Now you can have the speed of sendmail and the advantages of Google Apps.

Setup

Signup for a Google Apps account - here, and finish the process verifying that you can send/receive emails from your new Google Apps account.

Next setup your app to handle SMTP.

Sending emails

In your application use the run_later method in conjunction

run_later

Merb's run_later method works out of the box with thin and mongrel, but has a bug with passenger and Merb 1.0.x. Create the the file '/merb/hacks/run_later.rb' in your application with the following code to fix it.

Then add the following to your init.rb file

send_mail

Setup your mailers in merb like usual and use in your controller surrounded by run_later. Here is an example sending a confirmation email after a user signs up.

Conclusion

Linux's sendmail can be great, but when it comes to features and ease of configuration Google Apps can't be beat. Couple it with Merb's run_later method, and you can create really nice email functionality within your app. Plus, it is more scaleable since all the configuration is in the app, and connects remotely to Google Apps.

You should follow me on twitter here