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