Usage:
class ApplicationMailer < ActionMailer::Base
def post_notification(recipients, post)
@recipients = recipients
@from = post.author.email_address_with_name
@headers["bcc"] = SYSTEM_ADMINISTRATOR_EMAIL
@headers["reply-to"] = "notifications@example.com"
@subject = "[#{post.account.name} #{post.title}]"
@body["post"] = post
end
def comment_notification(recipient, comment)
@recipients = recipient.email_address_with_name
@subject = "[#{comment.post.project.client.firm.account.name}]" +
" Re: #{comment.post.title}"
@body["comment"] = comment
@from = comment.author.email_address_with_name
@sent_on = comment.posted_on
end
end
# After this post_notification will look for "templates/application_mailer/post_notification.rhtml"
ApplicationMailer.template_root = "templates"
ApplicationMailer.create_comment_notification(david, hello_world) # => a tmail object
ApplicationMailer.deliver_comment_notification(david, hello_world) # sends the email
Configuration options
These options are specified on the class level, like ActionMailer::Base.template_root = "/my/templates"
- template_root - template root determines the base from which template references will be made.
- logger - the logger is used for generating information on the mailing run if available. Can be set to nil for no logging. Compatible with both Ruby’s own Logger and Log4r loggers.
- server_settings - Allows detailed configuration of the server:
- :address Allows you to use a remote mail server. Just change it away from it’s default "localhost" setting.
- :port On the off change that your mail server doesn’t run on port 25, you can change it.
- :domain If you need to specify a HELO domain, you can do it here.
- :user_name If your mail server requires authentication, set the username and password in these two settings.
- :password If your mail server requires authentication, set the username and password in these two settings.
- :authentication If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain, :login, :cram_md5
- raise_delivery_errors - whether or not errors should be raised if the email fails to be delivered.
- delivery_method - Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test. Sendmail is assumed to be present at "/usr/sbin/sendmail".
- perform_deliveries - Determines whether deliver_* methods are actually carried out. By default they are, but this can be turned off to help functional testing.
- deliveries - Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.
- default_charset - The default charset used for the body and to
encode the subject. Defaults to UTF-8. You can also
pick a different charset from inside a method with <tt>@encoding</tt>.
Methods
- new
- quote_address_if_necessary
- quote_any_address_if_necessary
- quote_any_if_necessary
- quote_if_necessary
- receive
Constants
| CHARS_NEEDING_QUOTING | = | /[\000-\011\013\014\016-\037\177-\377]/ |
Attributes
| [RW] | bcc | |
| [RW] | body | |
| [RW] | cc | |
| [RW] | charset | |
| [RW] | from | |
| [RW] | headers | |
| [RW] | recipients | |
| [RW] | sent_on | |
| [RW] | subject |
Public Class methods
[ show source ]
# File vendor/rails/actionmailer/lib/action_mailer/base.rb, line 94
94: def initialize
95: @bcc = @cc = @from = @recipients = @sent_on = @subject = @body = nil
96: @charset = @@default_charset.dup
97: @headers = {}
98: end
Quote the given address if it needs to be. The address may be a regular email address, or it can be a phrase followed by an address in brackets. The phrase is the only part that will be quoted, and only if it needs to be. This allows extended characters to be used in the "to", "from", "cc", and "bcc" headers.
[ show source ]
# File vendor/rails/actionmailer/lib/action_mailer/base.rb, line 165
165: def quote_address_if_necessary(address, charset)
166: if Array === address
167: address.map { |a| quote_address_if_necessary(a, charset) }
168: elsif address =~ /^(\S.*)\s+(<.*>)$/
169: address = $2
170: phrase = quote_if_necessary($1.gsub(/^['"](.*)['"]$/, '\1'), charset)
171: "#{phrase} #{address}"
172: else
173: address
174: end
175: end
Quote any of the given addresses, if they need to be.
[ show source ]
# File vendor/rails/actionmailer/lib/action_mailer/base.rb, line 178
178: def quote_any_address_if_necessary(charset, *args)
179: args.map { |v| quote_address_if_necessary(v, charset) }
180: end
Quote any of the given strings if they contain any "illegal" characters
[ show source ]
# File vendor/rails/actionmailer/lib/action_mailer/base.rb, line 156
156: def quote_any_if_necessary(charset, *args)
157: args.map { |v| quote_if_necessary(v, charset) }
158: end
Quote the given text if it contains any "illegal" characters
[ show source ]
# File vendor/rails/actionmailer/lib/action_mailer/base.rb, line 149
149: def quote_if_necessary(text, charset)
150: (text =~ CHARS_NEEDING_QUOTING) ?
151: quoted_printable(text, charset) :
152: text
153: end
[ show source ]
# File vendor/rails/actionmailer/lib/action_mailer/base.rb, line 182
182: def receive(raw_email)
183: logger.info "Received mail:\n #{raw_email}" unless logger.nil?
184: new.receive(TMail::Mail.parse(raw_email))
185: end