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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Email

Returns a new instance of Email.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/courrier/email.rb', line 50

def initialize(options = {})
  @provider = options[:provider] || ENV["COURRIER_PROVIDER"] || self.class.provider || Courrier.configuration&.provider
  @api_key = options[:api_key] || ENV["COURRIER_API_KEY"] || self.class.api_key || Courrier.configuration&.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)



94
# File 'lib/courrier/email.rb', line 94

def method_missing(name, *) = @context_options[name]

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/courrier/email.rb', line 10

def api_key
  @api_key
end

#default_url_optionsObject

Returns the value of attribute default_url_options.



10
11
12
# File 'lib/courrier/email.rb', line 10

def default_url_options
  @default_url_options
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/courrier/email.rb', line 10

def options
  @options
end

#providerObject

Returns the value of attribute provider.



10
11
12
# File 'lib/courrier/email.rb', line 10

def provider
  @provider
end

Class Method Details

.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



25
26
27
# File 'lib/courrier/email.rb', line 25

def deliver(options = {})
  new(options).deliver_now
end

.deliver_nowObject



28
29
30
# File 'lib/courrier/email.rb', line 28

def deliver(options = {})
  new(options).deliver_now
end

.inherited(subclass) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/courrier/email.rb', line 39

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



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

def layout(options = {})
  self.layouts = options
end

Instance Method Details

#deliverObject Also known as: deliver_now



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/courrier/email.rb', line 71

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
  ).deliver
end