2017-01-12 07:17:33 -05:00
|
|
|
# frozen_string_literal: true
|
2003-08-06 07:37:55 -04:00
|
|
|
# = net/smtp.rb
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2007-03-04 17:14:23 -05:00
|
|
|
# Copyright (c) 1999-2007 Yukihiro Matsumoto.
|
2004-03-06 12:08:21 -05:00
|
|
|
#
|
2007-03-04 17:14:23 -05:00
|
|
|
# Copyright (c) 1999-2007 Minero Aoki.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Written & maintained by Minero Aoki <aamine@loveruby.net>.
|
|
|
|
#
|
|
|
|
# Documented by William Webber and Minero Aoki.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# This program is free software. You can re-distribute and/or
|
2004-03-06 12:08:21 -05:00
|
|
|
# modify this program under the same terms as Ruby itself.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# $Id$
|
2004-04-21 05:40:48 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# See Net::SMTP for documentation.
|
|
|
|
#
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
require 'net/protocol'
|
2001-12-01 09:07:01 -05:00
|
|
|
require 'digest/md5'
|
2004-03-06 12:08:21 -05:00
|
|
|
require 'timeout'
|
2004-04-21 05:40:48 -04:00
|
|
|
begin
|
2006-08-14 22:21:59 -04:00
|
|
|
require 'openssl'
|
2004-04-21 05:40:48 -04:00
|
|
|
rescue LoadError
|
|
|
|
end
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
module Net
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
# Module mixed in to all SMTP error classes
|
2003-07-01 22:34:39 -04:00
|
|
|
module SMTPError
|
2007-03-04 17:14:23 -05:00
|
|
|
# This *class* is a module for backward compatibility.
|
|
|
|
# In later release, this module becomes a class.
|
2003-07-01 22:34:39 -04:00
|
|
|
end
|
2003-08-06 07:37:55 -04:00
|
|
|
|
|
|
|
# Represents an SMTP authentication error.
|
2003-07-01 22:34:39 -04:00
|
|
|
class SMTPAuthenticationError < ProtoAuthError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 07:37:55 -04:00
|
|
|
|
|
|
|
# Represents SMTP error code 420 or 450, a temporary error.
|
2003-07-01 22:34:39 -04:00
|
|
|
class SMTPServerBusy < ProtoServerError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 07:37:55 -04:00
|
|
|
|
|
|
|
# Represents an SMTP command syntax error (error code 500)
|
2003-07-01 22:34:39 -04:00
|
|
|
class SMTPSyntaxError < ProtoSyntaxError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 07:37:55 -04:00
|
|
|
|
|
|
|
# Represents a fatal SMTP error (error code 5xx, except for 500)
|
2003-07-01 22:34:39 -04:00
|
|
|
class SMTPFatalError < ProtoFatalError
|
|
|
|
include SMTPError
|
|
|
|
end
|
2003-08-06 07:37:55 -04:00
|
|
|
|
|
|
|
# Unexpected reply code returned from server.
|
2003-07-01 22:34:39 -04:00
|
|
|
class SMTPUnknownError < ProtoUnknownError
|
|
|
|
include SMTPError
|
|
|
|
end
|
|
|
|
|
2007-03-04 19:17:38 -05:00
|
|
|
# Command is not supported on server.
|
|
|
|
class SMTPUnsupportedCommand < ProtocolError
|
|
|
|
include SMTPError
|
|
|
|
end
|
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# == What is This Library?
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# == What is This Library NOT?
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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,
|
2014-01-13 00:49:12 -05:00
|
|
|
# try RubyMail or TMail or search for alternatives in
|
|
|
|
# {RubyGems.org}[https://rubygems.org/] or {The Ruby
|
|
|
|
# Toolbox}[https://www.ruby-toolbox.com/].
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# FYI: the official documentation on internet mail is: [RFC2822] (http://www.ietf.org/rfc/rfc2822.txt).
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# == Examples
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Sending Messages
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# You must open a connection to an SMTP server before sending messages.
|
2009-03-05 22:56:38 -05: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 10:42:43 -04:00
|
|
|
# after the block is executed.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Then you can send messages.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# This is a test message.
|
|
|
|
# END_OF_MESSAGE
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-19 23:30:59 -04:00
|
|
|
# 'his_address@example.com'
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Closing the Session
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
|
|
|
# You MUST close the SMTP session after sending messages, by calling
|
2004-05-09 10:42:43 -04:00
|
|
|
# the #finish method:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# You can also use the block form of SMTP.start/SMTP#start. This closes
|
|
|
|
# the SMTP session automatically:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# I strongly recommend this scheme. This form is simpler and more robust.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === HELO domain
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::SMTP.start('your.smtp.server', 25,
|
|
|
|
# 'mail.from.domain') { |smtp| ... }
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === SMTP Authentication
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# The Net::SMTP class supports three authentication schemes;
|
|
|
|
# PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])
|
2009-03-05 22:56:38 -05:00
|
|
|
# To use SMTP authentication, pass extra arguments to
|
2004-05-09 10:42:43 -04:00
|
|
|
# SMTP.start/SMTP#start.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10: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 07:37:55 -04:00
|
|
|
#
|
2016-11-05 02:47:36 -04:00
|
|
|
class SMTP < Protocol
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
Revision = %q$Revision$.split[1]
|
2004-05-09 10:42:43 -04:00
|
|
|
|
2007-03-04 17:14:23 -05:00
|
|
|
# The default SMTP port number, 25.
|
2003-05-02 10:35:01 -04:00
|
|
|
def SMTP.default_port
|
|
|
|
25
|
|
|
|
end
|
|
|
|
|
2007-03-04 17:14:23 -05:00
|
|
|
# The default mail submission port number, 587.
|
|
|
|
def SMTP.default_submission_port
|
2006-08-14 22:21:59 -04:00
|
|
|
587
|
|
|
|
end
|
|
|
|
|
2007-03-04 17:14:23 -05:00
|
|
|
# The default SMTPS port number, 465.
|
|
|
|
def SMTP.default_tls_port
|
|
|
|
465
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
|
|
|
|
2007-03-04 17:14:23 -05:00
|
|
|
class << self
|
|
|
|
alias default_ssl_port default_tls_port
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def SMTP.default_ssl_context
|
2007-03-04 17:14:23 -05:00
|
|
|
OpenSSL::SSL::SSLContext.new
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-05-09 10: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 05:23:56 -04:00
|
|
|
def initialize(address, port = nil)
|
2003-05-02 10:35:01 -04:00
|
|
|
@address = address
|
2003-07-01 22:34:39 -04:00
|
|
|
@port = (port || SMTP.default_port)
|
2000-04-18 05:39:02 -04:00
|
|
|
@esmtp = true
|
2007-03-04 19:17:38 -05:00
|
|
|
@capabilities = nil
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket = nil
|
|
|
|
@started = false
|
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
2013-05-19 10:50:47 -04:00
|
|
|
@error_occurred = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@debug_output = nil
|
2007-03-04 17:14:23 -05:00
|
|
|
@tls = false
|
|
|
|
@starttls = false
|
|
|
|
@ssl_context = nil
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
# Provide human-readable stringification of class state.
|
2003-05-02 10:35:01 -04:00
|
|
|
def inspect
|
2003-07-01 22:34:39 -04:00
|
|
|
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
|
2000-04-18 05:39:02 -04:00
|
|
|
end
|
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Set whether to use ESMTP or not. This should be done before
|
2003-08-06 07: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 10:42:43 -04:00
|
|
|
#
|
2010-12-22 18:13:49 -05:00
|
|
|
attr_accessor :esmtp
|
2002-02-19 07:33:52 -05:00
|
|
|
|
2010-12-22 18:13:49 -05:00
|
|
|
# +true+ if the SMTP object uses ESMTP (which it does by default).
|
|
|
|
alias :esmtp? :esmtp
|
2000-04-18 05:39:02 -04:00
|
|
|
|
2007-03-04 19:17:38 -05: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-04 17:14:23 -05:00
|
|
|
def tls?
|
2006-08-14 22:21:59 -04:00
|
|
|
@tls
|
|
|
|
end
|
|
|
|
|
2007-03-04 17:14:23 -05: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-14 22:21:59 -04:00
|
|
|
raise 'openssl library not installed' unless defined?(OpenSSL)
|
2007-03-04 17:14:23 -05:00
|
|
|
raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @starttls
|
|
|
|
@tls = true
|
2006-08-14 22:21:59 -04:00
|
|
|
@ssl_context = context
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
2007-03-04 17:14:23 -05:00
|
|
|
|
|
|
|
alias enable_ssl enable_tls
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-04 17:14:23 -05:00
|
|
|
# Disables SMTP/TLS for this object. Must be called before the
|
2006-08-14 22:21:59 -04:00
|
|
|
# connection is established to have any effect.
|
2007-03-04 17:14:23 -05:00
|
|
|
def disable_tls
|
|
|
|
@tls = false
|
2006-08-14 22:21:59 -04:00
|
|
|
@ssl_context = nil
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
2007-03-04 17:14:23 -05: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-04 19:17:38 -05: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-05 22:56:38 -05:00
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
# Enables SMTP/TLS (STARTTLS) for this object.
|
|
|
|
# +context+ is a OpenSSL::SSL::SSLContext object.
|
2007-03-04 17:14:23 -05:00
|
|
|
def enable_starttls(context = SMTP.default_ssl_context)
|
2006-08-14 22:21:59 -04:00
|
|
|
raise 'openssl library not installed' unless defined?(OpenSSL)
|
2007-03-04 17:14:23 -05: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-14 22: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-04 17:14:23 -05:00
|
|
|
def disable_starttls
|
|
|
|
@starttls = false
|
2006-08-14 22:21:59 -04:00
|
|
|
@ssl_context = nil
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
# The address of the SMTP server to connect to.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :address
|
2003-08-06 07:37:55 -04:00
|
|
|
|
|
|
|
# The port number of the SMTP server to connect to.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :port
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
# Seconds to wait while attempting to open a connection.
|
|
|
|
# If the connection cannot be opened within this time, a
|
2013-03-30 21:47:47 -04:00
|
|
|
# Net::OpenTimeout is raised. The default value is 30 seconds.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_accessor :open_timeout
|
2003-08-06 07: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
|
2013-03-30 21:47:47 -04:00
|
|
|
# Net::ReadTimeout is raised. The default value is 60 seconds.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :read_timeout
|
2000-02-21 10:25:37 -05:00
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
# Set the number of seconds to wait until timing-out a read(2)
|
|
|
|
# call.
|
2004-04-21 05:23:56 -04:00
|
|
|
def read_timeout=(sec)
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket.read_timeout = sec if @socket
|
|
|
|
@read_timeout = sec
|
|
|
|
end
|
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07: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 10:42:43 -04:00
|
|
|
# smtp.start do |smtp|
|
2003-08-06 07:37:55 -04:00
|
|
|
# ....
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
|
|
|
#
|
2006-08-14 22:21:59 -04:00
|
|
|
def debug_output=(arg)
|
2003-05-02 10:35:01 -04:00
|
|
|
@debug_output = arg
|
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
alias set_debug_output debug_output=
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
#
|
|
|
|
# SMTP session control
|
|
|
|
#
|
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# Creates a new Net::SMTP object and connects to the server.
|
|
|
|
#
|
|
|
|
# This method is equivalent to:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::SMTP.new(address, port).start(helo_domain, account, password, authtype)
|
2003-08-06 07:37:55 -04:00
|
|
|
#
|
2004-05-09 10: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 07: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 10:42:43 -04:00
|
|
|
# === Parameters
|
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# +address+ is the hostname or ip address of your smtp server.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# +port+ is the port to connect to; it defaults to port 25.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# +helo+ is the _HELO_ _domain_ provided by the client to the
|
2009-03-05 22:56:38 -05:00
|
|
|
# server (see overview comments); it defaults to 'localhost'.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07: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 10:42:43 -04:00
|
|
|
# === Errors
|
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPAuthenticationError
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
2012-04-11 17:20:51 -04:00
|
|
|
# * Net::OpenTimeout
|
|
|
|
# * Net::ReadTimeout
|
2003-08-06 07:37:55 -04:00
|
|
|
# * IOError
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2008-07-02 23:54:25 -04:00
|
|
|
def SMTP.start(address, port = nil, helo = 'localhost',
|
2004-04-21 05:23:56 -04:00
|
|
|
user = nil, secret = nil, authtype = nil,
|
|
|
|
&block) # :yield: smtp
|
2003-05-02 10:35:01 -04:00
|
|
|
new(address, port).start(helo, user, secret, authtype, &block)
|
|
|
|
end
|
|
|
|
|
2003-08-06 07:37:55 -04:00
|
|
|
# +true+ if the SMTP session has been started.
|
2003-05-02 10:35:01 -04:00
|
|
|
def started?
|
|
|
|
@started
|
|
|
|
end
|
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# Opens a TCP connection and starts the SMTP session.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Parameters
|
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# +helo+ is the _HELO_ _domain_ that you'll dispatch mails from; see
|
|
|
|
# the discussion in the overview notes.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# If both of +user+ and +secret+ are given, SMTP authentication
|
|
|
|
# will be attempted using the AUTH command. +authtype+ specifies
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
# in the overview.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
|
|
|
# === Block Usage
|
|
|
|
#
|
2003-08-06 07: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-05 22:56:38 -05:00
|
|
|
# the block call finishes. Otherwise, it is the caller's
|
2003-08-06 07:37:55 -04:00
|
|
|
# responsibility to close the session when finished.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# This is very similar to the class method SMTP.start.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# require 'net/smtp'
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
# end
|
2004-05-09 10: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-05 22:56:38 -05:00
|
|
|
# started.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
|
|
|
# === Errors
|
2003-08-06 07: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
|
2012-04-11 17:20:51 -04:00
|
|
|
# * Net::OpenTimeout
|
|
|
|
# * Net::ReadTimeout
|
2003-08-06 07:37:55 -04:00
|
|
|
# * IOError
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2008-07-02 23:54:25 -04:00
|
|
|
def start(helo = 'localhost',
|
2004-05-09 10:42:43 -04:00
|
|
|
user = nil, secret = nil, authtype = nil) # :yield: smtp
|
2003-05-02 10:35:01 -04:00
|
|
|
if block_given?
|
|
|
|
begin
|
2006-08-14 22:21:59 -04:00
|
|
|
do_start helo, user, secret, authtype
|
2003-05-02 10:35:01 -04:00
|
|
|
return yield(self)
|
|
|
|
ensure
|
2003-09-29 14:12:20 -04:00
|
|
|
do_finish
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
else
|
2006-08-14 22:21:59 -04:00
|
|
|
do_start helo, user, secret, authtype
|
2003-05-02 10:35:01 -04:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-14 22: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
|
|
|
|
|
2010-12-22 16:37:13 -05:00
|
|
|
def tcp_socket(address, port)
|
|
|
|
TCPSocket.open address, port
|
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def do_start(helo_domain, user, secret, authtype)
|
2003-07-01 22:34:39 -04:00
|
|
|
raise IOError, 'SMTP session already started' if @started
|
2006-08-14 22:21:59 -04:00
|
|
|
if user or secret
|
2007-03-04 19:17:38 -05:00
|
|
|
check_auth_method(authtype || DEFAULT_AUTH_TYPE)
|
2006-08-14 22:21:59 -04:00
|
|
|
check_auth_args user, secret
|
|
|
|
end
|
2012-02-28 00:15:54 -05:00
|
|
|
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
|
|
|
|
tcp_socket(@address, @port)
|
|
|
|
end
|
2006-08-14 22:21:59 -04:00
|
|
|
logging "Connection opened: #{@address}:#{@port}"
|
2007-03-04 17:14:23 -05:00
|
|
|
@socket = new_internet_message_io(tls? ? tlsconnect(s) : s)
|
2007-03-04 19:17:38 -05:00
|
|
|
check_response critical { recv_response() }
|
2006-08-14 22:21:59 -04:00
|
|
|
do_helo helo_domain
|
2007-03-04 19:17:38 -05: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 05:40:48 -04:00
|
|
|
starttls
|
2007-03-04 17:14:23 -05:00
|
|
|
@socket = new_internet_message_io(tlsconnect(s))
|
2004-04-21 05:40:48 -04:00
|
|
|
# helo response may be different after STARTTLS
|
2006-08-14 22:21:59 -04:00
|
|
|
do_helo helo_domain
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
2007-03-04 19:17:38 -05:00
|
|
|
authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
|
2004-04-21 05:40:48 -04:00
|
|
|
@started = true
|
|
|
|
ensure
|
|
|
|
unless @started
|
|
|
|
# authentication failed, cancel connection.
|
2016-11-21 18:05:41 -05:00
|
|
|
s.close if s
|
2004-04-21 05:40:48 -04:00
|
|
|
@socket = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-22 16:37:13 -05:00
|
|
|
def ssl_socket(socket, context)
|
|
|
|
OpenSSL::SSL::SSLSocket.new socket, context
|
|
|
|
end
|
|
|
|
|
2007-03-04 17:14:23 -05:00
|
|
|
def tlsconnect(s)
|
2010-12-22 16:37:13 -05:00
|
|
|
verified = false
|
|
|
|
s = ssl_socket(s, @ssl_context)
|
2007-03-04 17:14:23 -05:00
|
|
|
logging "TLS connection started"
|
|
|
|
s.sync_close = true
|
2016-11-05 02:47:36 -04:00
|
|
|
ssl_socket_connect(s, @open_timeout)
|
2007-10-08 07:14:41 -04:00
|
|
|
if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
|
|
|
s.post_connection_check(@address)
|
|
|
|
end
|
2010-12-22 16:37:13 -05:00
|
|
|
verified = true
|
2007-03-04 17:14:23 -05:00
|
|
|
s
|
2010-12-22 16:37:13 -05:00
|
|
|
ensure
|
|
|
|
s.close unless verified
|
2007-03-04 17:14:23 -05:00
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def new_internet_message_io(s)
|
2016-11-14 02:53:32 -05:00
|
|
|
InternetMessageIO.new(s, read_timeout: @read_timeout,
|
|
|
|
debug_output: @debug_output)
|
2006-08-14 22:21:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def do_helo(helo_domain)
|
2007-03-04 19:17:38 -05:00
|
|
|
res = @esmtp ? ehlo(helo_domain) : helo(helo_domain)
|
|
|
|
@capabilities = res.capabilities
|
|
|
|
rescue SMTPError
|
|
|
|
if @esmtp
|
|
|
|
@esmtp = false
|
2013-05-19 10:50:47 -04:00
|
|
|
@error_occurred = false
|
2007-03-04 19:17:38 -05:00
|
|
|
retry
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
2007-03-04 19:17:38 -05:00
|
|
|
raise
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2003-09-29 14:12:20 -04:00
|
|
|
|
|
|
|
def do_finish
|
2013-05-19 10:50:47 -04:00
|
|
|
quit if @socket and not @socket.closed? and not @error_occurred
|
2003-09-29 14:12:20 -04:00
|
|
|
ensure
|
|
|
|
@started = false
|
2013-05-19 10:50:47 -04:00
|
|
|
@error_occurred = false
|
2016-11-21 18:05:41 -05:00
|
|
|
@socket.close if @socket
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket = nil
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
2006-08-14 22:21:59 -04:00
|
|
|
# Message Sending
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
public
|
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07: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-05 22:56:38 -05:00
|
|
|
# binary message with this method. +msgstr+ should include both
|
2003-08-06 07:37:55 -04:00
|
|
|
# the message headers and body.
|
|
|
|
#
|
|
|
|
# +from_addr+ is a String representing the source mail address.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07: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 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# Net::SMTP.start('smtp.example.com') do |smtp|
|
2003-08-06 07:37:55 -04:00
|
|
|
# smtp.send_message msgstr,
|
|
|
|
# 'from@example.com',
|
|
|
|
# ['dest@example.com', 'dest2@example.com']
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === Errors
|
2003-08-06 07:37:55 -04:00
|
|
|
#
|
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
2012-04-11 17:20:51 -04:00
|
|
|
# * Net::ReadTimeout
|
2003-08-06 07:37:55 -04:00
|
|
|
# * IOError
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2004-04-21 05:23:56 -04:00
|
|
|
def send_message(msgstr, from_addr, *to_addrs)
|
2006-08-14 22:21:59 -04:00
|
|
|
raise IOError, 'closed session' unless @socket
|
|
|
|
mailfrom from_addr
|
2010-04-16 19:57:43 -04:00
|
|
|
rcptto_list(to_addrs) {data msgstr}
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
alias send_mail send_message
|
|
|
|
alias sendmail send_message # obsolete
|
2001-12-30 14:18:45 -05:00
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07: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 10: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 07: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 10:42:43 -04:00
|
|
|
# === Parameters
|
|
|
|
#
|
2003-08-06 07:37:55 -04:00
|
|
|
# +from_addr+ is a String representing the source mail address.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-06 07: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 10: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 07: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 10:42:43 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === Errors
|
2003-08-06 07:37:55 -04:00
|
|
|
#
|
|
|
|
# This method may raise:
|
|
|
|
#
|
|
|
|
# * Net::SMTPServerBusy
|
|
|
|
# * Net::SMTPSyntaxError
|
|
|
|
# * Net::SMTPFatalError
|
|
|
|
# * Net::SMTPUnknownError
|
2012-04-11 17:20:51 -04:00
|
|
|
# * Net::ReadTimeout
|
2003-08-06 07:37:55 -04:00
|
|
|
# * IOError
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
|
|
|
def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
|
2003-07-29 05:48:30 -04:00
|
|
|
raise IOError, 'closed session' unless @socket
|
2003-07-01 22:34:39 -04:00
|
|
|
mailfrom from_addr
|
2010-04-16 19:57:43 -04:00
|
|
|
rcptto_list(to_addrs) {data(&block)}
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
alias ready open_message_stream # obsolete
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
#
|
2006-08-14 22:21:59 -04:00
|
|
|
# Authentication
|
2003-07-01 22:34:39 -04:00
|
|
|
#
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
public
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2007-03-04 19:17:38 -05:00
|
|
|
DEFAULT_AUTH_TYPE = :plain
|
|
|
|
|
|
|
|
def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE)
|
2006-08-14 22:21:59 -04:00
|
|
|
check_auth_method authtype
|
|
|
|
check_auth_args user, secret
|
2007-11-04 15:36:20 -05:00
|
|
|
send auth_method(authtype), user, secret
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
|
|
|
|
2004-04-21 05:23:56 -04:00
|
|
|
def auth_plain(user, secret)
|
2006-08-14 22:21:59 -04:00
|
|
|
check_auth_args user, secret
|
2007-03-04 19:17:38 -05:00
|
|
|
res = critical {
|
|
|
|
get_response('AUTH PLAIN ' + base64_encode("\0#{user}\0#{secret}"))
|
|
|
|
}
|
|
|
|
check_auth_response res
|
|
|
|
res
|
2003-05-24 14:24:26 -04:00
|
|
|
end
|
|
|
|
|
2004-04-21 05:23:56 -04:00
|
|
|
def auth_login(user, secret)
|
2006-08-14 22:21:59 -04:00
|
|
|
check_auth_args user, secret
|
2003-05-24 14:24:26 -04:00
|
|
|
res = critical {
|
2007-03-04 19:17:38 -05:00
|
|
|
check_auth_continue get_response('AUTH LOGIN')
|
|
|
|
check_auth_continue get_response(base64_encode(user))
|
2003-07-01 22:34:39 -04:00
|
|
|
get_response(base64_encode(secret))
|
2003-05-24 14:24:26 -04:00
|
|
|
}
|
2007-03-04 19:17:38 -05:00
|
|
|
check_auth_response res
|
|
|
|
res
|
2000-04-25 05:23:21 -04:00
|
|
|
end
|
|
|
|
|
2004-04-21 05:23:56 -04:00
|
|
|
def auth_cram_md5(user, secret)
|
2006-08-14 22:21:59 -04:00
|
|
|
check_auth_args user, secret
|
2007-03-04 19:17:38 -05:00
|
|
|
res = critical {
|
2007-05-20 14: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-04 19:17:38 -05:00
|
|
|
get_response(base64_encode("#{user} #{crammed}"))
|
2000-04-25 05:23:21 -04:00
|
|
|
}
|
2007-03-04 19:17:38 -05:00
|
|
|
check_auth_response res
|
|
|
|
res
|
2006-08-14 22:21:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_auth_method(type)
|
2007-03-04 19:17:38 -05:00
|
|
|
unless respond_to?(auth_method(type), true)
|
2006-08-14 22:21:59 -04:00
|
|
|
raise ArgumentError, "wrong authentication type #{type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-04 19:17:38 -05:00
|
|
|
def auth_method(type)
|
|
|
|
"auth_#{type.to_s.downcase}".intern
|
|
|
|
end
|
|
|
|
|
2009-05-10 19:56:22 -04:00
|
|
|
def check_auth_args(user, secret, authtype = DEFAULT_AUTH_TYPE)
|
2006-08-14 22: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 05:23:21 -04:00
|
|
|
end
|
|
|
|
|
2004-04-21 05:23:56 -04:00
|
|
|
def base64_encode(str)
|
2003-05-24 14:24:26 -04:00
|
|
|
# expects "str" may not become too long
|
2016-01-09 19:35:43 -05:00
|
|
|
[str].pack('m0')
|
2003-05-24 14:24:26 -04:00
|
|
|
end
|
|
|
|
|
2007-03-04 19:17:38 -05: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 14:33:25 -04:00
|
|
|
0.upto(buf.size - 1) do |i|
|
2007-03-04 19:17:38 -05:00
|
|
|
buf[i] = (buf[i].ord ^ mask).chr
|
|
|
|
end
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
#
|
|
|
|
# SMTP command dispatcher
|
|
|
|
#
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
public
|
|
|
|
|
2013-03-11 13:27:03 -04:00
|
|
|
# Aborts the current mail transaction
|
|
|
|
|
|
|
|
def rset
|
|
|
|
getok('RSET')
|
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def starttls
|
|
|
|
getok('STARTTLS')
|
|
|
|
end
|
2003-07-01 22:34:39 -04:00
|
|
|
|
2004-04-21 05:23:56 -04:00
|
|
|
def helo(domain)
|
2007-03-04 19:17:38 -05:00
|
|
|
getok("HELO #{domain}")
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2004-04-21 05:23:56 -04:00
|
|
|
def ehlo(domain)
|
2007-03-04 19:17:38 -05:00
|
|
|
getok("EHLO #{domain}")
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def mailfrom(from_addr)
|
|
|
|
if $SAFE > 0
|
|
|
|
raise SecurityError, 'tainted from_addr' if from_addr.tainted?
|
|
|
|
end
|
2007-03-04 19:17:38 -05:00
|
|
|
getok("MAIL FROM:<#{from_addr}>")
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def rcptto_list(to_addrs)
|
|
|
|
raise ArgumentError, 'mail destination not given' if to_addrs.empty?
|
2010-04-16 19:57:43 -04:00
|
|
|
ok_users = []
|
|
|
|
unknown_users = []
|
2006-08-22 06:21:31 -04:00
|
|
|
to_addrs.flatten.each do |addr|
|
2010-04-16 19:57:43 -04:00
|
|
|
begin
|
|
|
|
rcptto addr
|
|
|
|
rescue SMTPAuthenticationError
|
|
|
|
unknown_users << addr.dump
|
|
|
|
else
|
|
|
|
ok_users << addr
|
|
|
|
end
|
|
|
|
end
|
2010-04-27 05:00:52 -04:00
|
|
|
raise ArgumentError, 'mail destination not given' if ok_users.empty?
|
2010-04-16 19:57:43 -04:00
|
|
|
ret = yield
|
|
|
|
unless unknown_users.empty?
|
|
|
|
raise SMTPAuthenticationError, "failed to deliver for #{unknown_users.join(', ')}"
|
2006-08-14 22:21:59 -04:00
|
|
|
end
|
2010-04-16 19:57:43 -04:00
|
|
|
ret
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def rcptto(to_addr)
|
|
|
|
if $SAFE > 0
|
2008-08-04 01:46:53 -04:00
|
|
|
raise SecurityError, 'tainted to_addr' if to_addr.tainted?
|
2006-08-14 22:21:59 -04:00
|
|
|
end
|
2007-03-04 19:17:38 -05:00
|
|
|
getok("RCPT TO:<#{to_addr}>")
|
2006-08-14 22: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-05 22:56:38 -05:00
|
|
|
#
|
2006-08-14 22: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-04 19:17:38 -05:00
|
|
|
check_continue get_response('DATA')
|
2014-07-11 15:22:19 -04:00
|
|
|
socket_sync_bak = @socket.io.sync
|
|
|
|
begin
|
|
|
|
@socket.io.sync = false
|
|
|
|
if msgstr
|
|
|
|
@socket.write_message msgstr
|
|
|
|
else
|
|
|
|
@socket.write_message_by_block(&block)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@socket.io.flush
|
|
|
|
@socket.io.sync = socket_sync_bak
|
2006-08-14 22:21:59 -04:00
|
|
|
end
|
|
|
|
recv_response()
|
|
|
|
}
|
2007-03-04 19:17:38 -05:00
|
|
|
check_response res
|
|
|
|
res
|
2000-03-27 10:52:27 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
def quit
|
|
|
|
getok('QUIT')
|
2004-04-21 05:40:48 -04:00
|
|
|
end
|
2003-07-01 22:34:39 -04:00
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
private
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2016-06-08 03:06:57 -04:00
|
|
|
def validate_line(line)
|
|
|
|
# A bare CR or LF is not allowed in RFC5321.
|
|
|
|
if /[\r\n]/ =~ line
|
|
|
|
raise ArgumentError, "A line must not contain CR or LF"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-04 19:17:38 -05:00
|
|
|
def getok(reqline)
|
2016-06-08 03:06:57 -04:00
|
|
|
validate_line reqline
|
2003-07-01 22:34:39 -04:00
|
|
|
res = critical {
|
2007-03-04 19:17:38 -05:00
|
|
|
@socket.writeline reqline
|
2003-07-01 22:34:39 -04:00
|
|
|
recv_response()
|
|
|
|
}
|
2007-03-04 19:17:38 -05:00
|
|
|
check_response res
|
|
|
|
res
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
|
2007-03-04 19:17:38 -05:00
|
|
|
def get_response(reqline)
|
2016-06-08 03:06:57 -04:00
|
|
|
validate_line reqline
|
2007-03-04 19:17:38 -05:00
|
|
|
@socket.writeline reqline
|
2003-05-02 10:35:01 -04:00
|
|
|
recv_response()
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def recv_response
|
2017-01-12 07:17:33 -05:00
|
|
|
buf = ''.dup
|
2002-11-21 06:50:09 -05:00
|
|
|
while true
|
2003-05-02 10:35:01 -04:00
|
|
|
line = @socket.readline
|
2007-03-04 19:17:38 -05:00
|
|
|
buf << line << "\n"
|
2007-03-04 17:14:23 -05:00
|
|
|
break unless line[3,1] == '-' # "210-PIPELINING"
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2007-03-04 19:17:38 -05:00
|
|
|
Response.parse(buf)
|
2003-07-01 22:34:39 -04:00
|
|
|
end
|
|
|
|
|
* ext/pathname/lib/pathname.rb, ext/tk/lib/multi-tk.rb,
ext/tk/sample/demos-en/widget, lib/benchmark.rb, lib/irb/cmd/fork.rb,
lib/mkmf.rb, lib/net/ftp.rb, lib/net/smtp.rb, lib/open3.rb,
lib/pstore.rb, lib/rexml/element.rb, lib/rexml/light/node.rb,
lib/rinda/tuplespace.rb, lib/rss/maker/base.rb,
lib/rss/maker/entry.rb, lib/scanf.rb, lib/set.rb, lib/shell.rb,
lib/shell/command-processor.rb, lib/shell/process-controller.rb,
lib/shell/system-command.rb, lib/uri/common.rb: remove unused block
arguments to avoid creating Proc objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-11-05 03:37:47 -04:00
|
|
|
def critical
|
2013-12-02 00:18:04 -05:00
|
|
|
return Response.parse('200 dummy reply code') if @error_occurred
|
2003-07-01 22:34:39 -04:00
|
|
|
begin
|
|
|
|
return yield()
|
|
|
|
rescue Exception
|
2013-05-19 10:50:47 -04:00
|
|
|
@error_occurred = true
|
2003-07-01 22:34:39 -04:00
|
|
|
raise
|
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2007-03-04 19:17:38 -05:00
|
|
|
def check_response(res)
|
|
|
|
unless res.success?
|
|
|
|
raise res.exception_class, res.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_continue(res)
|
|
|
|
unless res.continue?
|
2012-04-24 08:55:13 -04:00
|
|
|
raise SMTPUnknownError, "could not get 3xx (#{res.status}: #{res.string})"
|
2007-03-04 19:17:38 -05:00
|
|
|
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
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# This class represents a response received by the SMTP server. Instances
|
|
|
|
# of this class are created by the SMTP class; they should not be directly
|
|
|
|
# created by the user. For more information on SMTP responses, view
|
|
|
|
# {Section 4.2 of RFC 5321}[http://tools.ietf.org/html/rfc5321#section-4.2]
|
2007-03-04 19:17:38 -05:00
|
|
|
class Response
|
2011-05-22 20:21:10 -04:00
|
|
|
# Parses the received response and separates the reply code and the human
|
|
|
|
# readable reply text
|
2010-12-22 18:26:05 -05:00
|
|
|
def self.parse(str)
|
2007-03-04 19:17:38 -05:00
|
|
|
new(str[0,3], str)
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# Creates a new instance of the Response class and sets the status and
|
|
|
|
# string attributes
|
2007-03-04 19:17:38 -05:00
|
|
|
def initialize(status, string)
|
|
|
|
@status = status
|
|
|
|
@string = string
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# The three digit reply code of the SMTP response
|
2007-03-04 19:17:38 -05:00
|
|
|
attr_reader :status
|
2011-05-22 20:21:10 -04:00
|
|
|
|
|
|
|
# The human readable reply text of the SMTP response
|
2007-03-04 19:17:38 -05:00
|
|
|
attr_reader :string
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# Takes the first digit of the reply code to determine the status type
|
2007-03-04 19:17:38 -05:00
|
|
|
def status_type_char
|
|
|
|
@status[0, 1]
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# Determines whether the response received was a Positive Completion
|
|
|
|
# reply (2xx reply code)
|
2007-03-04 19:17:38 -05:00
|
|
|
def success?
|
|
|
|
status_type_char() == '2'
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# Determines whether the response received was a Positive Intermediate
|
|
|
|
# reply (3xx reply code)
|
2007-03-04 19:17:38 -05:00
|
|
|
def continue?
|
|
|
|
status_type_char() == '3'
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# The first line of the human readable reply text
|
2007-03-04 19:17:38 -05:00
|
|
|
def message
|
|
|
|
@string.lines.first
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# Creates a CRAM-MD5 challenge. You can view more information on CRAM-MD5
|
|
|
|
# on Wikipedia: http://en.wikipedia.org/wiki/CRAM-MD5
|
2007-03-04 19:17:38 -05:00
|
|
|
def cram_md5_challenge
|
|
|
|
@string.split(/ /)[1].unpack('m')[0]
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:21:10 -04:00
|
|
|
# Returns a hash of the human readable reply text in the response if it
|
|
|
|
# is multiple lines. It does not return the first line. The key of the
|
|
|
|
# hash is the first word the value of the hash is an array with each word
|
|
|
|
# thereafter being a value in the array
|
2007-03-04 19:17:38 -05:00
|
|
|
def capabilities
|
|
|
|
return {} unless @string[3, 1] == '-'
|
|
|
|
h = {}
|
2007-05-20 14:33:25 -04:00
|
|
|
@string.lines.drop(1).each do |line|
|
2010-12-22 18:18:05 -05:00
|
|
|
k, *v = line[4..-1].chomp.split
|
2007-03-04 19:17:38 -05:00
|
|
|
h[k] = v
|
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2013-12-03 07:14:22 -05:00
|
|
|
# Determines whether there was an error and raises the appropriate error
|
2011-05-22 20:21:10 -04:00
|
|
|
# based on the reply code of the response
|
2007-03-04 19:17:38 -05:00
|
|
|
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 12:08:21 -05:00
|
|
|
def logging(msg)
|
2004-04-21 05:40:48 -04:00
|
|
|
@debug_output << msg + "\n" if @debug_output
|
2004-03-06 12:08:21 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22: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 02:04:40 -04:00
|
|
|
|
2013-02-01 23:37:45 -05:00
|
|
|
SMTPSession = SMTP # :nodoc:
|
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 02:04:40 -04:00
|
|
|
|
2006-08-14 22:21:59 -04:00
|
|
|
end
|