Class: PostageApp::Mailer
- Inherits:
-
ActionMailer::Base
- Object
- ActionMailer::Base
- PostageApp::Mailer
- Defined in:
- lib/postageapp/mailer/mailer_2.rb,
lib/postageapp/mailer/mailer_3.rb,
lib/postageapp/mailer/mailer_4.rb
Overview
Postage::Mailer allows you to use/re-use existing mailers set up using ActionMailer. The only catch is to change inheritance from ActionMailer::Base to PostageApp::Mailer. Also don’t forget to require ‘postageapp/mailer’
Here’s an example of a valid PostageApp::Mailer class
require 'postageapp/mailer'
class Notifier < PostageApp::Mailer
def signup_notification(recipient)
mail(
to: recipient.email,
from: '[email protected]',
subject: 'Test Message'
)
end
end
Postage::Mailer introduces a few mailer methods specific to Postage:
-
postageapp_template - template name that is defined in your PostageApp project
-
postageapp_variables - extra variables you want to send along with the message
Sending email
# Create a PostageApp::Request object
request = Notifier.signup_notification(user)
# Deliver the message and return a PostageApp::Response
response = request.deliver_now
Defined Under Namespace
Classes: Attachments
Constant Summary collapse
- CONTENT_TYPE_MAP =
{ 'html' => 'text/html', 'text' => 'text/plain' }
Instance Method Summary collapse
- #attachments ⇒ Object
-
#create_mail ⇒ Object
Creating a Postage::Request object unlike TMail one in ActionMailer::Base.
- #deliver!(mail = @mail) ⇒ Object
-
#headers(args = nil) ⇒ Object
Override for headers assignment.
-
#initialize(method_name = nil, *args) ⇒ Mailer
constructor
Instead of initializing Mail object, we prepare PostageApp::Request.
-
#mail(headers = { }, &block) ⇒ Object
Overriding method that prepares Mail object.
- #perform_delivery_postage(mail) ⇒ Object
- #postageapp_api_key(value = nil) ⇒ Object
-
#postageapp_template(value = nil) ⇒ Object
In API call we can specify PostageApp template that will be used to generate content of the message.
-
#postageapp_uid(value = nil) ⇒ Object
Possible to define custom uid.
-
#postageapp_variables(value = nil) ⇒ Object
Hash of variables that will be used to inject into the content.
-
#render(opts) ⇒ Object
Not insisting rendering a view if it’s not there.
Constructor Details
#initialize(method_name = nil, *args) ⇒ Mailer
Instead of initializing Mail object, we prepare PostageApp::Request
64 65 66 67 68 69 70 71 72 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 64 def initialize(method_name = nil, *args) super() @_message = PostageApp::Request.new(:send_message) if method_name process(method_name, *args) end end |
Instance Method Details
#attachments ⇒ Object
102 103 104 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 102 def @_attachments ||= Attachments.new(@_message) end |
#create_mail ⇒ Object
Creating a Postage::Request object unlike TMail one in ActionMailer::Base
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/postageapp/mailer/mailer_2.rb', line 53 def create_mail params = { } unless (self.recipients.blank?) params['recipients'] = self.recipients end params['headers'] = { } unless (self.subject.blank?) params['headers']['subject'] = self.subject end unless (self.subject.blank?) params['headers']['from'] = self.from end unless (self.headers.blank?) params['headers'].merge!(self.headers) end params['content'] = { } params['attachments'] = { } if (@parts.empty?) unless (self.body.blank?) params['content'][self.content_type] = self.body end else self.parts.each do |part| case (part.content_disposition) when 'inline' if (part.content_type.blank? && String === part.body) part.content_type = 'text/plain' end params['content'][part.content_type] = part.body when 'attachment' params['attachments'][part.filename] = { 'content_type' => part.content_type, 'content' => Base64.encode64(part.body) } end end end unless (self.postageapp_template.blank?) params['template'] = self.postageapp_template end unless (self.postageapp_variables.blank?) params['variables'] = self.postageapp_variables end if (params['headers'].blank?) params.delete('headers') end if (params['content'].blank?) params.delete('content') end if (params['attachments'].blank?) params.delete('attachments') end @mail = PostageApp::Request.new('send_message', params) unless (self.postageapp_uid.blank?) @mail.uid = self.postageapp_uid end unless (self.postageapp_api_key.blank?) @mail.api_key = self.postageapp_api_key end @mail end |
#deliver!(mail = @mail) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/postageapp/mailer/mailer_2.rb', line 42 def deliver!(mail = @mail) unless (mail) raise 'PostageApp::Request object not present, cannot deliver' end if (perform_deliveries) __send__("perform_delivery_#{delivery_method}", mail) end end |
#headers(args = nil) ⇒ Object
Override for headers assignment
107 108 109 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 107 def headers(args = nil) @_message.headers(args) end |
#mail(headers = { }, &block) ⇒ Object
Overriding method that prepares Mail object. This time we’ll be contructing PostageApp::Request payload.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 113 def mail(headers = { }, &block) # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is removed @mail_was_called = true m = @_message # At the beginning, do not consider class default for parts order neither content_type content_type = headers[:content_type] parts_order = headers[:parts_order] # Call all the procs (if any) default_values = self.class.default.merge(self.class.default) do |k,v| if (v.respond_to?(:call)) v.bind(self).call else v end end # Handle defaults headers = headers.reverse_merge(default_values) # Set configure delivery behavior wrap_delivery_behavior!(headers.delete(:delivery_method)) # Assigning recipients m.arguments['recipients'] = headers.delete(:to) # Assign all headers except parts_order, content_type and body assignable = headers.except(:parts_order, :content_type, :body, :template_name, :template_path) m.headers.merge!(assignable) # Render the templates and blocks responses, explicit_order = collect_responses_and_parts_order(headers, &block) create_parts_from_responses(m, responses) m end |
#perform_delivery_postage(mail) ⇒ Object
38 39 40 |
# File 'lib/postageapp/mailer/mailer_2.rb', line 38 def perform_delivery_postage(mail) mail.send end |
#postageapp_api_key(value = nil) ⇒ Object
79 80 81 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 79 def postageapp_api_key(value = nil) value ? @_message.api_key = value : @_message.api_key end |
#postageapp_template(value = nil) ⇒ Object
In API call we can specify PostageApp template that will be used to generate content of the message
85 86 87 88 89 90 91 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 85 def postageapp_template(value = nil) if (value) @_message.arguments['template'] = value else @_message.arguments['template'] end end |
#postageapp_uid(value = nil) ⇒ Object
Possible to define custom uid. Should be sufficiently unique
75 76 77 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 75 def postageapp_uid(value = nil) value ? @_message.uid = value : @_message.uid end |
#postageapp_variables(value = nil) ⇒ Object
Hash of variables that will be used to inject into the content
94 95 96 97 98 99 100 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 94 def postageapp_variables(value = nil) if (value) @_message.arguments['variables'] = value else @_message.arguments['variables'] end end |
#render(opts) ⇒ Object
Not insisting rendering a view if it’s not there. PostageApp gem can send blank content provided that the template is defined.
134 135 136 137 138 139 |
# File 'lib/postageapp/mailer/mailer_2.rb', line 134 def render(opts) super(opts) rescue ActionView::MissingTemplate # do nothing end |