Class: Cloudflare::EmailService::SMTPClient
- Inherits:
-
Object
- Object
- Cloudflare::EmailService::SMTPClient
- Defined in:
- lib/cloudflare/email_service/smtp_client.rb
Overview
Delivers a Message over Cloudflare’s SMTP submission endpoint (smtp.mx.cloudflare.net:465, implicit TLS).
MIME assembly is delegated to the ‘mail` gem, which is an optional dependency: it is required lazily the first time an SMTP client is built, so the REST transport stays dependency-free.
client = Cloudflare::EmailService::SMTPClient.new(api_token: "...")
client.send_email(from: "a@x.com", to: "b@y.com",
subject: "Hi", text: "Hello")
Constant Summary collapse
- SMTP_USERNAME =
Cloudflare requires the literal string “api_token” as the SMTP username; the password is the API token itself.
"api_token"
Instance Attribute Summary collapse
-
#api_token ⇒ Object
readonly
Returns the value of attribute api_token.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#deliver(message) ⇒ Response
Sends a pre-built Message over SMTP.
-
#initialize(api_token: nil, host: nil, port: nil, open_timeout: nil, timeout: nil) ⇒ SMTPClient
constructor
A new instance of SMTPClient.
-
#send_email(**kwargs) ⇒ Response
Builds and sends a message.
Constructor Details
#initialize(api_token: nil, host: nil, port: nil, open_timeout: nil, timeout: nil) ⇒ SMTPClient
Returns a new instance of SMTPClient.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 22 def initialize(api_token: nil, host: nil, port: nil, open_timeout: nil, timeout: nil) config = EmailService.configuration @api_token = api_token || config.api_token @host = host || config.smtp_host @port = port || config.smtp_port @open_timeout = open_timeout || config.open_timeout @timeout = timeout || config.timeout raise ConfigurationError, "no api_token configured" if @api_token.to_s.empty? load_dependencies! end |
Instance Attribute Details
#api_token ⇒ Object (readonly)
Returns the value of attribute api_token.
20 21 22 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 20 def api_token @api_token end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
20 21 22 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 20 def host @host end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
20 21 22 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 20 def open_timeout @open_timeout end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
20 21 22 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 20 def port @port end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
20 21 22 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 20 def timeout @timeout end |
Instance Method Details
#deliver(message) ⇒ Response
Sends a pre-built Message over SMTP.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 45 def deliver() .validate! envelope = build_envelope() envelope.delivery_method(:smtp, smtp_settings) transmit(envelope) accepted(envelope) rescue Net::SMTPAuthenticationError => e raise AuthenticationError, e. rescue Net::SMTPError => e # Every other SMTP protocol error (busy, syntax, fatal, unknown, ...). raise ServerError, e. rescue Timeout::Error, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError, IOError => e raise NetworkError, "SMTP delivery failed: #{e.class}: #{e.}" end |
#send_email(**kwargs) ⇒ Response
Builds and sends a message. Accepts the same keyword arguments as Message#initialize.
39 40 41 |
# File 'lib/cloudflare/email_service/smtp_client.rb', line 39 def send_email(**kwargs) deliver(Message.new(**kwargs)) end |