Class: Resend::Mailer
- Inherits:
-
Object
- Object
- Resend::Mailer
- Defined in:
- lib/resend/mailer.rb
Overview
Mailer class used by railtie
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
-
#build_resend_params(mail) ⇒ Object
Builds the payload for sending.
-
#deliver!(mail) ⇒ Object
Overwritten deliver! method.
-
#get_addons(mail) ⇒ Object
Add cc, bcc, reply_to fields.
-
#get_attachments(mail) ⇒ Object
Handle attachments when present.
-
#get_contents(mail) ⇒ Object
Gets the body of the email.
-
#get_from(input) ⇒ Object
Properly gets the ‘from` attr.
-
#get_headers(mail) ⇒ Object
Add custom headers fields.
-
#initialize(config) ⇒ Mailer
constructor
A new instance of Mailer.
Constructor Details
#initialize(config) ⇒ Mailer
Returns a new instance of Mailer.
10 11 12 13 14 15 16 |
# File 'lib/resend/mailer.rb', line 10 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
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/resend/mailer.rb', line 8 def config @config end |
#settings ⇒ Object
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
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/resend/mailer.rb', line 39 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[:attachments] = (mail) if mail..present? params.merge!(get_contents(mail)) params end |
#deliver!(mail) ⇒ Object
Overwritten deliver! method
25 26 27 28 29 30 |
# File 'lib/resend/mailer.rb', line 25 def deliver!(mail) params = build_resend_params(mail) resp = Resend::Emails.send(params) mail. = resp[:id] if resp[:error].nil? resp end |
#get_addons(mail) ⇒ Object
Add cc, bcc, reply_to fields
72 73 74 75 76 77 78 |
# File 'lib/resend/mailer.rb', line 72 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
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/resend/mailer.rb', line 122 def (mail) = [] mail..each do |part| = { filename: part.filename, content: part.body.decoded.bytes } .append() end end |
#get_contents(mail) ⇒ Object
Gets the body of the email
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/resend/mailer.rb', line 87 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
108 109 110 111 112 113 114 115 |
# File 'lib/resend/mailer.rb', line 108 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
59 60 61 62 63 |
# File 'lib/resend/mailer.rb', line 59 def get_headers(mail) params = {} params[:headers] = mail[:headers].unparsed_value if mail[:headers].present? params end |