eltiare (owner)

Revisions

gist: 79460 Download_button fork
public
Public Clone URL: git://gist.github.com/79460.git
Embed All Files: show embed
simple_smtp_client.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'socket'
require 'base64'
class SSMTP < TCPSocket
  
  def self.start(*args)
    s = new(*args[0..1])
    s.start(*args[2..5])
    yield s
    s.close
  end
  
  attr_accessor :verp
  
  def initialize(*)
    @remote_opts = {}
    @remote_opts[:attrs] = []
    super
  end
  
  def start(ehlo = 'localhost.localdomain', user=nil, pass=nil, method = :login)
    puts "EHLO #{ehlo}"
    r = recv(500)
    lines = r.gsub(/^250\-/).split(/\r\n/)
    @remote_opts[:host_name] = lines.shift
    lines.each do |line|
      case line
      when /^SIZE/ then @remote_opts[:max_size] = line.sub(/^SIZE /, '').to_i
      when /^AUTH/ then @remote_opts[:login_methods] = line.sub(/^AUTH /, '').split(' ').map { |str| str.downcase.to_sym }
      else @remote_opts[:attrs].push line
      end
    end
    
    raise 'Not a remote accepted login method' unless @remote_opts[:login_methods].include?(method)
    case method.to_sym
      when :login
        puts 'AUTH LOGIN'
        #r << recv(500)
        write Base64.encode64(user)
        #r << recv(500)
        write Base64.encode64(pass)
        #r << recv(500)
    else
      raise "Unimplemented method: #{method}"
    end
    r
  end
  
  def send_message(msg, from, to)
    if @verp
      from = "<#{from}>" unless from.match('<')
      from = "#{from} XVERP"
    end
    puts "MAIL FROM:#{from}"
    puts "RCPT TO:#{to}"
    puts "DATA", msg, '.'
    recv(500)
  end
  
end