Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Tuesday, May 11, 2010

Sending email with attachment in Ruby using Ruport gem

An example of sending email using Ruport. Sends email to 2 recipients. This example also includes attachment. I'm using this to send ruby script results and images

To install ruport via rubygems:
sudo gem install ruport
Check to see if it installed properly:
ruby -rubygems -e "require 'ruport'; puts Ruport::VERSION"

To use:
require 'ruport'
require 'ruport/util'

r = Ruport::Report.new
r.add_mailer :default,
             :host => "mail.domain.com",
             :address => "email@domain.com"

recipients = ["to1@domain.com","to2@domain.com"]
recipients.each do |recipient|
  r.send_to(recipient) do |mail|
    mail.subject = "Test Email"
    mail.attach "test_file.txt"
    mail.attach "stylesheet.xsl"
    mail.text = "This is an email with attachments"
  end
end