Class: Resend::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/resend/mailer.rb

Overview

Mailer class used by railtie

Constant Summary collapse

IGNORED_HEADERS =

These are set as ‘headers` by the Rails API, but these will be filtered out when constructing the Resend 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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mailer

Returns a new instance of Mailer.

Raises:



23
24
25
26
27
28
29
# File 'lib/resend/mailer.rb', line 23

def initialize(config)
  @config = config
  raise Resend::Error.new("Make sure your API Key is set", @config) unless Resend.api_key

  # avoids NilError exception
  @settings = { return_response: true }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/resend/mailer.rb', line 8

def config
  @config
end

#settingsObject

Returns the value of attribute settings.



8
9
10
# File 'lib/resend/mailer.rb', line 8

def settings
  @settings
end

Instance Method Details

#build_resend_params(mail) ⇒ Object

Builds the payload for sending

Parameters:

  • Mail

    mail rails mail object

Returns:

  • Hash hash with all Resend params



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/resend/mailer.rb', line 53

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!(get_tags(mail))
  params[:attachments] = get_attachments(mail) if mail.attachments.present?
  params.merge!(get_contents(mail))
  params
end

#cleanup_headers(headers) ⇒ Object

Remove nils from header values



115
116
117
# File 'lib/resend/mailer.rb', line 115

def cleanup_headers(headers)
  headers.delete_if { |_k, v| v.nil? }
end

#deliver!(mail) ⇒ Object

Overwritten deliver! method

Parameters:

  • Mail

    mail

Returns:

  • Object resend response



38
39
40
41
42
43
44
# File 'lib/resend/mailer.rb', line 38

def deliver!(mail)
  params = build_resend_params(mail)
  options = get_options(mail) if mail[:options].present?
  resp = Resend::Emails.send(params, options: options || {})
  mail.message_id = resp[:id] if resp[:error].nil?
  resp
end

#get_addons(mail) ⇒ Object

Add cc, bcc, reply_to fields

Parameters:

  • Mail

    mail Rails Mail Object

Returns:

  • Hash hash containing cc/bcc/reply_to attrs



165
166
167
168
169
170
171
# File 'lib/resend/mailer.rb', line 165

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

Returns:

  • Array attachments array



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/resend/mailer.rb', line 215

def get_attachments(mail)
  attachments = []
  mail.attachments.each do |part|
    attachment = {
      filename: part.filename,
      content: part.body.decoded.bytes
    }
    attachments.append(attachment)
  end
  attachments
end

#get_contents(mail) ⇒ Object

Gets the body of the email

Parameters:

  • Mail

    mail Rails Mail Object

Returns:

  • Hash hash containing html/text or both attrs



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/resend/mailer.rb', line 180

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

Parameters:

  • Mail

    input object

Returns:

  • String ‘from` string



201
202
203
204
205
206
207
208
# File 'lib/resend/mailer.rb', line 201

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.

Parameters:

  • Mail

    mail Rails Mail object

Returns:

  • Hash hash with headers param



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/resend/mailer.rb', line 86

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

Parameters:

  • Mail

    mail Rails Mail object

Returns:

  • Hash hash with headers param



105
106
107
108
109
110
111
112
# File 'lib/resend/mailer.rb', line 105

def get_options(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

Parameters:

  • Mail

    mail Rails Mail object

Returns:

  • Hash hash with tags param



152
153
154
155
156
# File 'lib/resend/mailer.rb', line 152

def get_tags(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

Parameters:

  • Mail

    mail Rails Mail object

Returns:

  • Hash hash with headers values



136
137
138
139
140
141
142
143
# File 'lib/resend/mailer.rb', line 136

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

Parameters:

  • Mail

    mail Rails Mail object

Returns:

  • Hash hash with mail headers values



123
124
125
126
127
128
129
130
# File 'lib/resend/mailer.rb', line 123

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

Parameters:

  • Mail

    mail

Returns:

  • Array headers



234
235
236
# File 'lib/resend/mailer.rb', line 234

def unignored_headers(mail)
  @unignored_headers ||= mail.header_fields.reject { |h| IGNORED_HEADERS.include?(h.name.downcase) }
end