Module: Cloudflare::EmailService

Defined in:
lib/cloudflare/email_service.rb,
lib/cloudflare/email_service/rails.rb,
lib/cloudflare/email_service/client.rb,
lib/cloudflare/email_service/errors.rb,
lib/cloudflare/email_service/message.rb,
lib/cloudflare/email_service/version.rb,
lib/cloudflare/email_service/response.rb,
lib/cloudflare/email_service/smtp_client.rb,
lib/cloudflare/email_service/configuration.rb

Overview

Send transactional email through the Cloudflare Email Service, over either the REST or the SMTP transport.

Defined Under Namespace

Modules: Rails Classes: APIError, AuthenticationError, Client, Configuration, ConfigurationError, Error, Message, NetworkError, RateLimitError, RequestError, Response, SMTPClient, ServerError, ValidationError

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.clientClient, SMTPClient

Builds the client for the configured transport (:rest or :smtp).

Returns:



42
43
44
45
46
47
48
49
# File 'lib/cloudflare/email_service.rb', line 42

def client
  case configuration.transport
  when :rest then Client.new
  when :smtp then SMTPClient.new
  else
    raise ConfigurationError, "unknown transport #{configuration.transport.inspect}"
  end
end

.configurationConfiguration

Returns the global default configuration.

Returns:



17
18
19
# File 'lib/cloudflare/email_service.rb', line 17

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Configuration

Yields the global Configuration for setup.

Cloudflare::EmailService.configure do |c|
  c. = ENV["CLOUDFLARE_ACCOUNT_ID"]
  c.api_token  = ENV["CLOUDFLARE_API_TOKEN"]
end

Yields:

Returns:



29
30
31
32
# File 'lib/cloudflare/email_service.rb', line 29

def configure
  yield configuration if block_given?
  configuration
end

.reset_configuration!Configuration

Resets the global configuration. Mainly useful in tests.

Returns:



36
37
38
# File 'lib/cloudflare/email_service.rb', line 36

def reset_configuration!
  @configuration = Configuration.new
end

.send_email(**kwargs) ⇒ Response

Convenience: build a Client from the global config and send. Accepts the same keyword arguments as Cloudflare::EmailService::Message#initialize.

Returns:



54
55
56
# File 'lib/cloudflare/email_service.rb', line 54

def send_email(**kwargs)
  client.send_email(**kwargs)
end

.smtp_settings(api_token: nil, host: nil, port: nil, open_timeout: nil, timeout: nil) ⇒ Hash

Cloudflare SMTP submission settings, in the shape both SMTPClient and ActionMailer’s built-in ‘:smtp` delivery method expect. Defaults come from the global configuration; pass keywords to override.

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudflare/email_service.rb', line 62

def smtp_settings(api_token: nil, host: nil, port: nil,
                  open_timeout: nil, timeout: nil)
  config = configuration
  {
    address: host || config.smtp_host,
    port: port || config.smtp_port,
    user_name: SMTPClient::SMTP_USERNAME,
    password: api_token || config.api_token,
    authentication: :plain,
    enable_starttls_auto: false,
    tls: true,
    open_timeout: open_timeout || config.open_timeout,
    read_timeout: timeout || config.timeout,
  }
end