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/inbound.rb,
lib/cloudflare/email_service/message.rb,
lib/cloudflare/email_service/railtie.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: Inbound, Rails Classes: APIError, AuthenticationError, Client, Configuration, ConfigurationError, Error, Message, NetworkError, Railtie, RateLimitError, RequestError, Response, SMTPClient, ServerError, ValidationError

Constant Summary collapse

VERSION =
"0.1.0"

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_emailResponse

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(**)
  client.send_email(**)
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)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloudflare/email_service.rb', line 70

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

.worker_template_pathString

Absolute path to the shipped Cloudflare Email Worker template that signs and forwards inbound mail to the Action Mailbox ingress. Set your app URL and secret in it, then deploy it.

Returns:

  • (String)


62
63
64
# File 'lib/cloudflare/email_service.rb', line 62

def worker_template_path
  File.expand_path("../../templates/cloudflare_email_worker.js", __dir__)
end