I am experiencing problems with ActionMailer, AWS SES and attachments (not inline) in Rails 3.2.13. When viewing the email in Gmail, it's displayed without any problems. When viewing it in Apple Mail or Sparrow, either attachment is missing and the body is displayed or vice-versa.
The headers for the email are as follows:
Return-Path: 0000013e79305031-08aae59a-f7a0-41c9-8bd9-076c46caf100-000000@amazonses.com
Date: Mon, 6 May 2013 09:33:29 +0000
From: Test Account <test@test.com>
To: test@localhost.com
Message-ID: <0000013e79305031-46caf100-000000@email.amazonses.com>
Subject: TEST
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_518778e85f16a_56844ceff0898ed";
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SES-Outgoing: 129.255.144.108
If the body is shown, the attachment can be found at the end of the source though:
----==_mimepart_518778e85f16a_56844ceff0898ed
Date: Mon, 06 May 2013 09:33:28 +0000
Mime-Version: 1.0
Content-Type: application/pdf;
charset=UTF-8;
filename="Test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="Test.pdf"
Content-ID: <518778e829b75_56844ceff089553@ip-10-36-149-153.mail>
...
I have found similar errors on Stack Overflow (Problem sending multipart mail using ActionMailer) but the suggested solutions did not work - especially the solution at the bottom is very interesting, as it focuses on a solution without inline attachments (https://stackoverflow.com/a/16334096/1016269). I get an error from AWS though saying that the start boundary is not set - I tried to explicitly set it, but no luck so far.
This is the original code I use for sending the email:
def test_email
attachments["Test.pdf"] = File.read(Rails.root.join("data", "test.pdf"))
mail(to: recipient, subject: "Test Email")
end
This is the code based on the solutions suggested in the link I posted:
def test_email
mixed = mail(to: recipient, subject: "Test Email")
mixed.add_part(
Mail::Part.new do
content_type 'multipart/alternative'
# THE ODD BIT vv
mixed.parts.reverse!.delete_if {|p| add_part p }
end
)
mixed.content_type 'multipart/mixed'
mixed.header['content-type'].parameters[:boundary] = mixed.body.boundary
attachments["Test.pdf"] = File.read(Rails.root.join("data", "test.pdf"))
end
The response is:
AWS::SES::ResponseError: InvalidParameterValue - Missing start boundary
Any ideas what could cause this issue?
Thanks a lot