1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sending Emails In Ruby

Discussion in 'Ruby on Rails' started by pradeep, Nov 16, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

    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:
    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:
    # 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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    I edited the emails for spam. Replace G4EF with go4expert.com if you wish to send us the emails.
     

Share This Page