Ian Purton

Ian Purton’s Notes

Simple Email Send Function In Ruby

  • PublishedAugust 1st, 2006
  • Authorian.purton
  • Categoriesrails

If you need to send an e-mail in ruby, this little function works well.

def send_email(from, from_alias, to, to_alias, subject, message)

msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
 Subject: #{subject}

 #{message}
END_OF_MESSAGE

  Net::SMTP.start('localhost') do |smtp|
  smtp.send_message msg, from, to
  end

end

"Simple Email Send Function In Ruby" was published on August 1st, 2006 and is listed in rails.

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Simple Email Send Function In Ruby: 5 Comments

  1. What a coincidence, I juts wrote a similar program on our group blog…

  2. One more thing, which Wordpress plugin are you using for code on your blog?

  3. I am not using a wordpress plugin, I hand code the entries then I have some css to style them.

    Please feel free to look at the HTML and stylesheets.

  4. abhi

    i m trying to send mail with the given code of net::smtp.start().
    but it give me error like

    Address family not supported by protocol - socket(2)

    /opt/lampp/lib/ruby/1.8/net/protocol.rb:206:in `initialize’
    /opt/lampp/lib/ruby/1.8/net/protocol.rb:206:in `old_open’
    /opt/lampp/lib/ruby/1.8/timeout.rb:56:in `timeout’
    /opt/lampp/lib/ruby/1.8/timeout.rb:76:in `timeout’
    /opt/lampp/lib/ruby/1.8/net/protocol.rb:206:in `old_open’
    /opt/lampp/lib/ruby/1.8/net/smtp.rb:393:in `do_start’
    /opt/lampp/lib/ruby/1.8/net/smtp.rb:378:in `start’
    /opt/lampp/lib/ruby/1.8/net/smtp.rb:316:in `start’
    app/controllers/hello_controller.rb:15:in `deliverMail’

  5. Ian.. I followed the steps and it works.. Thanks..

    I have to add 2 things here.. We need to install postfix (not sure what to install in windows machine) and add require ‘net/smtp’ ..

    Hope the problem with Abi is she might not have installed postfix.

Leave Your Comment

(required)
(required)
 

Subtraction Wordpress Theme by Ian Purton