# frozen_string_literal: true require 'midi-smtp-server' # Server class class BettermailProxy < MidiSmtpServer::Smtpd # get each message after DATA . def on_message_data_event(ctx) # Just decode message once to make sure, that this message ist readable mail = Mail.read_from_string(ctx[:message]) # Publish to rabbit @bunny_exchange.publish(mail.to_s, :headers => { 'x-smtp' => mail.header.to_s }, :routing_key => "new_email") end end # Create a new server instance for listening at localhost interfaces 127.0.0.1:2525 # and accepting a maximum of 4 simultaneous connections per default server = MySmtpd.new # save flag for Ctrl-C pressed flag_status_ctrl_c_pressed = false # try to gracefully shutdown on Ctrl-C trap('INT') do # print an empty line right after ^C puts # notify flag about Ctrl-C was pressed flag_status_ctrl_c_pressed = true # signal exit to app exit 0 end # Output for debug server.logger.info("Starting MySmtpd [#{MidiSmtpServer::VERSION::STRING}|#{MidiSmtpServer::VERSION::DATE}] (Basic usage) ...") # setup exit code at_exit do # check to shutdown connection if server # Output for debug server.logger.info('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed # info about shutdown server.logger.info('Shutdown MySmtpd...') # stop all threads and connections gracefully server.stop end # Output for debug server.logger.info('MySmtpd down!') end # Start the server server.start # Run on server forever server.join