469,366 Members | 2,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

How to attach file in an e-mail using ruby?

I have the following code, it is sending text message. I want to attach a file. How to do that?

require 'net/smtp' # plain mail
smtpclient = Net::SMTP::new( '' ) # create new object to send mail
the_email="From: ruby_using_smtp@webdevel.co.in\nTo:xxxx@bksys.co.i n\nSubject:Birthday \n\n"+"This is for demo" #Frame a mail
smtpclient.start # start the mail process
smtpclient.sendmail(the_email, 'ruby_using_smtp@webdevel.co.in','xxxx@bksys.co.in ') # send mail via ruby
smtpclient.finish # operation finished.
puts "Mail sent Successfully "
Jun 9 '10 #1
5 5596
303 Expert 100+
Here is an example.
Jun 9 '10 #2
It is sending mail properly with text message. It is not attach the file. In that program no syntax specified to attach mail.I have already tried this example. Any other simplest example ???
Jun 10 '10 #3
303 Expert 100+
There is a section near the bottom for adding attachments.
Expand|Select|Wrap|Line Numbers
  1. filename = "/tmp/test.txt"
  2. # Read a file and encode it into base64 format
  3. filecontent = File.read(filename)
  4. encodedcontent = [filecontent].pack("m")   # base64
  6. marker = "AUNIQUEMARKER"
  8. ...
  10. # Define the attachment section
  11. part3 =<<EOF
  12. Content-Type: multipart/mixed; name=\"#{filename}\"
  13. Content-Transfer-Encoding:base64
  14. Content-Disposition: attachment; filename="#{filename}"
  16. #{encodedcontent}
  17. --#{marker}--
  18. EOF
You then need to add part3 to your email.
Jun 10 '10 #4
Could you able to understand my question?

In sendmail function, there is no option to attach file. U can configure the file attachment details properly. How will you embed this ?
Jun 11 '10 #5
303 Expert 100+
The attachment is part of the email. In sendmail, you send the complete email which already has attachments added.
Jun 11 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by brianwmunz | last post: by
2 posts views Thread by calebmichaud | last post: by
3 posts views Thread by =?Utf-8?B?YmVueQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.