1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

How to Send Email with Ruby

By

The following example uses the data stream to compose a message. The data stream is a way of appending data to the end of a Ruby file and reading it using the DATA IO object. The DATA IO object is just like any of the other IO objects such as STDIN, STDOUT or an object returned by File.open. Calling DATA.read will read everything after the __END__ keyword into a string.

 #!/usr/bin/env ruby
 require 'net/smtp'
 
 Net::SMTP.start(
   'mail.isp.com',
   25,
   'isp.com'
 ) do|smtp|
 
   message = DATA.read
   smtp.send_message(
     message,
     'username@isp.com',
     'username@other-isp.com'
   )
   smtp.finish
 
 end
 
 __END__
 From: username@isp.com
 To: username@other-isp.com
 Subject: This is a message sent by Ruby!
 
 This is the body of the message.
 It can be as long as you like.
 
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Using Data Streams

©2014 About.com. All rights reserved.