Receiving emails with ActionMailer on Windows

Published by Dave Stephens on December 12th, 2006.

PS click here to get straight to the answer

There doesn’t seem to be any documentation on getting incoming emails into a Rails app running on Windows. I’ve tried every combination of search keywords I can think of, having just learnt a lesson on how they can determine your success in finding an elegant solution that someone put up recently. So if you know of a good guide somewhere else, please kindly post a comment.

The closest I found was this entry in the Ruby on Rails wiki. Virtually the entire page is about working with UNIXy MTAs and, to my surprise and horror, none of them has been satisfactorily ported over to Windows! There is a little section on “Receiving with ActionMailer on windows” but it involved manually saving the email in a file and sending the file to the Mailer as input, not the best thing to have our customers do!

Long story short, I eventually found two decent-looking free SMTP servers for Windows:

  1. XMail - Open Source
  2. MailEnable Standard Edition - Freeware

Champions of Open Source that we are, XMail just had to get first dibs. It turned out MailEnable’s scripting didn’t look very easy anyway.

So here’s how you receive email in a Rails app running in Windows:

First of all, install XMail using this forum post. The XMail Wizard step is only for setting the admin username/password, so if you cannot open the rar file that it comes in, just use xmcrypt to encrypt your admin password:

c:mailrootbinxmcrypt <password>

and write this in the ctrlaccounts.tab file:

"<username>"    "<xmcrypted password>"

BE SURE to use quotes around each token, a tab between the two, and a newline at the end of the line!!

Create a domain for your app in XMail Administrator, as per the instructions, and in this domain, add the accounts that correspond to your mailers, then for each of these accounts, uncheck the Store mail locally checkbox. Your screen should now look like this:

xmail_administrator.png

Now for each of these accounts, click into the Advanced tab and enter the following into the mailproc.tab settings:

"external","0","60","ruby","<RAILS_ROOT>/script/runner","-e","production","<YourMailer>.receive(File.read(ARGV[2][4..-1]).sub(/A.*?<<MAIL-DATA>>/im, '').lstrip)","$(FILE)"

substituting your own RAILS_ROOT and the mailer class that corresponds to the account. As you can see, there a little bit of massaging needed on both the file name and the email content that XMail provides.

Set the MX record for the domain to point to the XMail host and your hungry mailers will be fed!

Big thanks to Davide Libenzi for creating the amazing XMail, which combines SMTP, POP3, IMAP, Finger, an admin server for windows in one tight package, as well as to BooT on the XMail forum for the instructions that probably saved me days of suffering.

Leave a Reply