lib/snails/mailer.rb in snails-0.0.7 vs lib/snails/mailer.rb in snails-0.0.8
- old
+ new
@@ -1,12 +1,15 @@
require 'erb'
require 'tilt/erb'
require 'tuktuk'
+require 'snails'
module Snails
class Mailer
+ include Snails::SimpleFormat
+ Time.include(Snails::RelativeTime) unless Time.instance_methods.include?(:relative)
@queue = :emails
def initialize(opts)
cwd = Pathname.new(Dir.pwd)
@@ -31,21 +34,25 @@
define_singleton_method(name) do |*args|
instance_exec(*args, &block)
end
end
+ def helpers(&block)
+ instance_eval(&block)
+ end
+
def perform(method, *args)
send(method, *args)
end
# e.g. Notifier.queue(:some_notification, @project, "arg1")
def queue(method, obj, *args)
return unless Snails.env.production?
Resque.enqueue(self, method, obj.id, *args)
end
- def send_error(to: @from_email, err:, env:, params: {}})
+ def send_error(to: @from_email, err:, env:, params: {})
@exception, @env, @params = err, env, params
@url = "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']}"
subject = "#{@url} :: (#{@exception.class}) \"#{@exception.message}\""
content = %{
@@ -86,25 +93,28 @@
def logger
@logger ||= Logger.new(@logfile)
end
- def send_email(to:, subject:, body:, from: nil, html: nil)
+ def send_email(from: nil, to:, subject:, body: nil, template: nil, html_body: nil, html_template: nil)
raise "No recipient given for mail: #{subject}!" if to.blank?
message = {
to: to,
from: from || @from_email,
- subject: @base_subject + subject,
- body: body.is_a?(Symbol) ? render(body) : body
+ subject: @base_subject + subject
}
- if html
- message[:html_body] = html.is_a?(Symbol) ? render(html) : html
+ if body or template
+ message[:body] = template ? render(template) : body
end
- logger.info "[#{to}] Delivering: #{subject}"
- resp, email = Tuktuk.deliver(message) if Snails.env.production?
+ if html_body or html_template
+ message[:html_body] = html_template ? render(html_template) : html_body
+ end
+
+ logger.info "[#{to}] Delivering: #{subject}" # debug
+ resp, email = Tuktuk.deliver(message, debug: !Snails.env.production?)
if resp.is_a?(Tuktuk::Bounce)
logger.info "[#{to}] Email bounced! [#{resp.code}] #{resp.message}"
end
\ No newline at end of file