diff options
Diffstat (limited to 'actionmailer/lib')
-rw-r--r-- | actionmailer/lib/action_mailer.rb | 1 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 67 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/inline_preview_interceptor.rb | 61 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/log_subscriber.rb | 2 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/mail_helper.rb | 14 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/preview.rb | 2 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/test_helper.rb | 2 | ||||
-rw-r--r-- | actionmailer/lib/rails/generators/mailer/USAGE | 4 |
8 files changed, 116 insertions, 37 deletions
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb index 17d8dcc208..291a8c1e34 100644 --- a/actionmailer/lib/action_mailer.rb +++ b/actionmailer/lib/action_mailer.rb @@ -40,6 +40,7 @@ module ActionMailer autoload :Base autoload :DeliveryMethods + autoload :InlinePreviewInterceptor autoload :MailHelper autoload :Preview autoload :Previews, 'action_mailer/preview' diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 2d8f5085fe..3c522ce707 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -21,11 +21,11 @@ module ActionMailer # the mailer views, options on the mail itself such as the <tt>:from</tt> address, and attachments. # # class ApplicationMailer < ActionMailer::Base - # default from: 'from@exmaple.com' + # default from: 'from@example.com' # layout 'mailer' # end # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # default from: 'no-reply@example.com', # return_path: 'system@example.com' # @@ -88,7 +88,7 @@ module ActionMailer # # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same # name as the method in your mailer model. For example, in the mailer defined above, the template at - # <tt>app/views/notifier/welcome.text.erb</tt> would be used to generate the email. + # <tt>app/views/notifier_mailer/welcome.text.erb</tt> would be used to generate the email. # # Variables defined in the methods of your mailer model are accessible as instance variables in their # corresponding view. @@ -134,25 +134,28 @@ module ActionMailer # # = Sending mail # - # Once a mailer action and template are defined, you can deliver your message or create it and save it - # for delivery later: + # Once a mailer action and template are defined, you can deliver your message or defer its creation and + # delivery for later: # - # Notifier.welcome(User.first).deliver_now # sends the email - # mail = Notifier.welcome(User.first) # => an ActionMailer::MessageDelivery object - # mail.deliver_now # sends the email + # NotifierMailer.welcome(User.first).deliver_now # sends the email + # mail = NotifierMailer.welcome(User.first) # => an ActionMailer::MessageDelivery object + # mail.deliver_now # generates and sends the email now # - # The <tt>ActionMailer::MessageDelivery</tt> class is a wrapper around a <tt>Mail::Message</tt> object. If - # you want direct access to the <tt>Mail::Message</tt> object you can call the <tt>message</tt> method on - # the <tt>ActionMailer::MessageDelivery</tt> object. + # The <tt>ActionMailer::MessageDelivery</tt> class is a wrapper around a delegate that will call + # your method to generate the mail. If you want direct access to delegator, or <tt>Mail::Message</tt>, + # you can call the <tt>message</tt> method on the <tt>ActionMailer::MessageDelivery</tt> object. # - # Notifier.welcome(User.first).message # => a Mail::Message object + # NotifierMailer.welcome(User.first).message # => a Mail::Message object # - # Action Mailer is nicely integrated with Active Job so you can send emails in the background (example: outside - # of the request-response cycle, so the user doesn't have to wait on it): + # Action Mailer is nicely integrated with Active Job so you can generate and send emails in the background + # (example: outside of the request-response cycle, so the user doesn't have to wait on it): # - # Notifier.welcome(User.first).deliver_later # enqueue the email sending to Active Job + # NotifierMailer.welcome(User.first).deliver_later # enqueue the email sending to Active Job + # + # Note that <tt>deliver_later</tt> will execute your method from the background job. # # You never instantiate your mailer class. Rather, you just call the method you defined on the class itself. + # All instance method are expected to return a message object to be sent. # # = Multipart Emails # @@ -179,7 +182,7 @@ module ActionMailer # # Sending attachment in emails is easy: # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # def welcome(recipient) # attachments['free_book.pdf'] = File.read('path/to/file.pdf') # mail(to: recipient, subject: "New account information") @@ -195,7 +198,7 @@ module ActionMailer # If you need to send attachments with no content, you need to create an empty view for it, # or add an empty body parameter like this: # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # def welcome(recipient) # attachments['free_book.pdf'] = File.read('path/to/file.pdf') # mail(to: recipient, subject: "New account information", body: "") @@ -207,7 +210,7 @@ module ActionMailer # You can also specify that a file should be displayed inline with other HTML. This is useful # if you want to display a corporate logo or a photo. # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # def welcome(recipient) # attachments.inline['photo.png'] = File.read('path/to/photo.png') # mail(to: recipient, subject: "Here is what we look like") @@ -246,7 +249,7 @@ module ActionMailer # Action Mailer provides some intelligent defaults for your emails, these are usually specified in a # default method inside the class definition: # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # default sender: 'system@example.com' # end # @@ -254,8 +257,8 @@ module ActionMailer # <tt>ActionMailer::Base</tt> sets the following: # # * <tt>mime_version: "1.0"</tt> - # * <tt>charset: "UTF-8",</tt> - # * <tt>content_type: "text/plain",</tt> + # * <tt>charset: "UTF-8"</tt> + # * <tt>content_type: "text/plain"</tt> # * <tt>parts_order: [ "text/plain", "text/enriched", "text/html" ]</tt> # # <tt>parts_order</tt> and <tt>charset</tt> are not actually valid <tt>Mail::Message</tt> header fields, @@ -264,7 +267,7 @@ module ActionMailer # As you can pass in any header, you need to either quote the header as a string, or pass it in as # an underscored symbol, so the following will work: # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # default 'Content-Transfer-Encoding' => '7bit', # content_description: 'This is a description' # end @@ -272,7 +275,7 @@ module ActionMailer # Finally, Action Mailer also supports passing <tt>Proc</tt> objects into the default hash, so you # can define methods that evaluate as the message is being generated: # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # default 'X-Special-Header' => Proc.new { my_method } # # private @@ -283,7 +286,7 @@ module ActionMailer # end # # Note that the proc is evaluated right at the start of the mail message generation, so if you - # set something in the defaults using a proc, and then set the same thing inside of your + # set something in the default using a proc, and then set the same thing inside of your # mailer method, it will get over written by the mailer method. # # It is also possible to set these default options that will be used in all mailers through @@ -297,7 +300,7 @@ module ActionMailer # This may be useful, for example, when you want to add default inline attachments for all # messages sent out by a certain mailer class: # - # class Notifier < ApplicationMailer + # class NotifierMailer < ApplicationMailer # before_action :add_inline_attachment! # # def welcome @@ -316,8 +319,9 @@ module ActionMailer # callbacks in the same manner that you would use callbacks in classes that # inherit from <tt>ActionController::Base</tt>. # - # Note that unless you have a specific reason to do so, you should prefer using before_action - # rather than after_action in your Action Mailer classes so that headers are parsed properly. + # Note that unless you have a specific reason to do so, you should prefer + # using <tt>before_action</tt> rather than <tt>after_action</tt> in your + # Action Mailer classes so that headers are parsed properly. # # = Previewing emails # @@ -325,9 +329,9 @@ module ActionMailer # <tt>ActionMailer::Base.preview_path</tt>. Since most emails do something interesting # with database data, you'll need to write some scenarios to load messages with fake data: # - # class NotifierPreview < ActionMailer::Preview + # class NotifierMailerPreview < ActionMailer::Preview # def welcome - # Notifier.welcome(User.first) + # NotifierMailer.welcome(User.first) # end # end # @@ -376,8 +380,8 @@ module ActionMailer # * <tt>:password</tt> - If your mail server requires authentication, set the password in this setting. # * <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the # authentication type here. - # This is a symbol and one of <tt>:plain</tt> (will send the password in the clear), <tt>:login</tt> (will - # send password Base64 encoded) or <tt>:cram_md5</tt> (combines a Challenge/Response mechanism to exchange + # This is a symbol and one of <tt>:plain</tt> (will send the password Base64 encoded), <tt>:login</tt> (will + # send the password Base64 encoded) or <tt>:cram_md5</tt> (combines a Challenge/Response mechanism to exchange # information and a cryptographic Message Digest 5 algorithm to hash important information) # * <tt>:enable_starttls_auto</tt> - Detects if STARTTLS is enabled in your SMTP server and starts # to use it. Defaults to <tt>true</tt>. @@ -595,6 +599,7 @@ module ActionMailer class NullMail #:nodoc: def body; '' end + def header; {} end def respond_to?(string, include_all=false) true diff --git a/actionmailer/lib/action_mailer/inline_preview_interceptor.rb b/actionmailer/lib/action_mailer/inline_preview_interceptor.rb new file mode 100644 index 0000000000..6d02b39225 --- /dev/null +++ b/actionmailer/lib/action_mailer/inline_preview_interceptor.rb @@ -0,0 +1,61 @@ +require 'base64' + +module ActionMailer + # Implements a mailer preview interceptor that converts image tag src attributes + # that use inline cid: style urls to data: style urls so that they are visible + # when previewing a HTML email in a web browser. + # + # This interceptor is enabled by default. To disable it, delete it from the + # <tt>ActionMailer::Base.preview_interceptors</tt> array: + # + # ActionMailer::Base.preview_interceptors.delete(ActionMailer::InlinePreviewInterceptor) + # + class InlinePreviewInterceptor + PATTERN = /src=(?:"cid:[^"]+"|'cid:[^']+')/i + + include Base64 + + def self.previewing_email(message) #:nodoc: + new(message).transform! + end + + def initialize(message) #:nodoc: + @message = message + end + + def transform! #:nodoc: + return message if html_part.blank? + + html_source.gsub!(PATTERN) do |match| + if part = find_part(match[9..-2]) + %[src="#{data_url(part)}"] + else + match + end + end + + message + end + + private + def message + @message + end + + def html_part + @html_part ||= message.html_part + end + + def html_source + html_part.body.raw_source + end + + def data_url(part) + "data:#{part.mime_type};base64,#{strict_encode64(part.body.raw_source)}" + end + + def find_part(cid) + message.all_parts.find{ |p| p.attachment? && p.cid == cid } + end + end +end diff --git a/actionmailer/lib/action_mailer/log_subscriber.rb b/actionmailer/lib/action_mailer/log_subscriber.rb index 5b57c75ec3..c2f671fdac 100644 --- a/actionmailer/lib/action_mailer/log_subscriber.rb +++ b/actionmailer/lib/action_mailer/log_subscriber.rb @@ -2,7 +2,7 @@ require 'active_support/log_subscriber' module ActionMailer # Implements the ActiveSupport::LogSubscriber for logging notifications when - # email is delivered and received. + # email is delivered or received. class LogSubscriber < ActiveSupport::LogSubscriber # An email was delivered. def deliver(event) diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index cc7935a7e0..239974e7b1 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -4,7 +4,17 @@ module ActionMailer # attachments list. module MailHelper # Take the text and format it, indented two spaces for each line, and - # wrapped at 72 columns. + # wrapped at 72 columns: + # + # text = <<-TEXT + # This is + # the paragraph. + # + # * item1 * item2 + # TEXT + # + # block_format text + # # => " This is the paragraph.\n\n * item1\n * item2\n" def block_format(text) formatted = text.split(/\n\r?\n/).collect { |paragraph| format_paragraph(paragraph) @@ -33,6 +43,8 @@ module ActionMailer end # Returns +text+ wrapped at +len+ columns and indented +indent+ spaces. + # By default column length +len+ equals 72 characters and indent + # +indent+ equal two spaces. # # my_text = 'Here is a sample text with more than 40 characters' # diff --git a/actionmailer/lib/action_mailer/preview.rb b/actionmailer/lib/action_mailer/preview.rb index 44cf6665ba..25ad7ee721 100644 --- a/actionmailer/lib/action_mailer/preview.rb +++ b/actionmailer/lib/action_mailer/preview.rb @@ -21,7 +21,7 @@ module ActionMailer # :nodoc: mattr_accessor :preview_interceptors, instance_writer: false - self.preview_interceptors = [] + self.preview_interceptors = [ActionMailer::InlinePreviewInterceptor] end module ClassMethods diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb index 524e6e3af1..4d03a616d2 100644 --- a/actionmailer/lib/action_mailer/test_helper.rb +++ b/actionmailer/lib/action_mailer/test_helper.rb @@ -34,7 +34,7 @@ module ActionMailer original_count = ActionMailer::Base.deliveries.size yield new_count = ActionMailer::Base.deliveries.size - assert_equal original_count + number, new_count, "#{number} emails expected, but #{new_count - original_count} were sent" + assert_equal number, new_count - original_count, "#{number} emails expected, but #{new_count - original_count} were sent" else assert_equal number, ActionMailer::Base.deliveries.size end diff --git a/actionmailer/lib/rails/generators/mailer/USAGE b/actionmailer/lib/rails/generators/mailer/USAGE index d9d9d064d8..2b0a078109 100644 --- a/actionmailer/lib/rails/generators/mailer/USAGE +++ b/actionmailer/lib/rails/generators/mailer/USAGE @@ -12,6 +12,6 @@ Example: creates a Notifications mailer class, views, and test: Mailer: app/mailers/notifications_mailer.rb - Views: app/views/notifications/signup.text.erb [...] - Test: test/mailers/notifications_test.rb + Views: app/views/notifications_mailer/signup.text.erb [...] + Test: test/mailers/notifications_mailer_test.rb |