1
# taken from http://github.com/ambethia/smtp-tls/raw/master/lib/smtp-tls.rb
2
3
require "openssl"
4
require "net/smtp"
5
6
Net::SMTP.class_eval do
7
  private
8
  def do_start(helodomain, user, secret, authtype)
9
    raise IOError, 'SMTP session already started' if @started
10
    
11
    if RUBY_VERSION > "1.8.6"
12
      check_auth_args user, secret if user or secret
13
    else
14
      check_auth_args user, secret, authtype if user or secret
15
    end
16
17
    sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
18
    @socket = Net::InternetMessageIO.new(sock)
19
    @socket.read_timeout = 60 #@read_timeout
20
21
    check_response(critical { recv_response() })
22
    do_helo(helodomain)
23
24
    if starttls
25
      raise 'openssl library not installed' unless defined?(OpenSSL)
26
      ssl = OpenSSL::SSL::SSLSocket.new(sock)
27
      ssl.sync_close = true
28
      ssl.connect
29
      @socket = Net::InternetMessageIO.new(ssl)
30
      @socket.read_timeout = 60 #@read_timeout
31
      do_helo(helodomain)
32
    end
33
34
    authenticate user, secret, authtype if user
35
    @started = true
36
  ensure
37
    unless @started
38
      # authentication failed, cancel connection.
39
      @socket.close if not @started and @socket and not @socket.closed?
40
      @socket = nil
41
    end
42
  end
43
44
  def do_helo(helodomain)
45
    begin
46
      if @esmtp
47
        ehlo helodomain
48
      else
49
        helo helodomain
50
      end
51
    rescue Net::ProtocolError
52
      if @esmtp
53
        @esmtp = false
54
        @error_occured = false
55
        retry
56
      end
57
      raise
58
    end
59
  end
60
61
  def starttls
62
    getok('STARTTLS') rescue return false
63
  return true
64
  end
65
66
  def quit
67
    begin
68
      getok('QUIT')
69
    rescue EOFError
70
    end
71
  end
72
end