Class: EventMachine::Protocols::SmtpClient

  • Object
show all
Deferrable, LineText2
Defined in:


Simple SMTP client

Sending generated emails (using mailfactory)

mail = = '' mail.from = '' mail.subject = 'hi!' mail.text = 'hello world' mail.html = '

hello world


email = EM::P::SmtpClient.send(




 = EM::Protocols::SmtpClient.send(
  :port=>25, # optional, defaults 25
  :starttls=>true, # use ssl
  :to=> ["", ""],
  :header=> {"Subject" => "This is a subject line"},
  :body=> "This is the body of the email"
  puts 'Email sent!'
.errback{ |e|
  puts 'Email failed!'

Constant Summary

Constants included from LineText2

MaxBinaryLength, MaxLineLength

Instance Attribute Summary (collapse)

Attributes inherited from Connection


Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from LineText2

#receive_binary_data, #receive_data, #receive_end_of_binary_data, #set_binary_mode, #set_delimiter, #set_line_mode, #set_text_mode

Methods included from Deferrable

#callback, #cancel_callback, #cancel_errback, #cancel_timeout, #errback, #fail, future, #set_deferred_status, #succeed, #timeout

Methods inherited from Connection

#close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #detach, #error?, #get_idle_time, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sock_opt, #get_sockname, #get_status, #notify_readable=, #notify_readable?, #notify_writable=, #notify_writable?, #pause, #paused?, #pending_connect_timeout=, #proxy_completed, #proxy_incoming_to, #proxy_target_unbound, #receive_data, #resume, #send_data, #send_datagram, #send_file_data, #set_sock_opt, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data

Constructor Details

- (SmtpClient) initialize

A new instance of SmtpClient

# File 'lib/em/protocols/smtpclient.rb', line 71

def initialize
  @succeeded = nil
  @responder = nil
  @code = nil
  @msg = nil

Instance Attribute Details

- (Object) args=(value) (writeonly)

Sets the attribute args


  • value

    the value to set the attribute args to.

# File 'lib/em/protocols/smtpclient.rb', line 165

def args=(value)
  @args = value

Class Method Details

+ (Object) send(args = {})

:host => required String a string containing the IP address or host name of the SMTP server to connect to. :port => optional defaults to 25. :domain => required String This is passed as the argument to the EHLO command. :starttls => optional Boolean If it evaluates true, then the client will initiate STARTTLS with the server, and abort the connection if the negotiation doesn't succeed. TODO, need to be able to pass certificate parameters with this option. :auth => optional Hash of auth parameters If not given, then no auth will be attempted. (In that case, the connection will be aborted if the server requires auth.) Specify the hash value :type to determine the auth type, along with additional parameters depending on the type. Currently only :type => :plain is supported. Pass additional parameters :username (String), and :password (either a String or a Proc that will be called at auth-time).


:auth => {:type=>:plain, :username=>"", :password=>"mouse"}

:from => required String Specifies the sender of the message. Will be passed as the argument to the MAIL FROM. Do NOT enclose the argument in angle-bracket (<>) characters. The connection will abort if the server rejects the value. :to => required String or Array of Strings The recipient(s) of the message. Do NOT enclose any of the values in angle-brackets (<>) characters. It's NOT a fatal error if one or more recipients are rejected by the server. (Of course, if ALL of them are, the server will most likely trigger an error when we try to send data.) An array of codes containing the status of each requested recipient is available after the call completes. TODO, we should define an overridable stub that will be called on rejection of a recipient or a sender, giving user code the chance to try again or abort the connection. :header => Required hash of values to be transmitted in the header of the message. The hash keys are the names of the headers (do NOT append a trailing colon), and the values are strings containing the header values. TODO, support Arrays of header values, which would cause us to send that specific header line more than once.


:header => {"Subject" => "Bogus", "CC" => ""}

:body => Optional string, defaults blank. This will be passed as the body of the email message. TODO, this needs to be significantly beefed up. As currently written, this requires the caller to properly format the input into CRLF-delimited lines of 7-bit characters in the standard SMTP transmission format. We need to be able to automatically convert binary data, and add correct line-breaks to text data. I think the :body parameter should remain as it is, and we should add a :content parameter that contains autoconversions and/or conversion parameters. Then we can check if either :body or :content is present and do the right thing. :content => Optional array or string Alternative to providing header and body, an array or string of raw data which MUST be in correct SMTP body format, including a trailing dot line :verbose => Optional. If true, will cause a lot of information (including the server-side of the conversation) to be dumped to $>.

# File 'lib/em/protocols/smtpclient.rb', line 134

def self.send args={}