Class: EmailFuse::Mailer
- Inherits:
-
Object
- Object
- EmailFuse::Mailer
- Defined in:
- lib/email_fuse/mailer.rb
Overview
Mailer class used by railtie
Constant Summary collapse
- IGNORED_HEADERS =
These are set as
headersby the Rails API, but these will be filtered out when constructing the EmailFuse API payload, since they’re are sent as post params. resend.com/docs/api-reference/emails/send-email %w[ cc bcc from reply-to to subject mime-version html text content-type tags scheduled_at headers options ].freeze
- SUPPORTED_OPTIONS =
%w[idempotency_key].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
-
#api_key ⇒ Object
Returns the API key from settings or global config.
-
#base_url ⇒ Object
Returns the base URL from settings or global config.
-
#build_resend_params(mail) ⇒ Object
Builds the payload for sending.
-
#cleanup_headers(headers) ⇒ Object
Remove nils from header values.
-
#deliver!(mail) ⇒ Object
Overwritten deliver! method.
-
#get_addons(mail) ⇒ Object
Add cc, bcc, reply_to fields.
-
#get_attachments(mail) ⇒ Object
Handle attachments when present Uses base64 encoding for better API compatibility.
-
#get_contents(mail) ⇒ Object
Gets the body of the email.
-
#get_from(input) ⇒ Object
Properly gets the
fromattr. -
#get_headers(mail) ⇒ Object
Add custom headers fields.
-
#get_options(mail) ⇒ Object
Adds additional options fields.
-
#get_tags(mail) ⇒ Object
Add tags fields.
-
#headers_values(mail) ⇒ Object
Gets the values of the headers that are set through the
#headersmethod. -
#initialize(config) ⇒ Mailer
constructor
A new instance of Mailer.
-
#mail_headers_values(mail) ⇒ Object
Gets the values of the headers that are set through the
#mailmethod. -
#unignored_headers(mail) ⇒ Object
Get all headers that are not ignored.
Constructor Details
#initialize(config) ⇒ Mailer
Returns a new instance of Mailer.
24 25 26 27 28 29 30 |
# File 'lib/email_fuse/mailer.rb', line 24 def initialize(config) @config = config @settings = config.is_a?(Hash) ? config : {} # Check for API key in settings first, then fall back to global config raise EmailFuse::Error.new("Make sure your API Key is set", @config) unless api_key end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
9 10 11 |
# File 'lib/email_fuse/mailer.rb', line 9 def config @config end |
#settings ⇒ Object
Returns the value of attribute settings.
9 10 11 |
# File 'lib/email_fuse/mailer.rb', line 9 def settings @settings end |
Instance Method Details
#api_key ⇒ Object
Returns the API key from settings or global config
33 34 35 |
# File 'lib/email_fuse/mailer.rb', line 33 def api_key @settings[:api_key] || EmailFuse.api_key end |
#base_url ⇒ Object
Returns the base URL from settings or global config
38 39 40 |
# File 'lib/email_fuse/mailer.rb', line 38 def base_url @settings[:host] || @settings[:base_url] || EmailFuse.base_url end |
#build_resend_params(mail) ⇒ Object
Builds the payload for sending
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/email_fuse/mailer.rb', line 70 def build_resend_params(mail) params = { from: get_from(mail), to: mail.to, subject: mail.subject } params.merge!(get_addons(mail)) params.merge!(get_headers(mail)) params.merge!((mail)) params[:attachments] = (mail) if mail..present? params.merge!(get_contents(mail)) params end |
#cleanup_headers(headers) ⇒ Object
Remove nils from header values
132 133 134 |
# File 'lib/email_fuse/mailer.rb', line 132 def cleanup_headers(headers) headers.delete_if { |_k, v| v.nil? } end |
#deliver!(mail) ⇒ Object
Overwritten deliver! method
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/email_fuse/mailer.rb', line 49 def deliver!(mail) params = build_resend_params(mail) = (mail) if mail[:options].present? ||= {} # Pass api_key and base_url from settings to the request [:api_key] = api_key [:base_url] = base_url resp = EmailFuse::Emails.send(params, options: ) mail. = resp[:id] if resp[:error].nil? resp end |
#get_addons(mail) ⇒ Object
Add cc, bcc, reply_to fields
182 183 184 185 186 187 188 |
# File 'lib/email_fuse/mailer.rb', line 182 def get_addons(mail) params = {} params[:cc] = mail.cc if mail.cc.present? params[:bcc] = mail.bcc if mail.bcc.present? params[:reply_to] = mail.reply_to if mail.reply_to.present? params end |
#get_attachments(mail) ⇒ Object
Handle attachments when present Uses base64 encoding for better API compatibility
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/email_fuse/mailer.rb', line 233 def (mail) = [] mail..each do |part| # Get decoded content and ensure binary encoding for consistent base64 output content = part.body.decoded.dup content = content.force_encoding(Encoding::BINARY) = { filename: part.filename, content: Base64.strict_encode64(content), content_type: part.content_type } # Rails uses the auto generated cid for inline attachments [:content_id] = part.cid if part.inline? .append() end end |
#get_contents(mail) ⇒ Object
Gets the body of the email
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/email_fuse/mailer.rb', line 197 def get_contents(mail) params = {} case mail.mime_type when "text/plain" params[:text] = mail.body.decoded when "text/html" params[:html] = mail.body.decoded when "multipart/alternative", "multipart/mixed", "multipart/related" params[:text] = mail.text_part.decoded if mail.text_part params[:html] = mail.html_part.decoded if mail.html_part end params end |
#get_from(input) ⇒ Object
Properly gets the from attr
218 219 220 221 222 223 224 225 |
# File 'lib/email_fuse/mailer.rb', line 218 def get_from(input) return input.from.first if input[:from].nil? from = input[:from].formatted return from.first if from.is_a? Array from.to_s end |
#get_headers(mail) ⇒ Object
Add custom headers fields.
Both ways are supported:
1. Through the `#mail()` method ie:
mail(headers: { "X-Custom-Header" => "value" })
2. Through the Rails `#headers` method ie:
headers["X-Custom-Header"] = "value"
setting the header values through the #mail method will overwrite values set through the #headers method using the same key.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/email_fuse/mailer.rb', line 103 def get_headers(mail) params = {} if mail[:headers].present? || unignored_headers(mail).present? params[:headers] = {} params[:headers].merge!(headers_values(mail)) if unignored_headers(mail).present? params[:headers].merge!(mail_headers_values(mail)) if mail[:headers].present? end params end |
#get_options(mail) ⇒ Object
Adds additional options fields. Currently supports only :idempotency_key
122 123 124 125 126 127 128 129 |
# File 'lib/email_fuse/mailer.rb', line 122 def (mail) opts = {} if mail[:options].present? opts.merge!(mail[:options].unparsed_value) opts.delete_if { |k, _v| !SUPPORTED_OPTIONS.include?(k.to_s) } end opts end |
#get_tags(mail) ⇒ Object
Add tags fields
169 170 171 172 173 |
# File 'lib/email_fuse/mailer.rb', line 169 def (mail) params = {} params[:tags] = mail[:tags].unparsed_value if mail[:tags].present? params end |
#headers_values(mail) ⇒ Object
Gets the values of the headers that are set through the #headers method
153 154 155 156 157 158 159 160 |
# File 'lib/email_fuse/mailer.rb', line 153 def headers_values(mail) params = {} unignored_headers(mail).each do |h| params[h.name.to_s] = h.unparsed_value end cleanup_headers(params) params end |
#mail_headers_values(mail) ⇒ Object
Gets the values of the headers that are set through the #mail method
140 141 142 143 144 145 146 147 |
# File 'lib/email_fuse/mailer.rb', line 140 def mail_headers_values(mail) params = {} mail[:headers].unparsed_value.each do |k, v| params[k.to_s] = v end cleanup_headers(params) params end |
#unignored_headers(mail) ⇒ Object
Get all headers that are not ignored
260 261 262 |
# File 'lib/email_fuse/mailer.rb', line 260 def unignored_headers(mail) @unignored_headers ||= mail.header_fields.reject { |h| IGNORED_HEADERS.include?(h.name.downcase) } end |