2003-08-06 15:37:55 +04:00
|
|
|
# = net/smtp.rb
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2007-03-05 01:14:23 +03:00
|
|
|
# Copyright (c) 1999-2007 Yukihiro Matsumoto.
|
2004-03-06 20:08:21 +03:00
|
|
|
#
|
2007-03-05 01:14:23 +03:00
|
|
|
# Copyright (c) 1999-2007 Minero Aoki.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# Written & maintained by Minero Aoki <aamine@loveruby.net>.
|
|
|
|
#
|
|
|
|
# Documented by William Webber and Minero Aoki.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# This program is free software. You can re-distribute and/or
|
2004-03-06 20:08:21 +03:00
|
|
|
# modify this program under the same terms as Ruby itself.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-03-06 20:08:21 +03:00
|
|
|
# NOTE: You can find Japanese version of this document at:
|
2008-04-15 12:12:30 +04:00
|
|
|
# http://www.ruby-lang.org/ja/man/html/net_smtp.html
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# $Id$
|
2004-04-21 13:40:48 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# See Net::SMTP for documentation.
|
|
|
|
#
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
require 'net/protocol'
|
2001-12-01 17:07:01 +03:00
|
|
|
require 'digest/md5'
|
2004-03-06 20:08:21 +03:00
|
|
|
require 'timeout'
|
2004-04-21 13:40:48 +04:00
|
|
|
begin
|
2006-08-15 06:21:59 +04:00
|
|
|
require 'openssl'
|
2004-04-21 13:40:48 +04:00
|
|
|
rescue LoadError
|
|
|
|
end
|
2000-06-12 20:42:46 +04:00
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
module Net
|
2000-06-12 20:42:46 +04:00
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# Module mixed in to all SMTP error classes
|
2003-07-02 06:34:39 +04:00
|
|
|
module SMTPError
|
2007-03-05 01:14:23 +03:00
|
|
|
# This *class* is a module for backward compatibility.
|
|
|
|
# In later release, this module becomes a class.
|
2003-07-02 06:34:39 +04:00
|
|
|
end
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# Represents an SMTP authentication error.
|
2003-07-02 06:34:39 +04:00
|
|
|
class SMTPAuthenticationError < ProtoAuthError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# Represents SMTP error code 420 or 450, a temporary error.
|
2003-07-02 06:34:39 +04:00
|
|
|
class SMTPServerBusy < ProtoServerError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# Represents an SMTP command syntax error (error code 500)
|
2003-07-02 06:34:39 +04:00
|
|
|
class SMTPSyntaxError < ProtoSyntaxError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# Represents a fatal SMTP error (error code 5xx, except for 500)
|
2003-07-02 06:34:39 +04:00
|
|
|
class SMTPFatalError < ProtoFatalError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# Unexpected reply code returned from server.
|
2003-07-02 06:34:39 +04:00
|
|
|
class SMTPUnknownError < ProtoUnknownError
|
|
|
|
include SMTPError
|
|
|
|
end
|
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
# Command is not supported on server.
|
|
|
|
class SMTPUnsupportedCommand < ProtocolError
|
|
|
|
include SMTPError
|
|
|
|
end
|
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# = Net::SMTP
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# == What is This Library?
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# This library provides functionality to send internet
|
|
|
|
# mail via SMTP, the Simple Mail Transfer Protocol. For details of
|
|
|
|
# SMTP itself, see [RFC2821] (http://www.ietf.org/rfc/rfc2821.txt).
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# == What is This Library NOT?
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# 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.
|
|
|
|
# (http://www.ruby-lang.org/en/raa.html)
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# FYI: the official documentation on internet mail is: [RFC2822] (http://www.ietf.org/rfc/rfc2822.txt).
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# == Examples
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Sending Messages
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# You must open a connection to an SMTP server before sending messages.
|
2009-03-06 06:56:38 +03:00
|
|
|
# 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
|
2004-05-09 18:42:43 +04:00
|
|
|
# after the block is executed.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# require 'net/smtp'
|
|
|
|
# Net::SMTP.start('your.smtp.server', 25) do |smtp|
|
|
|
|
# # Use the SMTP object smtp only in this block.
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# Replace 'your.smtp.server' with your SMTP server. Normally
|
|
|
|
# your system manager or internet provider supplies a server
|
|
|
|
# for you.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# Then you can send messages.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# 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>
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# This is a test message.
|
|
|
|
# END_OF_MESSAGE
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# require 'net/smtp'
|
|
|
|
# Net::SMTP.start('your.smtp.server', 25) do |smtp|
|
|
|
|
# smtp.send_message msgstr,
|
|
|
|
# 'your@mail.address',
|
2010-03-20 06:30:59 +03:00
|
|
|
# 'his_address@example.com'
|
2004-05-09 18:42:43 +04:00
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Closing the Session
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
|
|
|
# You MUST close the SMTP session after sending messages, by calling
|
2004-05-09 18:42:43 +04:00
|
|
|
# the #finish method:
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# # using SMTP#finish
|
|
|
|
# smtp = Net::SMTP.start('your.smtp.server', 25)
|
|
|
|
# smtp.send_message msgstr, 'from@address', 'to@address'
|
|
|
|
# smtp.finish
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# You can also use the block form of SMTP.start/SMTP#start. This closes
|
|
|
|
# the SMTP session automatically:
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# # using block form of SMTP.start
|
|
|
|
# Net::SMTP.start('your.smtp.server', 25) do |smtp|
|
|
|
|
# smtp.send_message msgstr, 'from@address', 'to@address'
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# I strongly recommend this scheme. This form is simpler and more robust.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === HELO domain
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# 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.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# Net::SMTP.start('your.smtp.server', 25,
|
|
|
|
# 'mail.from.domain') { |smtp| ... }
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === SMTP Authentication
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# The Net::SMTP class supports three authentication schemes;
|
|
|
|
# PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])
|
2009-03-06 06:56:38 +03:00
|
|
|
# To use SMTP authentication, pass extra arguments to
|
2004-05-09 18:42:43 +04:00
|
|
|
# SMTP.start/SMTP#start.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# # 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)
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# # CRAM MD5
|
|
|
|
# Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
|
|
|
|
# 'Your Account', 'Your Password', :cram_md5)
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
2003-07-02 06:34:39 +04:00
|
|
|
class SMTP
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
Revision = %q$Revision$.split[1]
|
2004-05-09 18:42:43 +04:00
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
# The default SMTP port number, 25.
|
2003-05-02 18:35:01 +04:00
|
|
|
def SMTP.default_port
|
|
|
|
25
|
|
|
|
end
|
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
# The default mail submission port number, 587.
|
|
|
|
def SMTP.default_submission_port
|
2006-08-15 06:21:59 +04:00
|
|
|
587
|
|
|
|
end
|
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
# The default SMTPS port number, 465.
|
|
|
|
def SMTP.default_tls_port
|
|
|
|
465
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
class << self
|
|
|
|
alias default_ssl_port default_tls_port
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
def SMTP.default_ssl_context
|
2007-03-05 01:14:23 +03:00
|
|
|
OpenSSL::SSL::SSLContext.new
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
|
|
|
# Creates a new Net::SMTP object.
|
|
|
|
#
|
|
|
|
# +address+ is the hostname or ip address of your SMTP
|
|
|
|
# server. +port+ is the port to connect to; it defaults to
|
|
|
|
# port 25.
|
|
|
|
#
|
|
|
|
# This method does not open the TCP connection. You can use
|
|
|
|
# SMTP.start instead of SMTP.new if you want to do everything
|
|
|
|
# at once. Otherwise, follow SMTP.new with SMTP#start.
|
|
|
|
#
|
2004-04-21 13:23:56 +04:00
|
|
|
def initialize(address, port = nil)
|
2003-05-02 18:35:01 +04:00
|
|
|
@address = address
|
2003-07-02 06:34:39 +04:00
|
|
|
@port = (port || SMTP.default_port)
|
2000-04-18 13:39:02 +04:00
|
|
|
@esmtp = true
|
2007-03-05 03:17:38 +03:00
|
|
|
@capabilities = nil
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket = nil
|
|
|
|
@started = false
|
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
2003-07-02 06:34:39 +04:00
|
|
|
@error_occured = false
|
2003-05-02 18:35:01 +04:00
|
|
|
@debug_output = nil
|
2007-03-05 01:14:23 +03:00
|
|
|
@tls = false
|
|
|
|
@starttls = false
|
|
|
|
@ssl_context = nil
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# Provide human-readable stringification of class state.
|
2003-05-02 18:35:01 +04:00
|
|
|
def inspect
|
2003-07-02 06:34:39 +04:00
|
|
|
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
|
2000-04-18 13:39:02 +04:00
|
|
|
end
|
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# +true+ if the SMTP object uses ESMTP (which it does by default).
|
2002-02-19 15:33:52 +03:00
|
|
|
def esmtp?
|
|
|
|
@esmtp
|
|
|
|
end
|
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# Set whether to use ESMTP or not. This should be done before
|
2003-08-06 15:37:55 +04:00
|
|
|
# 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).
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2004-04-21 13:23:56 +04:00
|
|
|
def esmtp=(bool)
|
2002-02-19 15:33:52 +03:00
|
|
|
@esmtp = bool
|
|
|
|
end
|
|
|
|
|
|
|
|
alias esmtp esmtp?
|
2000-04-18 13:39:02 +04:00
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
# true if server advertises STARTTLS.
|
|
|
|
# You cannot get valid value before opening SMTP session.
|
|
|
|
def capable_starttls?
|
|
|
|
capable?('STARTTLS')
|
|
|
|
end
|
|
|
|
|
|
|
|
def capable?(key)
|
|
|
|
return nil unless @capabilities
|
|
|
|
@capabilities[key] ? true : false
|
|
|
|
end
|
|
|
|
private :capable?
|
|
|
|
|
|
|
|
# true if server advertises AUTH PLAIN.
|
|
|
|
# You cannot get valid value before opening SMTP session.
|
|
|
|
def capable_plain_auth?
|
|
|
|
auth_capable?('PLAIN')
|
|
|
|
end
|
|
|
|
|
|
|
|
# true if server advertises AUTH LOGIN.
|
|
|
|
# You cannot get valid value before opening SMTP session.
|
|
|
|
def capable_login_auth?
|
|
|
|
auth_capable?('LOGIN')
|
|
|
|
end
|
|
|
|
|
|
|
|
# true if server advertises AUTH CRAM-MD5.
|
|
|
|
# You cannot get valid value before opening SMTP session.
|
|
|
|
def capable_cram_md5_auth?
|
|
|
|
auth_capable?('CRAM-MD5')
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_capable?(type)
|
|
|
|
return nil unless @capabilities
|
|
|
|
return false unless @capabilities['AUTH']
|
|
|
|
@capabilities['AUTH'].include?(type)
|
|
|
|
end
|
|
|
|
private :auth_capable?
|
|
|
|
|
|
|
|
# Returns supported authentication methods on this server.
|
|
|
|
# You cannot get valid value before opening SMTP session.
|
|
|
|
def capable_auth_types
|
|
|
|
return [] unless @capabilities
|
|
|
|
return [] unless @capabilities['AUTH']
|
|
|
|
@capabilities['AUTH']
|
|
|
|
end
|
|
|
|
|
|
|
|
# true if this object uses SMTP/TLS (SMTPS).
|
2007-03-05 01:14:23 +03:00
|
|
|
def tls?
|
2006-08-15 06:21:59 +04:00
|
|
|
@tls
|
|
|
|
end
|
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
alias ssl? tls?
|
|
|
|
|
|
|
|
# Enables SMTP/TLS (SMTPS: SMTP over direct TLS connection) for
|
|
|
|
# this object. Must be called before the connection is established
|
|
|
|
# to have any effect. +context+ is a OpenSSL::SSL::SSLContext object.
|
|
|
|
def enable_tls(context = SMTP.default_ssl_context)
|
2006-08-15 06:21:59 +04:00
|
|
|
raise 'openssl library not installed' unless defined?(OpenSSL)
|
2007-03-05 01:14:23 +03:00
|
|
|
raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @starttls
|
|
|
|
@tls = true
|
2006-08-15 06:21:59 +04:00
|
|
|
@ssl_context = context
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
2007-03-05 01:14:23 +03:00
|
|
|
|
|
|
|
alias enable_ssl enable_tls
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
# Disables SMTP/TLS for this object. Must be called before the
|
2006-08-15 06:21:59 +04:00
|
|
|
# connection is established to have any effect.
|
2007-03-05 01:14:23 +03:00
|
|
|
def disable_tls
|
|
|
|
@tls = false
|
2006-08-15 06:21:59 +04:00
|
|
|
@ssl_context = nil
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
2007-03-05 01:14:23 +03:00
|
|
|
|
|
|
|
alias disable_ssl disable_tls
|
|
|
|
|
|
|
|
# Returns truth value if this object uses STARTTLS.
|
|
|
|
# If this object always uses STARTTLS, returns :always.
|
|
|
|
# If this object uses STARTTLS when the server support TLS, returns :auto.
|
|
|
|
def starttls?
|
|
|
|
@starttls
|
|
|
|
end
|
2007-03-05 03:17:38 +03:00
|
|
|
|
|
|
|
# true if this object uses STARTTLS.
|
|
|
|
def starttls_always?
|
|
|
|
@starttls == :always
|
|
|
|
end
|
|
|
|
|
|
|
|
# true if this object uses STARTTLS when server advertises STARTTLS.
|
|
|
|
def starttls_auto?
|
|
|
|
@starttls == :auto
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
# Enables SMTP/TLS (STARTTLS) for this object.
|
|
|
|
# +context+ is a OpenSSL::SSL::SSLContext object.
|
2007-03-05 01:14:23 +03:00
|
|
|
def enable_starttls(context = SMTP.default_ssl_context)
|
2006-08-15 06:21:59 +04:00
|
|
|
raise 'openssl library not installed' unless defined?(OpenSSL)
|
2007-03-05 01:14:23 +03:00
|
|
|
raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @tls
|
|
|
|
@starttls = :always
|
|
|
|
@ssl_context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
# Enables SMTP/TLS (STARTTLS) for this object if server accepts.
|
|
|
|
# +context+ is a OpenSSL::SSL::SSLContext object.
|
|
|
|
def enable_starttls_auto(context = SMTP.default_ssl_context)
|
|
|
|
raise 'openssl library not installed' unless defined?(OpenSSL)
|
|
|
|
raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @tls
|
|
|
|
@starttls = :auto
|
2006-08-15 06:21:59 +04:00
|
|
|
@ssl_context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
# Disables SMTP/TLS (STARTTLS) for this object. Must be called
|
|
|
|
# before the connection is established to have any effect.
|
2007-03-05 01:14:23 +03:00
|
|
|
def disable_starttls
|
|
|
|
@starttls = false
|
2006-08-15 06:21:59 +04:00
|
|
|
@ssl_context = nil
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# The address of the SMTP server to connect to.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :address
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# The port number of the SMTP server to connect to.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :port
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# Seconds to wait while attempting to open a connection.
|
|
|
|
# If the connection cannot be opened within this time, a
|
|
|
|
# TimeoutError is raised.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_accessor :open_timeout
|
2003-08-06 15:37:55 +04:00
|
|
|
|
|
|
|
# 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.
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :read_timeout
|
2000-02-21 18:25:37 +03:00
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# Set the number of seconds to wait until timing-out a read(2)
|
|
|
|
# call.
|
2004-04-21 13:23:56 +04:00
|
|
|
def read_timeout=(sec)
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket.read_timeout = sec if @socket
|
|
|
|
@read_timeout = sec
|
|
|
|
end
|
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# WARNING: This method causes serious security holes.
|
|
|
|
# Use this method for only debugging.
|
|
|
|
#
|
|
|
|
# Set an output stream for debug logging.
|
|
|
|
# You must call this before #start.
|
|
|
|
#
|
|
|
|
# # example
|
|
|
|
# smtp = Net::SMTP.new(addr, port)
|
|
|
|
# smtp.set_debug_output $stderr
|
2004-05-09 18:42:43 +04:00
|
|
|
# smtp.start do |smtp|
|
2003-08-06 15:37:55 +04:00
|
|
|
# ....
|
2004-05-09 18:42:43 +04:00
|
|
|
# end
|
|
|
|
#
|
2006-08-15 06:21:59 +04:00
|
|
|
def debug_output=(arg)
|
2003-05-02 18:35:01 +04:00
|
|
|
@debug_output = arg
|
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
alias set_debug_output debug_output=
|
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
#
|
|
|
|
# SMTP session control
|
|
|
|
#
|
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# Creates a new Net::SMTP object and connects to the server.
|
|
|
|
#
|
|
|
|
# This method is equivalent to:
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# Net::SMTP.new(address, port).start(helo_domain, account, password, authtype)
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# Net::SMTP.start('your.smtp.server') do |smtp|
|
|
|
|
# smtp.send_message msgstr, 'from@example.com', ['dest@example.com']
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === Block Usage
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
|
|
|
# If called with a block, the newly-opened Net::SMTP object is yielded
|
|
|
|
# to the block, and automatically closed when the block finishes. If called
|
|
|
|
# without a block, the newly-opened Net::SMTP object is returned to
|
|
|
|
# the caller, and it is the caller's responsibility to close it when
|
|
|
|
# finished.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Parameters
|
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +address+ is the hostname or ip address of your smtp server.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +port+ is the port to connect to; it defaults to port 25.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +helo+ is the _HELO_ _domain_ provided by the client to the
|
2009-03-06 06:56:38 +03:00
|
|
|
# server (see overview comments); it defaults to 'localhost'.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# The remaining arguments are used for SMTP authentication, if required
|
|
|
|
# or desired. +user+ is the account name; +secret+ is your password
|
|
|
|
# or other authentication token; and +authtype+ is the authentication
|
|
|
|
# type, one of :plain, :login, or :cram_md5. See the discussion of
|
|
|
|
# SMTP Authentication in the overview notes.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Errors
|
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPAuthenticationError
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
|
|
|
# * IOError
|
|
|
|
# * TimeoutError
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2008-07-03 07:54:25 +04:00
|
|
|
def SMTP.start(address, port = nil, helo = 'localhost',
|
2004-04-21 13:23:56 +04:00
|
|
|
user = nil, secret = nil, authtype = nil,
|
|
|
|
&block) # :yield: smtp
|
2003-05-02 18:35:01 +04:00
|
|
|
new(address, port).start(helo, user, secret, authtype, &block)
|
|
|
|
end
|
|
|
|
|
2003-08-06 15:37:55 +04:00
|
|
|
# +true+ if the SMTP session has been started.
|
2003-05-02 18:35:01 +04:00
|
|
|
def started?
|
|
|
|
@started
|
|
|
|
end
|
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# Opens a TCP connection and starts the SMTP session.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Parameters
|
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +helo+ is the _HELO_ _domain_ that you'll dispatch mails from; see
|
|
|
|
# the discussion in the overview notes.
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# If both of +user+ and +secret+ are given, SMTP authentication
|
|
|
|
# will be attempted using the AUTH command. +authtype+ specifies
|
2004-05-09 18:42:43 +04:00
|
|
|
# the type of authentication to attempt; it must be one of
|
|
|
|
# :login, :plain, and :cram_md5. See the notes on SMTP Authentication
|
2009-03-06 06:56:38 +03:00
|
|
|
# in the overview.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
|
|
|
# === Block Usage
|
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# When this methods is called with a block, the newly-started SMTP
|
|
|
|
# object is yielded to the block, and automatically closed after
|
2009-03-06 06:56:38 +03:00
|
|
|
# the block call finishes. Otherwise, it is the caller's
|
2003-08-06 15:37:55 +04:00
|
|
|
# responsibility to close the session when finished.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# This is very similar to the class method SMTP.start.
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# require 'net/smtp'
|
2004-05-09 18:42:43 +04:00
|
|
|
# smtp = Net::SMTP.new('smtp.mail.server', 25)
|
|
|
|
# smtp.start(helo_domain, account, password, authtype) do |smtp|
|
|
|
|
# smtp.send_message msgstr, 'from@example.com', ['dest@example.com']
|
2009-03-06 06:56:38 +03:00
|
|
|
# end
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
|
|
|
# The primary use of this method (as opposed to SMTP.start)
|
|
|
|
# is probably to set debugging (#set_debug_output) or ESMTP
|
|
|
|
# (#esmtp=), which must be done before the session is
|
2009-03-06 06:56:38 +03:00
|
|
|
# started.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
|
|
|
# === Errors
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
|
|
|
# If session has already been started, an IOError will be raised.
|
|
|
|
#
|
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPAuthenticationError
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
|
|
|
# * IOError
|
|
|
|
# * TimeoutError
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2008-07-03 07:54:25 +04:00
|
|
|
def start(helo = 'localhost',
|
2004-05-09 18:42:43 +04:00
|
|
|
user = nil, secret = nil, authtype = nil) # :yield: smtp
|
2003-05-02 18:35:01 +04:00
|
|
|
if block_given?
|
|
|
|
begin
|
2006-08-15 06:21:59 +04:00
|
|
|
do_start helo, user, secret, authtype
|
2003-05-02 18:35:01 +04:00
|
|
|
return yield(self)
|
|
|
|
ensure
|
2003-09-29 22:12:20 +04:00
|
|
|
do_finish
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
|
|
|
else
|
2006-08-15 06:21:59 +04:00
|
|
|
do_start helo, user, secret, authtype
|
2003-05-02 18:35:01 +04:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
# Finishes the SMTP session and closes TCP connection.
|
|
|
|
# Raises IOError if not started.
|
|
|
|
def finish
|
|
|
|
raise IOError, 'not yet started' unless started?
|
|
|
|
do_finish
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def do_start(helo_domain, user, secret, authtype)
|
2003-07-02 06:34:39 +04:00
|
|
|
raise IOError, 'SMTP session already started' if @started
|
2006-08-15 06:21:59 +04:00
|
|
|
if user or secret
|
2007-03-05 03:17:38 +03:00
|
|
|
check_auth_method(authtype || DEFAULT_AUTH_TYPE)
|
2006-08-15 06:21:59 +04:00
|
|
|
check_auth_args user, secret
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
s = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
|
2006-08-15 06:21:59 +04:00
|
|
|
logging "Connection opened: #{@address}:#{@port}"
|
2007-03-05 01:14:23 +03:00
|
|
|
@socket = new_internet_message_io(tls? ? tlsconnect(s) : s)
|
2007-03-05 03:17:38 +03:00
|
|
|
check_response critical { recv_response() }
|
2006-08-15 06:21:59 +04:00
|
|
|
do_helo helo_domain
|
2007-03-05 03:17:38 +03:00
|
|
|
if starttls_always? or (capable_starttls? and starttls_auto?)
|
|
|
|
unless capable_starttls?
|
|
|
|
raise SMTPUnsupportedCommand,
|
|
|
|
"STARTTLS is not supported on this server"
|
|
|
|
end
|
2004-04-21 13:40:48 +04:00
|
|
|
starttls
|
2007-03-05 01:14:23 +03:00
|
|
|
@socket = new_internet_message_io(tlsconnect(s))
|
2004-04-21 13:40:48 +04:00
|
|
|
# helo response may be different after STARTTLS
|
2006-08-15 06:21:59 +04:00
|
|
|
do_helo helo_domain
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
2007-03-05 03:17:38 +03:00
|
|
|
authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
|
2004-04-21 13:40:48 +04:00
|
|
|
@started = true
|
|
|
|
ensure
|
|
|
|
unless @started
|
|
|
|
# authentication failed, cancel connection.
|
|
|
|
s.close if s and not s.closed?
|
|
|
|
@socket = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-05 01:14:23 +03:00
|
|
|
def tlsconnect(s)
|
|
|
|
s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
|
|
|
|
logging "TLS connection started"
|
|
|
|
s.sync_close = true
|
|
|
|
s.connect
|
2007-10-08 15:14:41 +04:00
|
|
|
if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
|
|
|
s.post_connection_check(@address)
|
|
|
|
end
|
2007-03-05 01:14:23 +03:00
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
def new_internet_message_io(s)
|
|
|
|
io = InternetMessageIO.new(s)
|
|
|
|
io.read_timeout = @read_timeout
|
|
|
|
io.debug_output = @debug_output
|
|
|
|
io
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_helo(helo_domain)
|
2007-03-05 03:17:38 +03:00
|
|
|
res = @esmtp ? ehlo(helo_domain) : helo(helo_domain)
|
|
|
|
@capabilities = res.capabilities
|
|
|
|
rescue SMTPError
|
|
|
|
if @esmtp
|
|
|
|
@esmtp = false
|
|
|
|
@error_occured = false
|
|
|
|
retry
|
2000-02-21 18:25:37 +03:00
|
|
|
end
|
2007-03-05 03:17:38 +03:00
|
|
|
raise
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2003-09-29 22:12:20 +04:00
|
|
|
|
|
|
|
def do_finish
|
2003-07-02 06:34:39 +04:00
|
|
|
quit if @socket and not @socket.closed? and not @error_occured
|
2003-09-29 22:12:20 +04:00
|
|
|
ensure
|
|
|
|
@started = false
|
|
|
|
@error_occured = false
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
@socket = nil
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
#
|
2006-08-15 06:21:59 +04:00
|
|
|
# Message Sending
|
2001-12-30 22:18:45 +03:00
|
|
|
#
|
|
|
|
|
2003-07-02 06:34:39 +04:00
|
|
|
public
|
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# 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
|
2009-03-06 06:56:38 +03:00
|
|
|
# binary message with this method. +msgstr+ should include both
|
2003-08-06 15:37:55 +04:00
|
|
|
# the message headers and body.
|
|
|
|
#
|
|
|
|
# +from_addr+ is a String representing the source mail address.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +to_addr+ is a String or Strings or Array of Strings, representing
|
|
|
|
# the destination mail address or addresses.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# Net::SMTP.start('smtp.example.com') do |smtp|
|
2003-08-06 15:37:55 +04:00
|
|
|
# smtp.send_message msgstr,
|
|
|
|
# 'from@example.com',
|
|
|
|
# ['dest@example.com', 'dest2@example.com']
|
2004-05-09 18:42:43 +04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === Errors
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
|
|
|
# * IOError
|
|
|
|
# * TimeoutError
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2004-04-21 13:23:56 +04:00
|
|
|
def send_message(msgstr, from_addr, *to_addrs)
|
2006-08-15 06:21:59 +04:00
|
|
|
raise IOError, 'closed session' unless @socket
|
|
|
|
mailfrom from_addr
|
2010-04-17 03:57:43 +04:00
|
|
|
rcptto_list(to_addrs) {data msgstr}
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-07-02 06:34:39 +04:00
|
|
|
alias send_mail send_message
|
|
|
|
alias sendmail send_message # obsolete
|
2001-12-30 22:18:45 +03:00
|
|
|
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# Opens a message writer stream and gives it to the block.
|
|
|
|
# The stream is valid only in the block, and has these methods:
|
|
|
|
#
|
|
|
|
# puts(str = ''):: outputs STR and CR LF.
|
2004-05-09 18:42:43 +04:00
|
|
|
# print(str):: outputs STR.
|
|
|
|
# printf(fmt, *args):: outputs sprintf(fmt,*args).
|
|
|
|
# write(str):: outputs STR and returns the length of written bytes.
|
|
|
|
# <<(str):: outputs STR and returns self.
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
|
|
|
# If a single CR ("\r") or LF ("\n") is found in the message,
|
|
|
|
# it is converted to the CR LF pair. You cannot send a binary
|
|
|
|
# message with this method.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Parameters
|
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +from_addr+ is a String representing the source mail address.
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
2003-08-06 15:37:55 +04:00
|
|
|
# +to_addr+ is a String or Strings or Array of Strings, representing
|
|
|
|
# the destination mail address or addresses.
|
|
|
|
#
|
2004-05-09 18:42:43 +04:00
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# Net::SMTP.start('smtp.example.com', 25) do |smtp|
|
|
|
|
# smtp.open_message_stream('from@example.com', ['dest@example.com']) do |f|
|
2003-08-06 15:37:55 +04:00
|
|
|
# f.puts 'From: from@example.com'
|
|
|
|
# f.puts 'To: dest@example.com'
|
|
|
|
# f.puts 'Subject: test message'
|
|
|
|
# f.puts
|
|
|
|
# f.puts 'This is a test message.'
|
2004-05-09 18:42:43 +04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === Errors
|
2003-08-06 15:37:55 +04:00
|
|
|
#
|
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
|
|
|
# * IOError
|
|
|
|
# * TimeoutError
|
2004-05-09 18:42:43 +04:00
|
|
|
#
|
|
|
|
def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
|
2003-07-29 13:48:30 +04:00
|
|
|
raise IOError, 'closed session' unless @socket
|
2003-07-02 06:34:39 +04:00
|
|
|
mailfrom from_addr
|
2010-04-17 03:57:43 +04:00
|
|
|
rcptto_list(to_addrs) {data(&block)}
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
alias ready open_message_stream # obsolete
|
|
|
|
|
2003-07-02 06:34:39 +04:00
|
|
|
#
|
2006-08-15 06:21:59 +04:00
|
|
|
# Authentication
|
2003-07-02 06:34:39 +04:00
|
|
|
#
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
public
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
DEFAULT_AUTH_TYPE = :plain
|
|
|
|
|
|
|
|
def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE)
|
2006-08-15 06:21:59 +04:00
|
|
|
check_auth_method authtype
|
|
|
|
check_auth_args user, secret
|
2007-11-04 23:36:20 +03:00
|
|
|
send auth_method(authtype), user, secret
|
2000-02-21 18:25:37 +03:00
|
|
|
end
|
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def auth_plain(user, secret)
|
2006-08-15 06:21:59 +04:00
|
|
|
check_auth_args user, secret
|
2007-03-05 03:17:38 +03:00
|
|
|
res = critical {
|
|
|
|
get_response('AUTH PLAIN ' + base64_encode("\0#{user}\0#{secret}"))
|
|
|
|
}
|
|
|
|
check_auth_response res
|
|
|
|
res
|
2003-05-24 22:24:26 +04:00
|
|
|
end
|
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def auth_login(user, secret)
|
2006-08-15 06:21:59 +04:00
|
|
|
check_auth_args user, secret
|
2003-05-24 22:24:26 +04:00
|
|
|
res = critical {
|
2007-03-05 03:17:38 +03:00
|
|
|
check_auth_continue get_response('AUTH LOGIN')
|
|
|
|
check_auth_continue get_response(base64_encode(user))
|
2003-07-02 06:34:39 +04:00
|
|
|
get_response(base64_encode(secret))
|
2003-05-24 22:24:26 +04:00
|
|
|
}
|
2007-03-05 03:17:38 +03:00
|
|
|
check_auth_response res
|
|
|
|
res
|
2000-04-25 13:23:21 +04:00
|
|
|
end
|
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def auth_cram_md5(user, secret)
|
2006-08-15 06:21:59 +04:00
|
|
|
check_auth_args user, secret
|
2007-03-05 03:17:38 +03:00
|
|
|
res = critical {
|
2007-05-20 22:33:25 +04:00
|
|
|
res0 = get_response('AUTH CRAM-MD5')
|
|
|
|
check_auth_continue res0
|
|
|
|
crammed = cram_md5_response(secret, res0.cram_md5_challenge)
|
2007-03-05 03:17:38 +03:00
|
|
|
get_response(base64_encode("#{user} #{crammed}"))
|
2000-04-25 13:23:21 +04:00
|
|
|
}
|
2007-03-05 03:17:38 +03:00
|
|
|
check_auth_response res
|
|
|
|
res
|
2006-08-15 06:21:59 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_auth_method(type)
|
2007-03-05 03:17:38 +03:00
|
|
|
unless respond_to?(auth_method(type), true)
|
2006-08-15 06:21:59 +04:00
|
|
|
raise ArgumentError, "wrong authentication type #{type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
def auth_method(type)
|
|
|
|
"auth_#{type.to_s.downcase}".intern
|
|
|
|
end
|
|
|
|
|
2009-05-11 03:56:22 +04:00
|
|
|
def check_auth_args(user, secret, authtype = DEFAULT_AUTH_TYPE)
|
2006-08-15 06:21:59 +04:00
|
|
|
unless user
|
|
|
|
raise ArgumentError, 'SMTP-AUTH requested but missing user name'
|
|
|
|
end
|
|
|
|
unless secret
|
|
|
|
raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
|
|
|
|
end
|
2000-04-25 13:23:21 +04:00
|
|
|
end
|
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def base64_encode(str)
|
2003-05-24 22:24:26 +04:00
|
|
|
# expects "str" may not become too long
|
|
|
|
[str].pack('m').gsub(/\s+/, '')
|
|
|
|
end
|
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
IMASK = 0x36
|
|
|
|
OMASK = 0x5c
|
|
|
|
|
|
|
|
# CRAM-MD5: [RFC2195]
|
|
|
|
def cram_md5_response(secret, challenge)
|
|
|
|
tmp = Digest::MD5.digest(cram_secret(secret, IMASK) + challenge)
|
|
|
|
Digest::MD5.hexdigest(cram_secret(secret, OMASK) + tmp)
|
|
|
|
end
|
|
|
|
|
|
|
|
CRAM_BUFSIZE = 64
|
|
|
|
|
|
|
|
def cram_secret(secret, mask)
|
|
|
|
secret = Digest::MD5.digest(secret) if secret.size > CRAM_BUFSIZE
|
|
|
|
buf = secret.ljust(CRAM_BUFSIZE, "\0")
|
2007-05-20 22:33:25 +04:00
|
|
|
0.upto(buf.size - 1) do |i|
|
2007-03-05 03:17:38 +03:00
|
|
|
buf[i] = (buf[i].ord ^ mask).chr
|
|
|
|
end
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
2003-07-02 06:34:39 +04:00
|
|
|
#
|
|
|
|
# SMTP command dispatcher
|
|
|
|
#
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
public
|
|
|
|
|
|
|
|
def starttls
|
|
|
|
getok('STARTTLS')
|
|
|
|
end
|
2003-07-02 06:34:39 +04:00
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def helo(domain)
|
2007-03-05 03:17:38 +03:00
|
|
|
getok("HELO #{domain}")
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def ehlo(domain)
|
2007-03-05 03:17:38 +03:00
|
|
|
getok("EHLO #{domain}")
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
def mailfrom(from_addr)
|
|
|
|
if $SAFE > 0
|
|
|
|
raise SecurityError, 'tainted from_addr' if from_addr.tainted?
|
|
|
|
end
|
2007-03-05 03:17:38 +03:00
|
|
|
getok("MAIL FROM:<#{from_addr}>")
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
def rcptto_list(to_addrs)
|
|
|
|
raise ArgumentError, 'mail destination not given' if to_addrs.empty?
|
2010-04-17 03:57:43 +04:00
|
|
|
ok_users = []
|
|
|
|
unknown_users = []
|
2006-08-22 14:21:31 +04:00
|
|
|
to_addrs.flatten.each do |addr|
|
2010-04-17 03:57:43 +04:00
|
|
|
begin
|
|
|
|
rcptto addr
|
|
|
|
rescue SMTPAuthenticationError
|
|
|
|
unknown_users << addr.dump
|
|
|
|
else
|
|
|
|
ok_users << addr
|
|
|
|
end
|
|
|
|
end
|
2010-04-27 13:00:52 +04:00
|
|
|
raise ArgumentError, 'mail destination not given' if ok_users.empty?
|
2010-04-17 03:57:43 +04:00
|
|
|
ret = yield
|
|
|
|
unless unknown_users.empty?
|
|
|
|
raise SMTPAuthenticationError, "failed to deliver for #{unknown_users.join(', ')}"
|
2006-08-15 06:21:59 +04:00
|
|
|
end
|
2010-04-17 03:57:43 +04:00
|
|
|
ret
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
def rcptto(to_addr)
|
|
|
|
if $SAFE > 0
|
2008-08-04 09:46:53 +04:00
|
|
|
raise SecurityError, 'tainted to_addr' if to_addr.tainted?
|
2006-08-15 06:21:59 +04:00
|
|
|
end
|
2007-03-05 03:17:38 +03:00
|
|
|
getok("RCPT TO:<#{to_addr}>")
|
2006-08-15 06:21:59 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# This method sends a message.
|
|
|
|
# If +msgstr+ is given, sends it as a message.
|
|
|
|
# If block is given, yield a message writer stream.
|
|
|
|
# You must write message before the block is closed.
|
|
|
|
#
|
|
|
|
# # Example 1 (by string)
|
|
|
|
# smtp.data(<<EndMessage)
|
|
|
|
# From: john@example.com
|
|
|
|
# To: betty@example.com
|
|
|
|
# Subject: I found a bug
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2006-08-15 06:21:59 +04:00
|
|
|
# Check vm.c:58879.
|
|
|
|
# EndMessage
|
|
|
|
#
|
|
|
|
# # Example 2 (by block)
|
|
|
|
# smtp.data {|f|
|
|
|
|
# f.puts "From: john@example.com"
|
|
|
|
# f.puts "To: betty@example.com"
|
|
|
|
# f.puts "Subject: I found a bug"
|
|
|
|
# f.puts ""
|
|
|
|
# f.puts "Check vm.c:58879."
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
def data(msgstr = nil, &block) #:yield: stream
|
|
|
|
if msgstr and block
|
|
|
|
raise ArgumentError, "message and block are exclusive"
|
|
|
|
end
|
|
|
|
unless msgstr or block
|
|
|
|
raise ArgumentError, "message or block is required"
|
|
|
|
end
|
|
|
|
res = critical {
|
2007-03-05 03:17:38 +03:00
|
|
|
check_continue get_response('DATA')
|
2006-08-15 06:21:59 +04:00
|
|
|
if msgstr
|
|
|
|
@socket.write_message msgstr
|
|
|
|
else
|
|
|
|
@socket.write_message_by_block(&block)
|
|
|
|
end
|
|
|
|
recv_response()
|
|
|
|
}
|
2007-03-05 03:17:38 +03:00
|
|
|
check_response res
|
|
|
|
res
|
2000-03-27 19:52:27 +04:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
def quit
|
|
|
|
getok('QUIT')
|
2004-04-21 13:40:48 +04:00
|
|
|
end
|
2003-07-02 06:34:39 +04:00
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
private
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
def getok(reqline)
|
2003-07-02 06:34:39 +04:00
|
|
|
res = critical {
|
2007-03-05 03:17:38 +03:00
|
|
|
@socket.writeline reqline
|
2003-07-02 06:34:39 +04:00
|
|
|
recv_response()
|
|
|
|
}
|
2007-03-05 03:17:38 +03:00
|
|
|
check_response res
|
|
|
|
res
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
1999-12-29 14:14:04 +03:00
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
def get_response(reqline)
|
|
|
|
@socket.writeline reqline
|
2003-05-02 18:35:01 +04:00
|
|
|
recv_response()
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def recv_response
|
2007-03-05 03:17:38 +03:00
|
|
|
buf = ''
|
2002-11-21 14:50:09 +03:00
|
|
|
while true
|
2003-05-02 18:35:01 +04:00
|
|
|
line = @socket.readline
|
2007-03-05 03:17:38 +03:00
|
|
|
buf << line << "\n"
|
2007-03-05 01:14:23 +03:00
|
|
|
break unless line[3,1] == '-' # "210-PIPELINING"
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2007-03-05 03:17:38 +03:00
|
|
|
Response.parse(buf)
|
2003-07-02 06:34:39 +04:00
|
|
|
end
|
|
|
|
|
2004-04-21 13:23:56 +04:00
|
|
|
def critical(&block)
|
2003-07-02 06:34:39 +04:00
|
|
|
return '200 dummy reply code' if @error_occured
|
|
|
|
begin
|
|
|
|
return yield()
|
|
|
|
rescue Exception
|
|
|
|
@error_occured = true
|
|
|
|
raise
|
|
|
|
end
|
2003-05-02 18:35:01 +04:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2007-03-05 03:17:38 +03:00
|
|
|
def check_response(res)
|
|
|
|
unless res.success?
|
|
|
|
raise res.exception_class, res.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_continue(res)
|
|
|
|
unless res.continue?
|
|
|
|
raise SMTPUnknownError, "could not get 3xx (#{res.status})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_auth_response(res)
|
|
|
|
unless res.success?
|
|
|
|
raise SMTPAuthenticationError, res.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_auth_continue(res)
|
|
|
|
unless res.continue?
|
|
|
|
raise res.exception_class, res.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Response
|
|
|
|
def Response.parse(str)
|
|
|
|
new(str[0,3], str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(status, string)
|
|
|
|
@status = status
|
|
|
|
@string = string
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :status
|
|
|
|
attr_reader :string
|
|
|
|
|
|
|
|
def status_type_char
|
|
|
|
@status[0, 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def success?
|
|
|
|
status_type_char() == '2'
|
|
|
|
end
|
|
|
|
|
|
|
|
def continue?
|
|
|
|
status_type_char() == '3'
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
@string.lines.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def cram_md5_challenge
|
|
|
|
@string.split(/ /)[1].unpack('m')[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def capabilities
|
|
|
|
return {} unless @string[3, 1] == '-'
|
|
|
|
h = {}
|
2007-05-20 22:33:25 +04:00
|
|
|
@string.lines.drop(1).each do |line|
|
2007-03-05 03:17:38 +03:00
|
|
|
k, *v = line[4..-1].chomp.split(nil)
|
|
|
|
h[k] = v
|
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
def exception_class
|
|
|
|
case @status
|
|
|
|
when /\A4/ then SMTPServerBusy
|
|
|
|
when /\A50/ then SMTPSyntaxError
|
|
|
|
when /\A53/ then SMTPAuthenticationError
|
|
|
|
when /\A5/ then SMTPFatalError
|
|
|
|
else SMTPUnknownError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-06 20:08:21 +03:00
|
|
|
def logging(msg)
|
2004-04-21 13:40:48 +04:00
|
|
|
@debug_output << msg + "\n" if @debug_output
|
2004-03-06 20:08:21 +03:00
|
|
|
end
|
|
|
|
|
2003-07-02 06:34:39 +04:00
|
|
|
end # class SMTP
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
2003-07-02 06:34:39 +04:00
|
|
|
SMTPSession = SMTP
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
2006-08-15 06:21:59 +04:00
|
|
|
end
|