# How to send a html email with attachment

**URL:** <https://www.ruby-forum.com/t/how-to-send-a-html-email-with-attachment/178027>\
**Category:** Rails\
**Created:** [October 28, 2009, 8:23am UTC](https://www.ruby-forum.com/t/how-to-send-a-html-email-with-attachment/178027 "2009-10-28T08:23:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrytie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@terrytie](https://www.ruby-forum.com/u/terrytie)\
**Post date:** [October 28, 2009, 8:23am UTC](https://www.ruby-forum.com/t/how-to-send-a-html-email-with-attachment/178027/1 "2009-10-28T08:23:57Z")

</div>

Hi guys:  
I wanna send a html format email with a attachment.  
What I do is like:  
model:  
def weekly\_report  
address =  
WeeklyReportEmail.find\_all\_by\_validate(true).collect(&:address).to\_sentence(:last\_word\_connector  
=\> ‘,’, :two\_words\_connector =\> ‘,’)  
@recipients = address  
@from = “Chronos weekly report [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
@subject = “[Chronos] Weekly report”  
@sent\_on = Time.zone.now  
@body[:projects] = Project.all

```
attachment :content_type => "image/png",
  :body => File.read(RAILS_ROOT + "/public/images/cancel.png")

```

end

views: just a erb file.

and the way I sent the mail is like below:

mail = MailReport.create\_weekly\_report  
mail.set\_content\_type(“text/html”)  
MailReport.deliver(mail)

If I sent it like that, I can not get the attachment insted of getting  
some messy code.

I tried sent it like MailReport.deliver\_weekly\_report.  
If so, the email will not be a html format email.

So what should I do, if I wanna send a html email with attachment.

---

<div class="post-metadata">

**Author:** ![terrytie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@terrytie](https://www.ruby-forum.com/u/terrytie)\
**Post date:** [October 28, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/how-to-send-a-html-email-with-attachment/178027/2 "2009-10-28T14:20:09Z")

</div>

something like:

This is example procedure - assuming that i want to send few pdfs  
attached with a html email - this is ordinary mailer method

class ExampleNotify \< ActionMailer::Base

def example(\_user, \_attachments)  
recipients \_user.email  
from FROM  
subject ‘example’  
content\_type “text/html; charset=utf-8”

```
body render_message('example')

for a in _attachments
  attachment :content_type => "application/pdf", :body => 

```

File.read(File.join(RAILS\_ROOT,DOCUMENT\_DIR,a)), :filename=\>a  
end  
end

end

---

<div class="post-metadata">

**Author:** ![terrytie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@terrytie](https://www.ruby-forum.com/u/terrytie)\
**Post date:** [October 29, 2009, 8:07am UTC](https://www.ruby-forum.com/t/how-to-send-a-html-email-with-attachment/178027/3 "2009-10-29T08:07:37Z")

</div>

Hi

U can try this.  
User\_notifier:  
def method\_name(email)  
@recipients = email  
@content\_type = “text/html”  
attachment :content\_type =\> “application/octet-stream”,  
:filename =\> “User Manual.pdf”,  
:body =\> File.read(“public/User Manual.pdf”)

end

Thanks  
Seenu

On Wed, Oct 28, 2009 at 6:50 PM, Frantisek P. \<
