2010-10-27 15:12:26 -04:00
|
|
|
require 'eventmachine'
|
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
class MailCatcher::Smtp < EventMachine::Protocols::SmtpServer
|
2012-04-06 01:37:13 -04:00
|
|
|
# We override EM's mail from processing to allow multiple mail-from commands
|
|
|
|
# per [RFC 2821](http://tools.ietf.org/html/rfc2821#section-4.1.1.2)
|
|
|
|
def process_mail_from sender
|
|
|
|
if @state.include? :mail_from
|
|
|
|
@state -= [:mail_from, :rcpt, :data]
|
|
|
|
receive_reset
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
def current_message
|
|
|
|
@current_message ||= {}
|
|
|
|
end
|
2011-06-09 23:21:39 -04:00
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
def receive_reset
|
|
|
|
@current_message = nil
|
|
|
|
true
|
|
|
|
end
|
2011-06-09 23:21:39 -04:00
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
def receive_sender(sender)
|
|
|
|
current_message[:sender] = sender
|
|
|
|
true
|
|
|
|
end
|
2011-06-09 23:21:39 -04:00
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
def receive_recipient(recipient)
|
|
|
|
current_message[:recipients] ||= []
|
|
|
|
current_message[:recipients] << recipient
|
|
|
|
true
|
|
|
|
end
|
2011-06-09 23:21:39 -04:00
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
def receive_data_chunk(lines)
|
|
|
|
current_message[:source] ||= ""
|
2012-07-17 23:27:27 -04:00
|
|
|
lines.each do |line|
|
|
|
|
# RFC821 4.5.2 says leading periods should be stripped from the body data.
|
|
|
|
current_message[:source] << line.sub(/\A\./, "") << "\n"
|
|
|
|
end
|
2011-10-09 03:45:23 -04:00
|
|
|
true
|
|
|
|
end
|
2010-10-27 15:12:26 -04:00
|
|
|
|
2011-10-09 03:45:23 -04:00
|
|
|
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}"
|
2010-10-27 15:12:26 -04:00
|
|
|
end
|
2011-10-09 03:45:23 -04:00
|
|
|
puts " Please submit this as an issue at http://github.com/sj26/mailcatcher/issues"
|
|
|
|
false
|
|
|
|
ensure
|
|
|
|
@current_message = nil
|
2010-10-27 15:12:26 -04:00
|
|
|
end
|
2011-06-09 23:21:39 -04:00
|
|
|
end
|