public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
Feature: Mailer Support. [#24 state:resolved]
markbates (author)
Thu Jul 24 13:01:21 -0700 2008
commit  3fe046b8d040265fea50835c2cc2dd5b935f87af
tree    8ce18741489a1dea3bfaab847ed20128bddaf9a4
parent  424c5613226bbbc5876f4b3c5737420eebc77745
...
1
2
3
 
4
5
 
6
7
8
 
9
10
11
 
12
 
13
 
14
15
16
...
1
2
3
4
5
6
7
8
9
 
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -1,16 +1,21 @@
0
 module Mack
0
   module Mailer
0
     module Adapters # :nodoc:
0
+      # All mail adapters need to extend this class.
0
       class Base
0
         
0
+        # The origina Mack::Mailer object passed in.
0
         attr_accessor :mack_mailer
0
         
0
-        def initialize(mail)
0
+        def initialize(mail) # :nodoc:
0
           self.mack_mailer = mail
0
         end
0
         
0
+        # The transformed (ie, converted, object)
0
         needs_method :transformed
0
+        # Convert the Mack::Mailer object to the adapted object.
0
         needs_method :convert
0
+        # The RAW encoded String ready for delivery via SMTP, Sendmail, etc...
0
         needs_method :deliverable
0
         
0
       end # Base
...
3
4
5
 
6
7
8
9
10
11
 
 
 
12
13
14
15
16
 
17
18
19
20
 
21
22
23
...
3
4
5
6
7
8
 
 
 
 
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -3,21 +3,23 @@ require 'base64'
0
 module Mack
0
   module Mailer
0
     module Adapters # :nodoc:
0
+      # Converts a Mack::Mailer object into a TMail object.
0
       class Tmail < Mack::Mailer::Adapters::Base
0
         
0
-        def to_s
0
-          
0
-        end
0
-        
0
+        # Returns the underlying TMail object.
0
+        # Raises Mack::Errors::UnconvertedMailer if the convert method hasn't
0
+        # been called first.
0
         def transformed
0
           raise Mack::Errors::UnconvertedMailer.new if @tmail.nil?
0
           @tmail
0
         end
0
         
0
+        # Returns the ready to be delivered encoded String
0
         def deliverable
0
           transformed.encoded
0
         end
0
         
0
+        # Converts the Mack::Mailer object to a TMail object.
0
         def convert
0
           @tmail = TMail::Mail.new 
0
           @tmail.to =           mack_mailer.to
...
1
2
 
3
4
 
5
6
 
7
8
9
...
13
14
15
 
16
17
18
19
20
 
 
21
22
23
24
25
26
...
1
2
3
4
5
6
7
 
8
9
10
11
...
15
16
17
18
19
20
21
22
 
23
24
25
26
 
27
28
29
0
@@ -1,9 +1,11 @@
0
 module Mack
0
   module Mailer
0
+    # Creates an attachment for a Mack::Mailer object.
0
     class Attachment
0
       
0
+      # Returns a String representing the body of the attachment. This String is NOT encoded in anyway!
0
       attr_accessor :body
0
-      attr_accessor :content_type
0
+      # Returns the name of the attached file.
0
       attr_accessor :file_name
0
       
0
       def initialize(body = nil)
0
@@ -13,14 +15,15 @@ module Mack
0
         end
0
       end
0
       
0
+      # Takes an IO object and sets the body. You'll need to explicity set the file_name afterwards.
0
       def add_io(io)
0
         self.body = io.read
0
       end
0
       
0
-      def add_file(file, content_type = File.extname(file).gsub('.', ''))
0
+      # Takes a path to a file, reads it in, and sets the file_name based on the path.
0
+      def add_file(file)
0
         self.file_name = File.basename(file)
0
         self.body = File.read(file)
0
-        self.content_type = Mack::Utils::MimeTypes[content_type]
0
       end
0
       
0
     end # Attachment
...
11
12
13
14
 
15
16
17
...
11
12
13
 
14
15
16
17
0
@@ -11,7 +11,7 @@ module Mack
0
           Net::SMTP.start(smtp_settings[:address], smtp_settings[:port], 
0
                           smtp_settings[:domain], smtp_settings[:user_name], 
0
                           smtp_settings[:password], smtp_settings[:authentication]) do |smtp|
0
-            smtp.sendmail(mail.deliverable, mail.reply_to, mail.destinations)
0
+            smtp.sendmail(mail.deliverable, mail.reply_to, mail.recipients)
0
           end
0
         end
0
         
...
8
9
10
11
 
12
13
14
...
8
9
10
 
11
12
13
14
0
@@ -8,7 +8,7 @@ module Mack
0
           EmailRegistry.add(mail)
0
         end
0
         
0
-        class EmailRegistry < Mack::Utils::Registry
0
+        class EmailRegistry < Mack::Utils::Registry # :nodoc:
0
         end
0
         
0
       end # Test
...
1
 
2
3
4
...
11
12
13
 
14
 
15
16
17
18
19
20
 
 
21
22
23
...
25
26
27
 
 
28
29
30
...
32
33
34
 
35
36
37
38
 
39
40
41
...
49
50
51
 
52
53
54
55
 
56
57
58
59
 
 
60
61
62
63
64
 
65
66
67
68
 
69
70
71
72
 
73
74
75
76
77
 
 
78
79
80
 
81
82
83
...
1
2
3
4
5
...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
30
31
32
33
34
35
36
37
...
39
40
41
42
43
44
45
46
47
48
49
50
...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
93
94
95
96
97
98
99
100
101
0
@@ -1,4 +1,5 @@
0
 module Mack # :nodoc:
0
+  # The heart and soul of the mack-mailer package.
0
   module Mailer
0
     
0
     attr_accessor :to
0
@@ -11,13 +12,17 @@ module Mack # :nodoc:
0
     attr_accessor :html_body
0
     attr_accessor :date_sent
0
     attr_accessor :mime_version
0
+    attr_accessor :content_type
0
     
0
+    # A helper method that takes a Hash and will populate the email with the key/value pairs of that Hash.
0
     def build(options = {})
0
       options.each do |k,v|
0
         self.send("#{k}=", v)
0
       end
0
     end
0
     
0
+    # Returns the text_body of the email. If there is no text_body set it will attempt to build one using
0
+    # the text.erb template for this mailer.
0
     def text_body
0
       if @text_body.blank?
0
         @text_body = build_template(:text)
0
@@ -25,6 +30,8 @@ module Mack # :nodoc:
0
       return @text_body
0
     end
0
     
0
+    # Returns the html_body of the email. If there is no html_body set it will attempt to build one using
0
+    # the html.erb template for this mailer.
0
     def html_body
0
       if @html_body.blank?
0
         @html_body = build_template(:html)
0
@@ -32,10 +39,12 @@ module Mack # :nodoc:
0
       @html_body
0
     end
0
     
0
+    # Returns the mime_version of the email, defaults to "1.0"
0
     def mime_version
0
       (@mime_version ||= "1.0")
0
     end
0
     
0
+    # This will attempt to determine the content type of the email, unless one is already specified. 
0
     def content_type
0
       return @content_type unless @content_type.blank?
0
       if has_attachments?
0
@@ -49,35 +58,44 @@ module Mack # :nodoc:
0
       end
0
     end
0
     
0
+    # Returns the date sent, defaults to Time.now
0
     def date_sent
0
       (@date_sent ||= Time.now)
0
     end
0
     
0
+    # Returns the reply to address, defaults to the from address.
0
     def reply_to
0
       (@reply_to || self.from)
0
     end
0
     
0
+    # Adds a Mack::Mailer::Attachment to the email.
0
+    # Raise ArgumentError if the parameter is not a Mack::Mailer::Attachment
0
     def attach(file)
0
       raise ArgumentError.new unless file.is_a?(Mack::Mailer::Attachment)
0
       attachments << file
0
     end
0
     
0
+    # Returns true if there are attachments.
0
     def has_attachments?
0
       !attachments.empty?
0
     end
0
     
0
+    # Returns the attachments Array.
0
     def attachments
0
       @attachments ||= []
0
     end
0
     
0
+    # Delivers the email with the configured Mack::Mailer::DeliveryHandlers class.
0
     def deliver(handler = app_config.mailer.deliver_with)
0
       "Mack::Mailer::DeliveryHandlers::#{handler.camelcase}".constantize.deliver(self)
0
     end
0
     
0
-    def destinations
0
+    # Returns all the recipients of this email.
0
+    def recipients
0
       [self.to, self.cc, self.bcc].flatten.compact
0
     end
0
     
0
+    # Returns a ready to be delivered, encoded, version of the email.
0
     def deliverable(adapter = app_config.mailer.adapter)
0
       adap = "Mack::Mailer::Adapters::#{adapter.camelcase}".constantize.new(self)
0
       adap.convert
...
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
 
6
7
8
9
 
10
11
12
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
15
16
17
18
 
19
20
21
22
0
@@ -1,12 +1,22 @@
0
+# Generates the necessary files for a basic mailer.
0
+# 
0
+# Example:
0
+#   rake generate:mailer name=welcome_email
0
+# generates the following files:
0
+#   app/mailers/welcome_email.rb
0
+#   app/mailers/templates/welcome_email/text.erb
0
+#   app/mailers/templates/welcome_email/html.erb
0
+#   test/unit/welcome_email_spec.rb # => if using RSpec
0
+#   test/unit/welcome_email_test.rb # => if using Test::Unit::TestCase
0
 class MailerGenerator < Genosaurus
0
   
0
   require_param :name
0
   
0
-  def file_name
0
+  def file_name # :nodoc:
0
     param(:name).underscore.downcase
0
   end
0
   
0
-  def class_name
0
+  def class_name # :nodoc:
0
     param(:name).camelcase
0
   end
0
   
...
1
2
3
 
4
5
6
7
 
 
 
 
 
8
9
10
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0
@@ -1,9 +1,15 @@
0
 module Mack # :nodoc:
0
   module Paths
0
     
0
+    # The path to the app/mailers directory.
0
     def self.mailers(*args)
0
       File.join(Mack.root, "app", "mailers", args)
0
     end
0
     
0
+    # The path to the app/mailers/templates directory.
0
+    def self.mailer_templates(*args)
0
+      Mack::Paths.mailers("templates", args)
0
+    end
0
+    
0
   end
0
 end
0
\ No newline at end of file
...
1
2
3
4
 
 
 
5
6
7
 
8
9
10
...
1
 
 
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 module Mack
0
-  module Rendering
0
-    module Type
0
-      class Mailer < Mack::Rendering::Type::FileBase
0
+  module Rendering # :nodoc:
0
+    module Type # :nodoc:
0
+      class Mailer < Mack::Rendering::Type::FileBase # :nodoc:
0
         
0
         def render
0
-          x_file = Mack::Paths.mailers("templates", self.render_value, self.options[:format])
0
+          x_file = Mack::Paths.mailer_templates(self.render_value, self.options[:format])
0
           render_file(x_file)
0
         end
0
         
...
11
12
13
14
15
16
17
...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
...
11
12
13
 
14
15
16
...
30
31
32
 
33
34
35
36
37
 
 
38
39
40
0
@@ -11,7 +11,6 @@ describe Mack::Mailer::Attachment do
0
     it "should read in a file and set the content_type based on the extension" do
0
       at = Mack::Mailer::Attachment.new
0
       at.add_file(@my_file)
0
-      at.content_type.should == "image/png"
0
       at.body.should == File.read(@my_file)
0
     end
0
     
0
@@ -31,14 +30,11 @@ describe Mack::Mailer::Attachment do
0
     
0
     it "should take a string and read call add_file" do
0
       at = Mack::Mailer::Attachment.new(@my_file)
0
-      at.content_type.should == "image/png"
0
       at.body.should == File.read(@my_file)
0
     end
0
     
0
     it "should take an IO and call add_io" do
0
       at = Mack::Mailer::Attachment.new(File.open(@my_file))
0
-      at.content_type = "image/png"
0
-      at.content_type.should == "image/png"
0
       at.body.should == File.read(@my_file)
0
     end
0
     
...
60
61
62
63
 
64
65
66
...
60
61
62
 
63
64
65
66
0
@@ -60,7 +60,7 @@ describe Mack::Mailer do
0
       @we.to = "mark@mackframework.com"
0
       @we.cc = ["foo@example.com", "bar@example.com"]
0
       @we.bcc = "fubar@example.com"
0
-      @we.destinations.should == ["mark@mackframework.com", "foo@example.com", "bar@example.com", "fubar@example.com"]
0
+      @we.recipients.should == ["mark@mackframework.com", "foo@example.com", "bar@example.com", "fubar@example.com"]
0
     end
0
     
0
   end

Comments