Class: Courrier::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/courrier/email.rb,
lib/courrier/email/result.rb,
lib/courrier/email/address.rb,
lib/courrier/email/layouts.rb,
lib/courrier/email/options.rb,
lib/courrier/email/request.rb,
lib/courrier/email/provider.rb,
lib/courrier/email/transformer.rb,
lib/courrier/email/providers/ses.rb,
lib/courrier/email/providers/base.rb,
lib/courrier/email/providers/loops.rb,
lib/courrier/email/providers/logger.rb,
lib/courrier/email/providers/resend.rb,
lib/courrier/email/providers/mailgun.rb,
lib/courrier/email/providers/mailjet.rb,
lib/courrier/email/providers/smtp2go.rb,
lib/courrier/email/providers/mailpace.rb,
lib/courrier/email/providers/postmark.rb,
lib/courrier/email/providers/sendgrid.rb,
lib/courrier/email/providers/userlist.rb,
lib/courrier/email/providers/sparkpost.rb,
lib/courrier/email/providers/cloudflare.rb,
lib/courrier/email/providers/lettermint.rb

Defined Under Namespace

Modules: Address, Providers Classes: Layouts, Options, Provider, Request, Result, Transformer

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Email

Returns a new instance of Email.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/courrier/email.rb', line 80

def initialize(options = {})
  @provider = options[:provider] || ENV["COURRIER_PROVIDER"] || self.class.provider || Courrier.configuration&.email&.[](:provider)
  @api_key = options[:api_key] || ENV["COURRIER_API_KEY"] || self.class.api_key || Courrier.configuration&.email&.[](:api_key)

  @default_url_options = self.class.default_url_options.merge(options[:default_url_options] || {})
  @context_options = options.except(:provider, :api_key, :from, :to, :reply_to, :cc, :bcc, :subject, :text, :html)
  @options = Email::Options.new(
    options.merge(
      from: options[:from] || self.class.from || Courrier.configuration&.from,
      reply_to: options[:reply_to] || self.class.reply_to || Courrier.configuration&.reply_to,
      cc: options[:cc] || self.class.cc || Courrier.configuration&.cc,
      bcc: options[:bcc] || self.class.bcc || Courrier.configuration&.bcc,
      subject: subject,
      text: text,
      html: html,
      auto_generate_text: Courrier.configuration&.auto_generate_text,
      layouts: Courrier::Email::Layouts.new(self).build
    )
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



131
132
133
134
135
136
137
138
139
# File 'lib/courrier/email.rb', line 131

def method_missing(name, *)
  if name == :text || name == :html
    render_template(name.to_s).tap do |result|
      return result || markdown_rendered if name == :html
    end
  else
    @context_options[name]
  end
end

Class Attribute Details

.headers(**options) ⇒ Object



41
42
43
# File 'lib/courrier/email.rb', line 41

def headers(**options)
  options.empty? ? (@headers ||= {}) : @headers = options
end

.queue_optionsObject



35
36
37
# File 'lib/courrier/email.rb', line 35

def queue_options
  @queue_options ||= {}
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



13
14
15
# File 'lib/courrier/email.rb', line 13

def api_key
  @api_key
end

#default_url_optionsObject

Returns the value of attribute default_url_options.



13
14
15
# File 'lib/courrier/email.rb', line 13

def default_url_options
  @default_url_options
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/courrier/email.rb', line 13

def options
  @options
end

#providerObject

Returns the value of attribute provider.



13
14
15
# File 'lib/courrier/email.rb', line 13

def provider
  @provider
end

#queue_optionsObject

Returns the value of attribute queue_options.



13
14
15
# File 'lib/courrier/email.rb', line 13

def queue_options
  @queue_options
end

Class Method Details

.after_deliver(&block) ⇒ Object



58
59
60
# File 'lib/courrier/email.rb', line 58

def after_deliver(&block)
  (@after_deliver ||= []) << block
end

.after_deliver_callbacksObject



66
67
68
# File 'lib/courrier/email.rb', line 66

def after_deliver_callbacks
  @after_deliver || []
end

.before_deliver(&block) ⇒ Object



54
55
56
# File 'lib/courrier/email.rb', line 54

def before_deliver(&block)
  (@before_deliver ||= []) << block
end

.before_deliver_callbacksObject



62
63
64
# File 'lib/courrier/email.rb', line 62

def before_deliver_callbacks
  @before_deliver || []
end

.configure(**options) ⇒ Object Also known as: set



30
31
32
# File 'lib/courrier/email.rb', line 30

def configure(**options)
  options.each { |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end

.deliver(**options) ⇒ Object



70
71
72
# File 'lib/courrier/email.rb', line 70

def deliver(**options)
  new(options).deliver_now
end

.deliver_nowObject



73
74
75
# File 'lib/courrier/email.rb', line 73

def deliver(**options)
  new(options).deliver_now
end

.enqueue(**options) ⇒ Object Also known as: enqueue_with



45
46
47
# File 'lib/courrier/email.rb', line 45

def enqueue(**options)
  self.queue_options = options
end

.inherited(subclass) ⇒ Object



75
76
77
# File 'lib/courrier/email.rb', line 75

def inherited(subclass)
  super
end

.layout(**options) ⇒ Object



50
51
52
# File 'lib/courrier/email.rb', line 50

def layout(**options)
  self.layouts = options
end

Instance Method Details

#deliverObject Also known as: deliver_now



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/courrier/email.rb', line 101

def deliver
  if delivery_disabled?
    Courrier.configuration&.logger&.info "[Courrier] Email delivery skipped: delivery is disabled via environment variable"

    return nil
  end

  return nil if self.class.before_deliver_callbacks.any? { |callback| callback.call(self) == false }

  Provider.new(
    provider: @provider,
    api_key: @api_key,
    options: @options,
    provider_options: Courrier.configuration&.providers&.[](@provider.to_s.downcase.to_sym),
    context_options: @context_options,
    custom_headers: self.class.headers
  ).deliver.tap do |result|
    Test.record(self, result)

    self.class.after_deliver_callbacks.each { |callback| callback.call(self, result) }
  end
end