Class: ActionMailer::Base
- Inherits:
-
Object
- Object
- ActionMailer::Base
- Defined in:
- lib/action_mailer/base.rb
Overview
Usage:
class ApplicationMailer < ActionMailer::Base
def post_notification(recipients, post)
@recipients = recipients
@from = post..email_address_with_name
@headers["bcc"] = SYSTEM_ADMINISTRATOR_EMAIL
@headers["reply-to"] = "[email protected]"
@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..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 alsopick a different charset from inside a method with <tt>@encoding</tt>.
-
encode_subject
- Whether or not to encode the subject with the active charset. Defaults to true.
Constant Summary collapse
- @@server_settings =
{ :address => "localhost", :port => 25, :domain => 'localhost.localdomain', :user_name => nil, :password => nil, :authentication => nil }
- @@raise_delivery_errors =
true
- @@delivery_method =
:smtp
- @@perform_deliveries =
true
- @@deliveries =
[]
- @@default_charset =
"utf-8"
- @@encode_subject =
true
Instance Attribute Summary collapse
-
#bcc ⇒ Object
Returns the value of attribute bcc.
-
#body ⇒ Object
Returns the value of attribute body.
-
#cc ⇒ Object
Returns the value of attribute cc.
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#encode_subject ⇒ Object
Returns the value of attribute encode_subject.
-
#from ⇒ Object
Returns the value of attribute from.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#recipients ⇒ Object
Returns the value of attribute recipients.
-
#sent_on ⇒ Object
Returns the value of attribute sent_on.
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
- .create(to, subject, body, from, timestamp = nil, headers = {}, encode = @@encode_subject, charset = @@default_charset) ⇒ Object
-
.deliver(mail) ⇒ Object
:nodoc:.
- .mail(to, subject, body, from, timestamp = nil, headers = {}, encode = @@encode_subject, charset = @@default_charset) ⇒ Object
-
.method_missing(method_symbol, *parameters) ⇒ Object
:nodoc:.
-
.quoted_printable(text, charset) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
99 100 101 102 103 104 |
# File 'lib/action_mailer/base.rb', line 99 def initialize @bcc = @cc = @from = @recipients = @sent_on = @subject = @body = nil @charset = @@default_charset.dup @encode_subject = @@encode_subject @headers = {} end |
Instance Attribute Details
#bcc ⇒ Object
Returns the value of attribute bcc.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def bcc @bcc end |
#body ⇒ Object
Returns the value of attribute body.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def body @body end |
#cc ⇒ Object
Returns the value of attribute cc.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def cc @cc end |
#charset ⇒ Object
Returns the value of attribute charset.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def charset @charset end |
#encode_subject ⇒ Object
Returns the value of attribute encode_subject.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def encode_subject @encode_subject end |
#from ⇒ Object
Returns the value of attribute from.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def from @from end |
#headers ⇒ Object
Returns the value of attribute headers.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def headers @headers end |
#recipients ⇒ Object
Returns the value of attribute recipients.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def recipients @recipients end |
#sent_on ⇒ Object
Returns the value of attribute sent_on.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def sent_on @sent_on end |
#subject ⇒ Object
Returns the value of attribute subject.
97 98 99 |
# File 'lib/action_mailer/base.rb', line 97 def subject @subject end |
Class Method Details
.create(to, subject, body, from, timestamp = nil, headers = {}, encode = @@encode_subject, charset = @@default_charset) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/action_mailer/base.rb', line 126 def create(to, subject, body, from, = nil, headers = {}, encode = @@encode_subject, charset = @@default_charset ) #:nodoc: m = TMail::Mail.new m.to, m.subject, m.body, m.from = to, ( encode ? quoted_printable(subject,charset) : subject ), body, from m.date = .respond_to?("to_time") ? .to_time : ( || Time.now) m.set_content_type "text", "plain", { "charset" => charset } headers.each do |k, v| m[k] = v end return m end |
.deliver(mail) ⇒ Object
:nodoc:
143 144 145 146 |
# File 'lib/action_mailer/base.rb', line 143 def deliver(mail) #:nodoc: logger.info "Sent mail:\n #{mail.encoded}" unless logger.nil? send("perform_delivery_#{delivery_method}", mail) if perform_deliveries end |
.mail(to, subject, body, from, timestamp = nil, headers = {}, encode = @@encode_subject, charset = @@default_charset) ⇒ Object
120 121 122 123 124 |
# File 'lib/action_mailer/base.rb', line 120 def mail(to, subject, body, from, = nil, headers = {}, encode = @@encode_subject, charset = @@default_charset ) #:nodoc: deliver(create(to, subject, body, from, , headers, charset)) end |
.method_missing(method_symbol, *parameters) ⇒ Object
:nodoc:
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/action_mailer/base.rb', line 107 def method_missing(method_symbol, *parameters)#:nodoc: case method_symbol.id2name when /^create_([_a-z]*)/ create_from_action($1, *parameters) when /^deliver_([_a-z]*)/ begin deliver(send("create_" + $1, *parameters)) rescue Object => e raise e if raise_delivery_errors end end end |
.quoted_printable(text, charset) ⇒ Object
:nodoc:
148 149 150 151 |
# File 'lib/action_mailer/base.rb', line 148 def quoted_printable(text, charset)#:nodoc: text = text.gsub( /[^a-z ]/i ) { "=%02x" % $&[0] }.gsub( / /, "_" ) "=?#{charset}?Q?#{text}?=" end |