require 'net/smtp' begin require 'mailfactory' rescue LoadError puts "You need to install the mailfactory gem to use Merb::Mailer" MERB_LOGGER.fatal "You need to install the mailfactory gem to use Merb::Mailer" end module Merb class Mailer shared_accessor :config def sendmail sendmail = IO.popen("sendmail #{@mail.to}", 'w+') sendmail.puts @mail.to_s sendmail.close end # :plain, :login, or :cram_md5 def net_smtp Net::SMTP.start(config[:host], config[:port].to_i, config[:domain], config[:user], config[:pass], (config[:auth].intern||:plain)) { |smtp| smtp.send_message(@mail.to_s, @mail.from, @mail.to) } end def deliver! config == :sendmail ? sendmail : net_smtp end def initialize(o={}) self.config = :sendmail if config.nil? @mail = returning MailFactory.new() do |m| m.to = o[:to] m.from = o[:from] m.subject = o[:subject] m.text = o[:body] m.html = o[:html] if o[:html] end end end end =begin m = Merb::Mailer.new :to => 'foo@bar.com', :from => 'bar@foo.com', :subject => 'Welcome to whatever!', :body => partial(:sometemplate) m.deliver! Merb::Mailer.config = { :host=>'smtp.yourserver.com', :port=>'25', :user=>'user', :pass=>'pass', :auth=>:plain # :plain, :login, or :cram_md5, default :plain } =end