10

I want to send email from a Ruby application. Is there a call in the core language to do this or is there a library I should use? What's the best way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David
  • 14,047
  • 24
  • 80
  • 101

6 Answers6

6

If you don't want to use ActionMailer you can use Net::SMTP (for the actual sending) together with tmail for easily creating emails (with multiple parts, etc.).

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
ujh
  • 4,023
  • 3
  • 27
  • 31
1
require 'net/smtp'
SMTP_SERVER = 'mailserver01' #change to your server

def send_emails(sender_address, recipients, subject, message_body)
    recipients.each do |recipient_address|
        message_header =''
        message_header << "From: <#{sender_address}>\r\n"
        message_header << "To: <#{recipient_address}>\r\n"
        message_header << "Subject: #{subject}\r\n"
        message_header << "Date: " + Time.now.to_s + "\r\n"
        message = message_header + "\r\n" + message_body + "\r\n"
        Net::SMTP.start(SMTP_SERVER, 25) do |smtp|
            smtp.send_message message, sender_address, recipient_address
        end
    end
end
send_emails('sender@domain.com',['recip1@test.com', 'recip2@other.com'],'test Email',"Hi there this is a test email hope you like it")
pauliephonic
  • 2,127
  • 1
  • 13
  • 9
0

I know this is a late answer to this, but this was just released:

http://adam.blog.heroku.com/past/2008/11/2/pony_the_express_way_to_send_email_from_ruby/

Might be useful.

jonnii
  • 28,019
  • 8
  • 80
  • 108
0

I use the Net::SMTP library

Pat
  • 36,282
  • 18
  • 72
  • 87
0

RubyMail is an email handling library for Ruby.

ChrisInEdmonton
  • 4,470
  • 5
  • 33
  • 48
-1

You might also consider taking a look at the ActionMailer component that ships as part of, but is not dependent on Rails.

Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127