Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MidiSmtpServer for incoming SMTP Service - Enables AUTH and STARTTLS #386

Closed
wants to merge 7 commits into from
Closed
25 changes: 20 additions & 5 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Apparently rubygems won't activate these on its own, so here we go. Let's
# repeat the invention of Bundler all over again.
gem "eventmachine", "1.0.9.1"
gem "midi-smtp-server", "~> 2.3.1"
gem "mail", "~> 2.3"
gem "rack", "~> 1.5"
gem "sinatra", "~> 1.2"
Expand All @@ -12,6 +13,8 @@
require "optparse"
require "rbconfig"


require "midi-smtp-server"
require "eventmachine"
require "thin"

Expand Down Expand Up @@ -176,6 +179,14 @@ def run! options=nil
# Stash them away for later
@@options = options

# daemonize mode, reap the child automatically to prevent zombie
if options[:daemon] && pid = fork
Process.detach(pid)
# wait a second to make sure that all output is send before exit
sleep 2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not sleeping a second the output from command line is printed too fast so that the output of starting mailcatcher will follow later. That confuses the user.

exit
end

# If we're running in the foreground sync the output.
unless options[:daemon]
$stdout.sync = $stderr.sync = true
Expand All @@ -188,9 +199,9 @@ def run! options=nil

# One EventMachine loop...
EventMachine.run do
# Set up an SMTP server to run within EventMachine
# Set up MidiSmtpServer server to run within EventMachine loop
rescue_port options[:smtp_port] do
EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
Smtp.new(options[:smtp_port], options[:smtp_ip], 4, { logger_severity: development? ? 0:9 }).start
puts "==> #{smtp_url}"
end

Expand All @@ -216,7 +227,10 @@ def run! options=nil
else
puts "*** MailCatcher is now running as a daemon that cannot be quit."
end
Process.daemon
# when daemonize redirect standard input, standard output and standard error to /dev/null
$stdin.reopen "/dev/null"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-routing stdin, stdout and stderr was former handled by Process.daemon.
Read at https://ruby-doc.org/core-2.1.0/Process.html#method-c-daemon
Second paramter (noclose = nil)

$stdout.reopen "/dev/null", "a"
$stderr.reopen '/dev/null', 'a'
end
end
end
Expand All @@ -241,8 +255,9 @@ def rescue_port port
yield

# XXX: EventMachine only spits out RuntimeError with a string description
rescue RuntimeError
if $!.to_s =~ /\bno acceptor\b/
# XXX: MidiSmtpServer uses seperate ErrorClass
rescue RuntimeError, Errno::EADDRINUSE
if $!.to_s =~ /\bno acceptor\b/ || $!.class == Errno::EADDRINUSE
puts "~~> ERROR: Something's using port #{port}. Are you already running MailCatcher?"
puts "==> #{smtp_url}"
puts "==> #{http_url}"
Expand Down
67 changes: 27 additions & 40 deletions lib/mail_catcher/smtp.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,42 @@
require "eventmachine"
require "midi-smtp-server"

require "mail_catcher/mail"

class MailCatcher::Smtp < EventMachine::Protocols::SmtpServer
# 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

def current_message
@current_message ||= {}
end
class MailCatcher::Smtp < MidiSmtpServer::Smtpd

def receive_reset
@current_message = nil
true
def initialize(ports = DEFAULT_SMTPD_PORT, hosts = DEFAULT_SMTPD_HOST, max_processings = DEFAULT_SMTPD_MAX_PROCESSINGS, opts = {})
# set compatible modes and enable optional TLS and AUTH per default
opts[:io_cmd_timeout] = nil
opts[:tls_mode] = :TLS_OPTIONAL
opts[:auth_mode] = :AUTH_OPTIONAL
opts[:pipelining_extension] = true
# initialize MidiSmtpServer::Smtpd
super ports, hosts, max_processings, opts
end

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

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

def receive_data_chunk(lines)
current_message[:source] ||= ""
lines.each do |line|
current_message[:source] << line << "\r\n"
def on_auth_event(ctx, authorization_id, authentication_id, authentication)
# simply allow any combination of user and password
if authentication_id != '' && authentication != ''
# simulate successful authentification
return 'mailcatcher'
end
true
# otherwise exit with authentification exception
raise MidiSmtpServer::Smtpd535Exception
end

def receive_message
def on_message_data_event(ctx)
# build current_message from ctx values
current_message = {}
current_message[:sender] = ctx[:envelope][:from]
current_message[:recipients] = ctx[:envelope][:to]
current_message[:source] = ctx[:message][:data]
# append to MailCatcher
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
# re-raise to signal error to smtp client
raise
end

end
1 change: 1 addition & 0 deletions mailcatcher.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 2.0.0"

s.add_dependency "eventmachine", "1.0.9.1"
s.add_dependency "midi-smtp-server", "~> 2.3.1"
s.add_dependency "mail", "~> 2.3"
s.add_dependency "rack", "~> 1.5"
s.add_dependency "sinatra", "~> 1.2"
Expand Down