Class

SMTP

Extends:

Net::SMTP

What is This Library?

This library provides functionality to send internet mail via SMTP, the Simple Mail Transfer Protocol. For details of SMTP itself, see [RFC2821] (www.ietf.org/rfc/rfc2821.txt).

What is This Library NOT?

This library does NOT provide functions to compose internet mails. You must create them by yourself. If you want better mail support, try RubyMail or TMail. You can get both libraries from RAA. (www.ruby-lang.org/en/raa.html)

FYI: the official documentation on internet mail is: [RFC2822] (www.ietf.org/rfc/rfc2822.txt).

Examples

Sending Messages

You must open a connection to an SMTP server before sending messages. The first argument is the address of your SMTP server, and the second argument is the port number. Using SMTP.start with a block is the simplest way to do this. This way, the SMTP connection is closed automatically after the block is executed.

require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  # Use the SMTP object smtp only in this block.
end

Replace ‘your.smtp.server’ with your SMTP server. Normally your system manager or internet provider supplies a server for you.

Then you can send messages.

msgstr = <<END_OF_MESSAGE
From: Your Name <your@mail.address>
To: Destination Address <someone@example.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string@example.com>

This is a test message.
END_OF_MESSAGE

require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message msgstr,
                    'your@mail.address',
                    'his_addess@example.com'
end

Closing the Session

You MUST close the SMTP session after sending messages, by calling the #finish method:

# using SMTP#finish
smtp = Net::SMTP.start('your.smtp.server', 25)
smtp.send_message msgstr, 'from@address', 'to@address'
smtp.finish

You can also use the block form of SMTP.start/SMTP#start. This closes the SMTP session automatically:

# using block form of SMTP.start
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message msgstr, 'from@address', 'to@address'
end

I strongly recommend this scheme. This form is simpler and more robust.

HELO domain

In almost all situations, you must provide a third argument to SMTP.start/SMTP#start. This is the domain name which you are on (the host to send mail from). It is called the "HELO domain". The SMTP server will judge whether it should send or reject the SMTP session by inspecting the HELO domain.

Net::SMTP.start('your.smtp.server', 25,
                'mail.from.domain') { |smtp| ... }

SMTP Authentication

The Net::SMTP class supports three authentication schemes; PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554]) To use SMTP authentication, pass extra arguments to SMTP.start/SMTP#start.

# PLAIN
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
                'Your Account', 'Your Password', :plain)
# LOGIN
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
                'Your Account', 'Your Password', :login)

# CRAM MD5
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
                'Your Account', 'Your Password', :cram_md5)
Constants
Revision
Public Attributes
address The address of the SMTP server to connect to.
open_timeout Seconds to wait while attempting to open a connection. If the connection cannot be opened within this time, a TimeoutError is raised.
port The port number of the SMTP server to connect to.
read_timeout Seconds to wait while reading one block (by one read(2) call). If the read(2) call does not complete within this time, a TimeoutError is raised.
Public Methods
default_port The default SMTP port, port 25.
esmtp Alias for #esmtp?
esmtp= Set whether to use ESMTP or not. This should be done before calling #start. Note that if #start is called in ESMTP mode, and the connection fails due to a ProtocolError, the SMTP object will automatically switch to plain SMTP mode and retry (but not vice versa).
esmtp? true if the SMTP object uses ESMTP (which it does by default).
finish Finishes the SMTP session and closes TCP connection. Raises IOError if not started.
inspect Provide human-readable stringification of class state.
new Creates a new Net::SMTP object.
open_message_stream Opens a message writer stream and gives it to the block. The stream is valid only in the block, and has these methods:
read_timeout= Set the number of seconds to wait until timing-out a read(2) call.
ready Alias for #open_message_stream
send_mail Alias for #send_message
send_message Sends msgstr as a message. Single CR ("\r") and LF ("\n") found in the msgstr, are converted into the CR LF pair. You cannot send a binary message with this method. msgstr should include both the message headers and body.
sendmail Alias for #send_message
set_debug_output WARNING: This method causes serious security holes. Use this method for only debugging.
start Creates a new Net::SMTP object and connects to the server.
start Opens a TCP connection and starts the SMTP session.
started? true if the SMTP session has been started.
Private Methods
auth_cram_md5
auth_login
auth_plain
authenticate
base64_encode
check_auth_args auth
check_response
critical
do_finish
do_start
ehlo
get_response
getok row level library
helo SMTP command dispatcher
mailfrom
quit
rcptto
recv_response
send0
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.