# Trouble with email attachments

**URL:** <https://www.ruby-forum.com/t/trouble-with-email-attachments/140810>\
**Category:** Rails\
**Created:** [June 18, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/trouble-with-email-attachments/140810 "2008-06-18T20:04:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 18, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/trouble-with-email-attachments/140810/1 "2008-06-18T20:04:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [July 3, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/trouble-with-email-attachments/140810/2 "2008-07-03T23:12:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [July 3, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/trouble-with-email-attachments/140810/3 "2008-07-03T23:58:54Z")

</div>

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
