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


17
18
19
# File 'lib/mail_catcher/smtp.rb', line 17

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)


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

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


37
38
39
40
41
42
43
# File 'lib/mail_catcher/smtp.rb', line 37

def receive_data_chunk(lines)
  current_message[:source] ||= ""
  lines.each do |line|
    current_message[:source] << line << "\r\n"
  end
  true
end

#receive_messageObject


45
46
47
48
49
50
51
52
53
54
# File 'lib/mail_catcher/smtp.rb', line 45

def receive_message
  MailCatcher::Mail.add_message current_message
  puts "==> SMTP: Received message from '#{current_message[:sender]}' (#{current_message[:source].length} bytes)"
  true
rescue => exception
  MailCatcher.log_exception("Error receiving message", @current_message, exception)
  false
ensure
  @current_message = nil
end

#receive_recipient(recipient) ⇒ Object


31
32
33
34
35
# File 'lib/mail_catcher/smtp.rb', line 31

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

#receive_resetObject


21
22
23
24
# File 'lib/mail_catcher/smtp.rb', line 21

def receive_reset
  @current_message = nil
  true
end

#receive_sender(sender) ⇒ Object


26
27
28
29
# File 'lib/mail_catcher/smtp.rb', line 26

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