472,122 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

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( '192.168.1.1' ) # 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 5765
improvcornartist
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
improvcornartist
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
  5.  
  6. marker = "AUNIQUEMARKER"
  7.  
  8. ...
  9.  
  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}"
  15.  
  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
improvcornartist
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.