ActionMailer, Attachments, and Hotmail
Last week I had the need to use ActionMailer to send email messages with attachments. As with most things Rails, doing so seemed pretty straight forward and my initial implementation appeared to go off without a hitch. My application would send the messages to an email address that I generally check using Thunderbird and each message would arrive with attachment in tow. The attachment would be named something generic like, "ATT001," would have the proper MIME type, and could be opened using an appropriate application. At some point, however, I changed to recipient address to send the email message to a Hotmail account. I checked the account using the on-line, Hotmail interface. The message was received and, in the Inbox listing, appeared to have an attachment, but when I opened the message there was no sign of the attachment at all. After a few more tests I came to the conclusion that Hotmail's failure to present the attachment was actually a failure on my part to construct the email properly in my Rails code. My code looked something like this:
def notification(applicant)
subject 'Employment Application'
recipients 'myuname@hotmail.com'
from applicant.email
body :applicant => applicant
attachment :content_type => applicant.resume_content_type,
:body => File.open(applicant.resume.path, 'rb') { |f| f.read }
end
I should mention that my platform was Rails 2.3.4 and that I was using the Paperclip plugin to upload the files to be attached to the email messages.
So, first things first, I decided to Google around a bit to see if this was a common problem. As you might guess, since I have decided to write about this experience, I really didn't find much; just one post on apidock.com. Here's a link to the post so you can read it yourself:
http://apidock.com/rails/ActionMailer/Base#673-Content-type-for-emails-with-attachments
The post essentially said that setting the content-type for the message to multipart/alternative would cause Hotmail to ignore attachments and that I should use multipart/mixed instead. Well, as you can see, I was not explicitly setting the content-type for the message (and I had only provided a single template for rendering the message). Still, I took the poster's advice and explicitly set the content-type for the message to multipart/mixed. Alas, the problem persisted.
After searching and searching the web for help and trying many variations on the code I realized that I was fast approaching my deadline for having the application done and decided I better move on and return to the problem later. Before moving on I decided that I did not like the generic names that were being assigned to the attachments and so I changed the code to ensure a more meaningful name. On a whim, I decided to try sending to the Hotmail account again and guess what! I worked! Apparently, not explicitly assigning a name to the attachment prevents Hotmail from allowing access to the attachment.
For good measure, I decided to try one more test: Gmail. It turned out to be a good decision, as when using the Gmail web interface to access an email message generate by my code, the message appeared with an empty body and two attachments. The first attachment was the intended message body and the second attachment was the expected attachment. The problem was easily rememdied by removing the line of code that set the message's content-type to multipart/mixed. My final code looked something like this:
def notification(applicant)
subject 'Employment Application'
recipients 'myuname@hotmail.com'
from applicant.email
body :applicant => applicant
attachment :content_type => applicant.resume_content_type,
:filename => "resume",
:body => File.open(applicant.resume.path, 'rb') { |f| f.read }
end


