Skip to content

Instantly share code, notes, and snippets.

@michelson
Created August 3, 2015 20:44
Show Gist options
  • Save michelson/1658ad3c79a888f55250 to your computer and use it in GitHub Desktop.
Save michelson/1658ad3c79a888f55250 to your computer and use it in GitHub Desktop.
mail catcher for rails env
if Rails.env.test?
class MailCatcher
def self.create(opts)
File.open("#{Rails.root}/tmp/mailcatcher/mail-#{Time.now.to_i}.html", 'w') do |f|
f.write(opts[:body].raw_source)
end
end
end
module ActionMailerCatcher
module InstanceMethods
def send_with_mail_catch!(opts={})
send_without_mail_catch!(opts)
logger.info "STORING MAIL IN MONGO!"
MailCatcher.create(
:from=> @_message.from ,
:to=> @_message.to,
:body=> @_message.body ,
:content_type=> @_message.content_type ,
:subject=> @_message.subject,
:reply_to=> @_message.reply_to
) if MailCatcher
end
end
def self.included(receiver)
receiver.send :include, InstanceMethods
receiver.class_eval do
alias_method_chain :send!, :mail_catch
end
end
end
BaseMailer.send :include, ActionMailerCatcher
end
@michelson
Copy link
Author

create file in:

config/initializers

create folder in tmp:

tmp/mailcatcher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment