Skip to content
This repository
Browse code

Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…

…eas Schwarz]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@54 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 165097ede50e609ff5661b969b95cc9959722ce1 1 parent 35c89c4
David Heinemeier Hansson authored December 07, 2004
13  actionmailer/CHANGELOG
... ...
@@ -1,3 +1,16 @@
  1
+*SVN*
  2
+
  3
+* Added access to custom headers, like cc, bcc, and reply-to #268 [Andreas Schwarz]. Example:
  4
+
  5
+    def post_notification(recipients, post)
  6
+      @recipients          = recipients
  7
+      @from                = post.author.email_address_with_name
  8
+      @headers["bcc"]      = SYSTEM_ADMINISTRATOR_EMAIL
  9
+      @headers["reply-to"] = "notifications@example.com"
  10
+      @subject             = "[#{post.account.name} #{post.title}]"
  11
+      @body["post"]        = post
  12
+    end
  13
+
1 14
 *0.4* (5)
2 15
 
3 16
 * Consolidated the server configuration options into Base#server_settings= and expanded that with controls for authentication and more [Marten]
30  actionmailer/lib/action_mailer/base.rb
@@ -3,10 +3,12 @@ module ActionMailer #:nodoc:
3 3
   #
4 4
   #   class ApplicationMailer < ActionMailer::Base
5 5
   #     def post_notification(recipients, post)
6  
-  #       @recipients   = recipients
7  
-  #       @subject      = "[#{post.account.name} #{post.title}]"
8  
-  #       @body["post"] = post
9  
-  #       @from         = post.author.email_address_with_name
  6
+  #       @recipients          = recipients
  7
+  #       @from                = post.author.email_address_with_name
  8
+  #       @headers["bcc"]      = SYSTEM_ADMINISTRATOR_EMAIL
  9
+  #       @headers["reply-to"] = "notifications@example.com"
  10
+  #       @subject             = "[#{post.account.name} #{post.title}]"
  11
+  #       @body["post"]        = post
10 12
   #     end
11 13
   #     
12 14
   #     def comment_notification(recipient, comment)
@@ -72,7 +74,11 @@ class Base
72 74
     @@deliveries = []
73 75
     cattr_accessor :deliveries
74 76
 
75  
-    attr_accessor :recipients, :subject, :body, :from, :sent_on, :bcc, :cc
  77
+    attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers, :bcc, :cc
  78
+
  79
+    def initialize
  80
+      @headers = {}
  81
+    end
76 82
 
77 83
     class << self
78 84
       def method_missing(method_symbol, *parameters)#:nodoc:
@@ -88,14 +94,18 @@ def method_missing(method_symbol, *parameters)#:nodoc:
88 94
         end        
89 95
       end
90 96
 
91  
-      def mail(to, subject, body, from, timestamp = nil) #:nodoc:
92  
-        deliver(create(to, subject, body, from, timestamp))
  97
+      def mail(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc:
  98
+        deliver(create(to, subject, body, from, timestamp, headers))
93 99
       end
94 100
 
95  
-      def create(to, subject, body, from, timestamp = nil) #:nodoc:
  101
+      def create(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc:
96 102
         m = TMail::Mail.new
97 103
         m.to, m.subject, m.body, m.from = to, subject, body, from
98 104
         m.date = timestamp.respond_to?("to_time") ? timestamp.to_time : (timestamp || Time.now)    
  105
+        headers.each do |k, v|
  106
+          m[k] = v
  107
+        end
  108
+
99 109
         return m
100 110
       end
101 111
 
@@ -129,9 +139,9 @@ def create_from_action(method_name, *parameters)
129 139
           mailer.send(method_name, *parameters)
130 140
 
131 141
           if String === mailer.body
132  
-            mail = create(mailer.recipients, mailer.subject, mailer.body, mailer.from, mailer.sent_on)
  142
+            mail = create(mailer.recipients, mailer.subject, mailer.body, mailer.from, mailer.sent_on, mailer.headers)
133 143
           else
134  
-            mail = create(mailer.recipients, mailer.subject, render_body(mailer, method_name), mailer.from, mailer.sent_on)
  144
+            mail = create(mailer.recipients, mailer.subject, render_body(mailer, method_name), mailer.from, mailer.sent_on, mailer.headers)
135 145
           end
136 146
 
137 147
           mail.bcc = @bcc if @bcc

0 notes on commit 165097e

Please sign in to comment.
Something went wrong with that request. Please try again.