1. Home
  2. Computing & Technology
  3. Ruby

How to Send Email with Ruby

By Amanda & Michael Morin, About.com

5 of 5

Using Heredocs

A "heredoc" is a way of making multi-line strings easy to type. It's possible to form multi-line strings with the string concatenation operator (such as "string1" + "string2"), but this operation is slow and inefficient and it adds extra syntax to every line as well. The heredoc syntax starts with something that looks like this: <<END. This means every line after this one will be part of a string, not a Ruby program. END on a line by itself will end the multi-line string. You can use any other identifier instead of END if you want, but END is the common convention.

#!/usr/bin/env ruby
require 'net/smtp'

Net::SMTP.start(
  'mail.isp.com',
  25,
  'isp.com'
) do|smtp|

  message = <<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.
END

  smtp.send_message(
    message,
    'username@isp.com',
    'username@other-isp.com'
  )
  smtp.finish

end
Explore Ruby
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Tasks & Scripts
  5. Using Heredocs

©2009 About.com, a part of The New York Times Company.

All rights reserved.