Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send mail from a Ruby program?

Tags:

email

ruby

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?

like image 787
David Avatar asked Sep 12 '08 08:09

David


2 Answers

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.).

like image 109
ujh Avatar answered Nov 12 '22 09:11

ujh


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('[email protected]',['[email protected]', '[email protected]'],'test Email',"Hi there this is a test email hope you like it")
like image 21
pauliephonic Avatar answered Nov 12 '22 09:11

pauliephonic