-
Notifications
You must be signed in to change notification settings - Fork 583
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
240e829
Use MidiSmtpServer for incoming SMTP Service
TomFreudenberg 9434417
Change Process.daemon with Process.detach
TomFreudenberg f840d4b
Remove sleep but replace process.daemon by process.detach
TomFreudenberg 43fd4f2
Allow optional STARTTLS with option --smtp-tls
TomFreudenberg af09d89
Enable internationalization options from MidiSmtpServer by default
TomFreudenberg 064cac4
Forward with changes from upstream master branch
TomFreudenberg ce8e73d
Forward with changes from upstream master branch
TomFreudenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
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[:auth_mode] = :AUTH_OPTIONAL | ||
opts[:pipelining_extension] = true | ||
opts[:internationalization_extensions] = 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 | ||
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 | ||
# re-raise to signal error to smtp client | ||
raise | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)