Class: Mailjet::APIMailer
- Inherits:
-
Object
- Object
- Mailjet::APIMailer
- Defined in:
- lib/mailjet/mailer.rb
Overview
Mailjet::APIMailer maps a Mail::Message coming from ActionMailer To Mailjet Send API (see full documentation here dev.mailjet.com/guides/#send-transactional-email) Mailjet sends API expects a JSON payload as the input. The deliver methods maps the Mail::Message attributes to the MailjetSend API JSON expected structure
Constant Summary collapse
- V3_0_PERMITTED_OPTIONS =
[ :recipients, :'mj-prio', :'mj-campaign', :'mj-deduplicatecampaign', :'mj-templatelanguage', :'mj-templateerrorreporting', :'mj-templateerrordeliver', :'mj-templateid', :'mj-trackopen', :'mj-trackclick', :'mj-customid', :'mj-eventpayload', :vars, :headers, ]
- V3_1_PERMITTED_OPTIONS =
[ :'Priority', :'CustomCampaign', :'DeduplicateCampaign', :'TemplateLanguage', :'TemplateErrorReporting', :'TemplateErrorDeliver', :'TemplateID', :'TrackOpens', :'TrackClicks', :'CustomID', :'EventPayload', :'Variables', :'Headers', ]
- CONNECTION_PERMITTED_OPTIONS =
[:api_key, :secret_key]
- HEADER_BLACKLIST =
[ 'from', 'sender', 'subject', 'to', 'cc', 'bcc', 'return-path', 'delivered-to', 'dkim-signature', 'domainkey-status', 'received-spf', 'authentication-results', 'received', 'user-agent', 'x-mailer', 'x-feedback-id', 'list-id', 'date', 'x-csa-complaints', 'message-id', 'reply-to', 'content-type', 'mime-version', 'content-transfer-encoding' ]
Instance Method Summary collapse
- #decode_emails_V3_1!(payload) ⇒ Object
- #deliver!(mail, opts = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ APIMailer
constructor
A new instance of APIMailer.
- #setContentV3_0(mail) ⇒ Object
- #setContentV3_1(mail) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ APIMailer
Returns a new instance of APIMailer.
53 54 55 56 57 58 59 60 |
# File 'lib/mailjet/mailer.rb', line 53 def initialize(opts = {}) = HashWithIndifferentAccess.new(opts) @version = [:version] @delivery_method_options_v3_0 = .slice(*V3_0_PERMITTED_OPTIONS) @delivery_method_options_v3_1 = .slice(*V3_1_PERMITTED_OPTIONS) @connection_options = .slice(*CONNECTION_PERMITTED_OPTIONS) end |
Instance Method Details
#decode_emails_V3_1!(payload) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/mailjet/mailer.rb', line 216 def decode_emails_V3_1!(payload) # ActionMailer may have handed us encoded email # addresses, mailjet will reject. Therefore we # walk through the payload to decode them back. payload.each do |key, value| if key == :Email payload[key] = Mail::Encodings.value_decode(value) elsif value.is_a?(Hash) decode_emails_V3_1! value elsif value.is_a?(Array) value.each do |item| if item.is_a?(Hash) decode_emails_V3_1! item end end end end end |
#deliver!(mail, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mailjet/mailer.rb', line 62 def deliver!(mail, opts = {}) = HashWithIndifferentAccess.new(opts) # Mailjet Send API does not support full from. Splitting the from field into two: name and email address mail[:from] ||= Mailjet.config.default_from if Mailjet.config.default_from # add `@connection_options` in `options` only if not exist yet (values in `options` prime) .reverse_merge!(@connection_options) # add `@version` in options if set [:version] = @version if @version # `options[:version]` primes on global config version = [:version] || Mailjet.config.api_version if (version == 'v3.1') Mailjet::Send.create({ :Messages => [setContentV3_1(mail)], SandboxMode: Mailjet.config.sandbox_mode }, ) else Mailjet::Send.create(setContentV3_0(mail), ) end end |
#setContentV3_0(mail) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/mailjet/mailer.rb', line 235 def setContentV3_0(mail) content = {} content[:text_part] = mail.text_part.try(:decoded) if !mail.text_part.blank? content[:html_part] = mail.html_part.try(:decoded) if !mail.html_part.blank? # try message `body` as fallback if no content found unless content[:text_part] || content[:html_part] || mail.body.try(:raw_source).empty? content[mail.content_type.try(:include?,'text/html') ? :html_part : :text_part] = mail.body.raw_source end # Formatting attachments (inline + regular) unless mail..empty? content[:attachments] = [] content[:inline_attachments] = [] mail..each do || = { 'Content-Type' => .content_type.split(';')[0], 'Filename' => .filename, 'content' => Base64.encode64(.body.decoded) } if .inline? content[:inline_attachments].push() else content[:attachments].push() end end end # We do the next part in purpose to accept only custom header from the user, but without accepting the mailjet related ones # The mailjet related ones are stocked in another variable and directly added to the body if mail.header && !mail.header.fields.empty? content[:headers] = {} mail.header.fields.each do |header| content[:headers][header.name] = header.value if header.name =~ /^X-(?!\b(MJ|Mailjet)\b).*$/ end end # Reply-To is not a property in Mailjet Send API # Passing it as an header content[:headers]['Reply-To'] = mail.reply_to.join(', ') if mail.reply_to # Mailjet Send API does not support full from. Splitting the from field into two: name and email address mail[:from] ||= Mailjet.config.default_from base_from = { from_name: mail[:from].display_names.first, from_email: mail[:from].addresses.first } payload = { to: mail[:to].formatted.join(', '), sender: mail[:sender], subject: mail.subject } payload[:cc] = mail[:cc].formatted.join(', ') if mail[:cc] payload[:reply_to] = mail[:reply_to].formatted.join(', ') if mail[:reply_to] payload[:bcc] = mail[:bcc].formatted.join(', ') if mail[:bcc] # Send the final payload to Mailjet Send API payload.merge!(content, base_from, @delivery_method_options_v3_0) end |
#setContentV3_1(mail) ⇒ Object
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/mailjet/mailer.rb', line 84 def setContentV3_1(mail) content = {} content[:TextPart] = mail.text_part.try(:decoded) if !mail.text_part.blank? content[:HTMLPart] = mail.html_part.try(:decoded) if !mail.html_part.blank? # try message `body` as fallback if no content found unless content[:TextPart] || content[:HTMLPart] || mail.body.try(:raw_source).empty? content[mail.content_type.try(:include?,'text/html') ? :HTMLPart : :TextPart] = mail.body.raw_source end if mail..any? content[:Attachments] = [] content[:InlinedAttachments] = [] mail..each do || = { 'ContentType' => .content_type.split(';')[0], 'Filename' => .filename, 'Base64Content' => Base64.encode64(.body.decoded) } if .inline? ['ContentId'] = .content_id content[:InlinedAttachments].push() else content[:Attachments].push() end end end # We do the next part in purpose to accept only custom header from the user, but without accepting the mailjet related ones # The mailjet related ones are stocked in another variable and directly added to the body if mail.header && mail.header.fields.any? content[:Headers] = {} mail.header.fields.each do |header| if !header.name.start_with?('X-MJ') && !header.name.start_with?('X-Mailjet') && !HEADER_BLACKLIST.include?(header.name.downcase) content[:Headers][header.name] = header.value end end end # ReplyTo property was added in v3.1 # Passing it as an header if mail.reply_to if mail[:reply_to] if mail[:reply_to].respond_to?(:display_names) && mail[:reply_to].display_names.first content[:ReplyTo] = {:Email=> mail[:reply_to].addresses.first, :Name=> mail[:reply_to].display_names.first} else content[:ReplyTo] = {:Email=> mail[:reply_to].addresses.first} end end if (mail[:to]) if (mail[:to].is_a? String) if mail[:to].display_names.first to = [{:Email=>mail[:to].addresses.first, :Name=>mail[:to].display_names.first}] else to = [{:Email=>mail[:to].addresses.first}] end else to = [] mail[:to].each do |t| if (t.display_name) to << { :Email => t.address, :Name => t.display_name } else to << { :Email => t.address } end end end end base_from = { From:{ :Email=> mail[:from].addresses.first } } if (mail[:from].display_names.first) base_from[:From][:Name] = mail[:from].display_names.first end if (mail[:cc]) if (mail[:cc].is_a? String) if mail[:cc].display_name.first ccs =[{:Email=>mail[:cc].address.first, :Name=>mail[:cc].display_name.first}] else ccs =[{:Email=>mail[:cc].address.first}] end else ccs = [] mail[:cc].each do |cc| if cc.display_name ccs << {:Email=> cc.address, :Name=>cc.display_name} else ccs << {:Email=> cc.address} end end end end if (mail[:bcc]) if (mail[:bcc].formatted.is_a? String) if mail[:bcc].display_name.first payload[:Bcc] = [{:Email=>mail[:bcc].address.first, :Name=>mail[:bcc].display_name.first}] else payload[:Bcc] = [{:Email=>mail[:bcc].address.first}] end else bccs = [] mail[:bcc].each do |bcc| if bcc.display_name bccs << {:Email=> bcc.address, :Name=>bcc.display_name} else bccs << {:Email=> bcc.address} end end end end payload = { :To=> to, }.merge!(content, base_from, @delivery_method_options_v3_1) payload[:Subject] = mail.subject if !mail.subject.blank? payload[:Sender] = mail[:sender] if !mail[:sender].blank? payload[:Cc] = ccs if mail[:cc] payload[:Bcc] = bccs if mail[:bcc] decode_emails_V3_1!(payload) end |