Class: MailCatcher::Smtp

Inherits:
EventMachine::Protocols::SmtpServer
  • Object
show all
Defined in:
lib/mail_catcher/smtp.rb

Instance Method Summary collapse

Instance Method Details

#current_messageObject



15
16
17
# File 'lib/mail_catcher/smtp.rb', line 15

def current_message
  @current_message ||= {}
end

#process_mail_from(sender) ⇒ Object

We override EM’s mail from processing to allow multiple mail-from commands per [RFC 2821](tools.ietf.org/html/rfc2821#section-4.1.1.2)



6
7
8
9
10
11
12
13
# File 'lib/mail_catcher/smtp.rb', line 6

def process_mail_from sender
  if @state.include? :mail_from
    @state -= [:mail_from, :rcpt, :data]
    receive_reset
  end

  super
end

#receive_data_chunk(lines) ⇒ Object



35
36
37
38
39
# File 'lib/mail_catcher/smtp.rb', line 35

def receive_data_chunk(lines)
  current_message[:source] ||= ""
  current_message[:source] << lines.join("\n")
  true
end

#receive_messageObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mail_catcher/smtp.rb', line 41

def receive_message
  MailCatcher::Mail.add_message current_message
  puts "==> SMTP: Received message from '#{current_message[:sender]}' (#{current_message[:source].length} bytes)"
  true
rescue
  puts "*** Error receiving message: #{current_message.inspect}"
  puts "    Exception: #{$!}"
  puts "    Backtrace:"
  $!.backtrace.each do |line|
    puts "       #{line}"
  end
  puts "    Please submit this as an issue at http://github.com/sj26/mailcatcher/issues"
  false
ensure
  @current_message = nil
end

#receive_recipient(recipient) ⇒ Object



29
30
31
32
33
# File 'lib/mail_catcher/smtp.rb', line 29

def receive_recipient(recipient)
  current_message[:recipients] ||= []
  current_message[:recipients] << recipient
  true
end

#receive_resetObject



19
20
21
22
# File 'lib/mail_catcher/smtp.rb', line 19

def receive_reset
  @current_message = nil
  true
end

#receive_sender(sender) ⇒ Object



24
25
26
27
# File 'lib/mail_catcher/smtp.rb', line 24

def receive_sender(sender)
  current_message[:sender] = sender
  true
end