Forum: Ruby on Rails Trouble sending email image attachment with rails

Posted by Jim Burgess (jim_in_germany)
on 2011-01-21 23:22
Hi,

I'm trying to use ActionMailer to send an image as an attachment to an
email. Here's my code:

class ContactMailer < ActionMailer::Base
  def message(sent_at = Time.now)
    from 'bob@example.com'
    recipients 'jim@example.com'
    subject 'New message'
    sent_on sent_at
    body {}
    attachment :content_type => "image/jpeg", :body =>
File.read("public/images/rails.png")
  end
end

This works, in so far as the email is delivered okay (obviously with
different addresses), but the image attached to the email seems to be
corrupted.

It doesn't display in my mail client and when I download it and try to
open it with a suitable program I get the message that the file is
either too large or corrupted.

I tried replacing the image with a pdf as follows:
attachment :content_type => "application/pdf", :body =>
File.read("public/images/test.pdf")
and this works fine.

Can anybody give me a clue as to what I'm doing wrong?

Thanks in advance.
Posted by Philip Hallstrom (Guest)
on 2011-01-22 00:05
(Received via mailing list)
On Jan 21, 2011, at 2:22 PM, Jim Burgess wrote:

>    sent_on sent_at
>    body {}
>    attachment :content_type => "image/jpeg", :body =>
> File.read("public/images/rails.png")

Content type is JPEG.  File you're reading is PNG.
Posted by Jim Burgess (jim_in_germany)
on 2011-01-22 08:08
> Content type is JPEG.  File you're reading is PNG.

Yeah, I noticed this too, but according to the book I'm reading this 
should work.

I tried sending a jpg as an attachment (instead of a png), but with the 
same result. I also tried:
attachment :content_type => "image/png", :body => 
File.read("public/images/rails.png")

but again, in the mail the image displays as broken and when I download 
it I cannot open it.

Could I be doing anything else wrong?

Thanks very much.
Posted by Jim Burgess (jim_in_germany)
on 2011-01-23 11:16
Just an update:

I've checked through the documentation for ActionMailer.
Reusing the example they list in their documentation I now have:

def message(sent_at = Time.now)
  from 'bob@example.com'
  recipients 'jim@example.com'
  subject 'New message'
  sent_on sent_at
  attachment "image/jpg" do |a|
    a.body = File.read("public/images/test.jpg")
    a.filename = "hello.jpg"
  end
end

test.jpg exists in the folder public/images and is definitely a jpg.

In my previous post I had configured my app to use the mail transfer
agent on my production server (presumably sendmail).

To eliminate this as being the problematic link in the chain I also
configured my rails app to use my gmail account to send the email.

Again the mail generated in my rails app gets delivered, but with the
image attachment I get exactly the same result (jpg doesn't display
inline and when I download it and try to open it I get the message "File
corrupt).

Could anyone give me a clue as to what I am doing wrong?

I'm using rails 2.3.8

Thanks very much in advance.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.