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/base.rb,
lib/courrier/email/providers/inbox.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/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

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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/courrier/email.rb', line 75

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)



144
145
146
147
148
149
150
151
152
# File 'lib/courrier/email.rb', line 144

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



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

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

.queue_optionsObject



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

def queue_options
  @queue_options ||= {}
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#default_url_optionsObject

Returns the value of attribute default_url_options.



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

def default_url_options
  @default_url_options
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#providerObject

Returns the value of attribute provider.



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

def provider
  @provider
end

#queue_optionsObject

Returns the value of attribute queue_options.



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

def queue_options
  @queue_options
end

Class Method Details

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



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

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

.deliver(**options) ⇒ Object



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

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

.deliver_later(**options) ⇒ Object



60
61
62
# File 'lib/courrier/email.rb', line 60

def deliver_later(**options)
  new(options).deliver_later
end

.deliver_nowObject



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

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

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



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

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

.inherited(subclass) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/courrier/email.rb', line 64

def inherited(subclass)
  super

  # If you read this and know how to move this Rails-specific logic somewhere
  # else, e.g. `lib/courrier/railtie.rb`, open a PR ❤️
  if defined?(Rails) && Rails.application
    subclass.include Rails.application.routes.url_helpers
  end
end

.layout(**options) ⇒ Object



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

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

Instance Method Details

#deliverObject Also known as: deliver_now



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/courrier/email.rb', line 96

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

    return nil
  end

  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
end

#deliver_laterObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/courrier/email.rb', line 114

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

    return nil
  end

  data = {
    email_class: self.class.name,
    provider: @provider,
    api_key: @api_key,
    options: @options.to_h,
    provider_options: Courrier.configuration&.providers&.[](@provider.to_s.downcase.to_sym),
    context_options: @context_options
  }

  job = Courrier::Jobs::EmailDeliveryJob
  job = job.set(**self.class.queue_options) if self.class.queue_options.any?

  job.perform_later(data)
rescue => error
  raise Courrier::BackgroundDeliveryError, "Failed to enqueue email: #{error.message}"
end