# Attachment in mail

**URL:** <https://www.ruby-forum.com/t/attachment-in-mail/141969>\
**Category:** Rails\
**Created:** [July 2, 2008, 6:45am UTC](https://www.ruby-forum.com/t/attachment-in-mail/141969 "2008-07-02T06:45:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [July 2, 2008, 6:45am UTC](https://www.ruby-forum.com/t/attachment-in-mail/141969/1 "2008-07-02T06:45:38Z")

</div>

How to do the attachment in mail like the yahoo and gmail does?

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [July 2, 2008, 10:49am UTC](https://www.ruby-forum.com/t/attachment-in-mail/141969/2 "2008-07-02T10:49:51Z")

</div>

\<% form\_tag(depot\_help\_desk\_path, :method =\> :post, :multipart =\>  
true) do %\>

 Helpdesk formulier 
1. Ik werk met de volgende browser: \<%= radio\_button "helpdesk", "browser", "Internet Explorer" %\>Internet Explorer  
 \<%= radio\_button "helpdesk", "browser", "Safari" %\>Safari  
 \<%= radio\_button "helpdesk", "browser", "Mozilla Firefox" %\>Mozilla Firefox  
  

```
  <div style="width: 300 px;">2. Ik werk met de volgende computer:</

```

div\>  
\<%= radio\_button “helpdesk”, “os”, “windows” %\>Windows  
  
\<%= radio\_button “helpdesk”, “os”, “mac” %\>Mac  
  
\<%= radio\_button “helpdesk”, “os”, “linux” %\>Linux

```
  <div style="width: 300 px;">3. Mijn gebruikersnaam is:</div>
  <label id="label_right"><%= text_field 'helpdesk',

```

‘user\_name’, :class =\> “txtfield”, :size =\> 20 %\>

```
  <div style="width: 300 px;">4. Mijn e-mail adres is:</div>
  <label id="label_right"><%= text_field 'helpdesk',

```

‘email’, :class =\> “txtfield”, :size =\> 20 %\>

```
  <div style="width: 400 px;">5. Het probleem is op de volgende

```

datum opgetreden:

  
\<%= text\_field ‘helpdesk’, ‘datum’, :class  
=\> “txtfield”, :size =\> 20 %\>  
  

```
  <div style="width: 300 px;">6. De URL waar het probleem zich

```

voordoet is:

  
\<%= text\_field ‘helpdesk’, ‘url’, :class  
=\> “txtfield”, :size =\> 20 %\>  
  

```
  <div style="width: 300 px;">7. Bestand toevoegen:</div>
  <%= file_field 'helpdesk', 'file', :class => "txtfield", :size =>

```

20 %\>

```
  <div style="width: 300 px;">8. Het originele bestand heb ik

```

bijgevoegd:  
\<%= radio\_button “helpdesk”, “bestand”, “ja” %\>ja  
  
\<%= radio\_button “helpdesk”, “bestand”, “nee” %\>nee

```
  <div style="width: 550 px;">9. Graag zo volledig mogelijk

```

omschrijven (stapsgewijs) wat het probleem / je vraag is.  
\<%= text\_area ‘helpdesk’, ‘text’, :cols =\>  
“70”, :rows =\> 20 %\>

```
  <div style="width: 550 px;">Hartelijk dank voor het invullen van

```

bovenstaand formulier. Klik op versturen en wij streven ernaar binnen  
5 werkdagen te reageren.

```
  <div><%= submit_tag "versturen", :onClick => "", :id => 'disable-

```

me’ %\>

 \<% end %\>

def ticket\_with\_attachment(browser, os, email, user\_name, datum,  
url, file, bestand, text, sent\_at = Time.now )  
@subject = 'Helpdesk verzoek: ’ + user\_name  
@body = { :browser =\> browser, :os =\> os, :user\_name =\>  
user\_name, :email =\> email, :datum =\> datum, :url =\> url, :bestand =\>  
bestand, :text =\> text }  
@recipients = “removed\_email\_address@domain.invalid”  
@from = “removed\_email\_address@domain.invalid”  
@sent\_on = sent\_at  
unless file.blank?  
attachment :body =\> file.read, :filename =\>  
file.original\_filename  
end  
end

That’s working code from one of our apps. Not very nice looking, but  
should do.  
Important parts are the file\_field in the form (all others are just  
text or radio buttons)  
and those lines in the ActionMailer delivery method:

unless file.blank?  
attachment :body =\> file.read, :filename =\> file.original\_filename  
end

which will attach the file to the mail

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [July 2, 2008, 11:25am UTC](https://www.ruby-forum.com/t/attachment-in-mail/141969/3 "2008-07-02T11:25:37Z")

</div>

Thank you Muller I will try it and let you know the oucome.

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [July 2, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/attachment-in-mail/141969/4 "2008-07-02T12:35:53Z")

</div>

I read your code and do something like this in my actionmailer class  
def contact(recipient, subject, message,user\_id,attachment1=nil,  
sent\_at = Time.now)  
@body[‘user\_id’]= user\_id

```
  @subject = subject
  @recipients = recipient
  @from = 'removed_email_address@domain.invalid'
  @sent_on = sent_at
@body["title"] = 'You have new Job Application'
   @body["message"] = message
  unless attachment1.nil?
   attachment "application/octet-stream" do |a|
    a.body=attachment1.read
    a.filename=attachment1.original_filename
   end
  end
   attachment :content_type=>"image/jpeg",
     :body=>File.read("#{RAILS_ROOT}/public/images/

```

afterpinkslip.jpg"),  
:filename=\>File.basename("#{RAILS\_ROOT}/public/images/  
afterpinkslip.jpg")  
end

and it works beautifully. Thank you once again.
