Action Mailer tips

Posted by tobi — 05:47 AM Mar 15

OK so today I confused the hell out of my partner who does all the shipping for snowdevil.

After testing a new revision of the software I he called me and asked “Hey did you really just order 3 snowboards??!”. My local test have been dispatching emails looking just like the real thing.

Here is a simple which keeps emails going but doesn’t confuse the hell out of other people (added to your development.rb) :


ActionMailer::Base.perform_deliveries              = true
ActionMailer::Base.delivery_method                 = :fixed_email

class ActionMailer::Base

  def self.perform_delivery_fixed_email(mail)
    mail.to = 'your.own@email-address.com'
    mail.subject = '[DEV] ' + mail.subject
    Net::SMTP.start(
           server_settings[:address], 
           server_settings[:port], 
           server_settings[:domain], 
           server_settings[:user_name], 
           server_settings[:password], 
           server_settings[:authentication]) do |smtp|
                 smtp.sendmail(mail.encoded, mail.from, mail.destinations)
           end
  end

end

Now i get all the emails the system sends myself. Thats much better…

Comments

  • finn 28 Oct 12:50

    does this continue to work with more recent versions of rails? (the 0.14.* series of 1.0 release candidates.)

  • finn 17 Jan 15:46

    Here’s the version that worked for me. Note that I am using perform_delivery_smtp to send with my default SMTP settings. Replace that with perform_delivery_sendmail if you want to use the local sendmail instead.

    config.action_mailer.perform_deliveries = true
    config.action_mailer.delivery_method = :fixed_email
    class ActionMailer::Base
      def perform_delivery_fixed_email(mail)
        dest = mail.destinations
        mail.to = your.own@email-address.com'
        mail.cc = ''
        mail.bcc = ''
        mail.subject = '[DEV] ' + mail.subject
        mail.body = "[ORIGINAL RECIPIENTS] \n#{dest}\n\n" + mail.body
        perform_delivery_smtp(mail)
      end
    end

Commenting are now closed…