Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Ruby on Rails (http://www.go4expert.com/articles/ruby/)
-   -   Sending Emails In Ruby (http://www.go4expert.com/articles/sending-emails-ruby-t7323/)

pradeep 16Nov2007 11:03

Sending Emails In Ruby
 

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.

shabbir 16Nov2007 19:04

Re: Sending Emails In Ruby
 
I edited the emails for spam. Replace G4EF with go4expert.com if you wish to send us the emails.


All times are GMT +5.5. The time now is 06:00.