Sending Emails In Ruby

pradeep's Avatar author of Sending Emails In Ruby
This is an article on Sending Emails In Ruby in Ruby on Rails.
Rated 5.00 By 3 users

Sending Emails In Ruby



To send emails using ruby you can use the Net::SMTP library. In the following example from_addr is a String that represents the source email address to_addr is also a String. It can also be an Array of strings if you want to send the email to multiple recepients.

Code: Ruby
require 'net/smtp'
  Net::SMTP.start('smtp.example.com', 25) do |smtp|
    smtp.open_message_stream('from_addr', [to_addr]) do |f|
      f.puts 'From: shabbir@g4ef.com'
      f.puts 'To: pradeep@g4ef.com'
      f.puts 'Subject: test message'
      f.puts 'This is a test message.'
    end
  end

SMTP Authentication



If you need to authenticate before sending mails then Net::SMTP class supports three authentication schemes; PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])

Code: Ruby
# PLAIN
  Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain','Your Account', 'Your Password', :plain)
 
  # LOGIN
  Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain','Your Account', 'Your Password', :login)
 
  # CRAM MD5
  Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain','Your Account', 'Your Password', :cram_md5)


Read more about Net::SMTP here.
Go4Expert Founder
16Nov2007,19:04   #2
shabbir's Avatar
I edited the emails for spam. Replace G4EF with go4expert.com if you wish to send us the emails.