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-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' recipients ''jim-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' 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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Jan-21 23:04 UTC
Re: Trouble sending email image attachment with rails
On Jan 21, 2011, at 2:22 PM, Jim Burgess wrote:> 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-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' > recipients ''jim-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' > subject ''New message'' > 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.> 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 via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' recipients ''jim-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.