# Email attachment corrupted

**URL:** <https://www.ruby-forum.com/t/email-attachment-corrupted/153881>\
**Category:** Rails\
**Created:** [November 22, 2008, 11:32am UTC](https://www.ruby-forum.com/t/email-attachment-corrupted/153881 "2008-11-22T11:32:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![up1notch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@up1notch](https://www.ruby-forum.com/u/up1notch)\
**Post date:** [November 22, 2008, 11:32am UTC](https://www.ruby-forum.com/t/email-attachment-corrupted/153881/1 "2008-11-22T11:32:13Z")

</div>

Having some problem with sending email with attachment:

I am able to send an email without attachment via gmail server.

However, when i tried to send an image attachment, it is corrupted at  
the recipient mailbox; coz the mail received:  
(1) Has the attachment, but not able to open the image file.  
(2) The attachment image file size is much smaller.  
(3) The message in the email body is missing (which was there if i  
send without the attachment portion).

Below is the part of my model code:

============== models/event\_mailer.rb ======================  
class EventMailer \< ActionMailer::Base  
def email\_friend(recipient, sender\_name, event)  
subject ‘Check out this great event’  
from ‘removed\_email\_address@domain.invalid’  
recipients recipient  
content\_type ‘text/html’  
body “sender\_name” =\> sender\_name, “event” =\> event

```
attachment :context_type => 'image/png',
            :body => File.read('public/images/rails.png')

```

end  
end

BTW, i am using rails 2.1.2, ruby 1.8.6

What did i miss out? Any suggestions will be greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![up1notch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@up1notch](https://www.ruby-forum.com/u/up1notch)\
**Post date:** [November 23, 2008, 9:32am UTC](https://www.ruby-forum.com/t/email-attachment-corrupted/153881/2 "2008-11-23T09:32:44Z")

</div>

Found the solutions:

1. For the error where the email body was not received by the recipient,  
it was because of the naming of the view file has the wrong extension. I  
had to changed it to “email\_friend.erb”, instead of  
"email\_friend.html.erb.

2. As for the corrupted image file, it was because it was not opened as  
a binary file. I learn of this from:

[http://www.ruby-forum.com/topic/85591#156951](http://www.ruby-forum.com/topic/85591#156951)
