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



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

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)



10
11
12
13
14
15
16
17
18
# File 'lib/mail_catcher/smtp.rb', line 10

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



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

def receive_data_chunk(lines)
  current_message[:source] ||= +""

  lines.each do |line|
    current_message[:source] << line << "\r\n"
  end

  true
end

#receive_messageObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mail_catcher/smtp.rb', line 57

def receive_message
  MailCatcher::Mail.add_message current_message
  MailCatcher::Mail.delete_older_messages!
  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



40
41
42
43
44
45
# File 'lib/mail_catcher/smtp.rb', line 40

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

  true
end

#receive_resetObject



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

def receive_reset
  @current_message = nil

  true
end

#receive_sender(sender) ⇒ Object



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

def receive_sender(sender)
  # EventMachine SMTP advertises size extensions [https://tools.ietf.org/html/rfc1870]
  # so strip potential " SIZE=..." suffixes from senders
  sender = $` if sender =~ / SIZE=\d+\z/

  current_message[:sender] = sender

  true
end