Using Ruby on Rails' ActionMailer on its own
06/08/29
If you ever need to create and send a bunch of dynamically created emails, here’s a nice little script to help you out. It should work on any server that has Ruby on Rails installed.
#
# my_mailer.rb
#
require 'rubygems'
require_gem 'actionmailer'
ActionMailer::Base.server_settings = {
:address => 'mail.myserver.com',
:port => 25,
:domain=> 'myserver.com',
:user_name=> 'admin',
:password=> 'mypassword',
:authentication=> :login
}
ActionMailer::Base.template_root = 'templates'
ActionMailer::Base.delivery_method = :smtp
class MyMailer < ActionMailer::Base
def database ( email_address )
date = Time.new.strftime('%m%d%Y')
@recipients = email_address
@subject = "[myserver.com] Database Dump : #{date}"
@from = "admin@myserver.com"
attachment "plain/text" do |a|
a.body = File.read('/path/to/database_dump.sql')
a.filename = "database_dump_#{date}.sql"
end
end
end
MyMailer.deliver_database ('someone@example.com')
You will also have to make sure that you have the proper directory structure set up for this to work.
./my_mailer.rb
./templates/my_mailer/database.rhtml
The body of your email will be created from the database.rhtml file. You can use any instance variables that you create in you database method just as in any Rails project.
Comments