@@ -49,26 +49,39 end | |||||
49 |
|
49 | |||
50 | ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" } |
|
50 | ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" } | |
51 |
|
51 | |||
52 | module AsynchronousMailer |
|
52 | require 'mail' | |
53 | # Adds :async_smtp and :async_sendmail delivery methods |
|
53 | ||
54 | # to perform email deliveries asynchronously |
|
54 | module DeliveryMethods | |
55 | %w(smtp sendmail).each do |type| |
|
55 | class AsyncSMTP < ::Mail::SMTP | |
56 | define_method("perform_delivery_async_#{type}") do |mail| |
|
56 | def deliver!(*args) | |
|
57 | Thread.start do | |||
|
58 | super *args | |||
|
59 | end | |||
|
60 | end | |||
|
61 | end | |||
|
62 | ||||
|
63 | class AsyncSendmail < ::Mail::Sendmail | |||
|
64 | def deliver!(*args) | |||
57 | Thread.start do |
|
65 | Thread.start do | |
58 | send "perform_delivery_#{type}", mail |
|
66 | super *args | |
59 | end |
|
67 | end | |
60 | end |
|
68 | end | |
61 | end |
|
69 | end | |
62 |
|
70 | |||
63 | # Adds a delivery method that writes emails in tmp/emails for testing purpose |
|
71 | class TmpFile | |
64 | def perform_delivery_tmp_file(mail) |
|
72 | def initialize(*args); end | |
65 | dest_dir = File.join(Rails.root, 'tmp', 'emails') |
|
73 | ||
66 | Dir.mkdir(dest_dir) unless File.directory?(dest_dir) |
|
74 | def deliver!(mail) | |
67 | File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) } |
|
75 | dest_dir = File.join(Rails.root, 'tmp', 'emails') | |
|
76 | Dir.mkdir(dest_dir) unless File.directory?(dest_dir) | |||
|
77 | File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) } | |||
|
78 | end | |||
68 | end |
|
79 | end | |
69 | end |
|
80 | end | |
70 |
|
81 | |||
71 | ActionMailer::Base.send :include, AsynchronousMailer |
|
82 | ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP | |
|
83 | ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail | |||
|
84 | ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile | |||
72 |
|
85 | |||
73 | module ActionController |
|
86 | module ActionController | |
74 | module MimeResponds |
|
87 | module MimeResponds |
General Comments 0
You need to be logged in to leave comments.
Login now