Trouble with email attachments

116 views
Skip to first unread message

Ryan

unread,
Jun 18, 2008, 1:32:58 PM6/18/08
to Ruby on Rails: Talk
I'm running rails 2.0.2. I am trying to send an email using the
ActionMailer. The email is delivered when sending with or without an
attachment. My problem is that when I send an attachment, the body of
the email is blank. I only get an email with an attachment.

I've tried a couple different ways using the attachment method:

attachment :content_type => "image/jpeg",
:body => File.read("an-image.jpg")

attachment "application/pdf" do |a|
a.body = File.read("an-image-.jpg")
end

Both ways result in an email with an attachment, but my body content
is missing.

Jon Pospischil

unread,
Jul 3, 2008, 5:11:34 PM7/3/08
to Ruby on Rails: Talk
I am having exactly the same issue.

Looking at the output, it seems that sending with an attachment simply
overwrites the body section of the email...

Anyone know what the reason is?

(I am also running rails 2.0.2)



On Jun 18, 1:32 pm, Ryan <miller2...@comcast.net> wrote:
> I'm running rails 2.0.2.  I am trying to send anemailusing the
> ActionMailer.  Theemailis delivered when sending with or without an
> attachment.  My problem is that when I send an attachment, thebodyof
> theemailis blank.  I only get anemailwith an attachment.
>
> I've tried a couple different ways using the attachment method:
>
> attachment :content_type => "image/jpeg",
>         :body=> File.read("an-image.jpg")
>
> attachment "application/pdf" do |a|
>         a.body= File.read("an-image-.jpg")

Jon Pospischil

unread,
Jul 3, 2008, 5:58:12 PM7/3/08
to Ruby on Rails: Talk
For anyone else, the trick here is (from the Rails API Docs):

Implicit template rendering is not performed if any attachments or
parts have been added to the email. This means that you‘ll have to
manually add each part to the email and set the content type of the
email to multipart/alternative.

what this means is that we have to define the parts ourselves if we
add attachments:
attachment :content_type => 'application/vnd.ms-excel', :filename
=> 'invoice.xls', :body => invoice_body

part :content_type => "text/html", :body =>
render_message("invoice", :variable1 => value, :variable2 => value)

Hope this helps!
Jon
Reply all
Reply to author
Forward
0 new messages