Module: MailCatcher

Extended by:
MailCatcher
Included in:
MailCatcher
Defined in:
lib/mail_catcher.rb,
lib/mail_catcher/bus.rb,
lib/mail_catcher/web.rb,
lib/mail_catcher/version.rb,
lib/mail_catcher/integrations.rb,
lib/mail_catcher/integrations/mcp_tools.rb,
lib/mail_catcher/integrations/mcp_server.rb,
lib/mail_catcher/web/application.rb

Defined Under Namespace

Modules: Integrations, Mail, Web Classes: Smtp, SmtpTls

Constant Summary collapse

Bus =
EventMachine::Channel.new
VERSION =
'1.5.7'

Instance Method Summary collapse

Instance Method Details

#browsable?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mail_catcher.rb', line 51

def browsable?
  windows? or which? 'open'
end

#browse(url) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/mail_catcher.rb', line 55

def browse(url)
  if windows?
    system 'start', '/b', url
  elsif which? 'open'
    system 'open', url
  end
end

#development?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/mail_catcher.rb', line 37

def development?
  env == 'development'
end

#envObject



33
34
35
# File 'lib/mail_catcher.rb', line 33

def env
  ENV.fetch('MAILCATCHER_ENV', 'production')
end

#http_serverObject



107
108
109
# File 'lib/mail_catcher.rb', line 107

def http_server
  @http_server
end

#log_exception(message, context, exception) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mail_catcher.rb', line 63

def log_exception(message, context, exception)
  gems_paths = (Gem.path | [Gem.default_dir]).map { |path| Regexp.escape(path) }
  gems_regexp = %r{(?:#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)}
  gems_replace = '\1 (\2) \3'

  @logger.error("#{message}: #{context.inspect}")
  @logger.error("Exception: #{exception}")

  backtrace = exception.backtrace&.map { |line| line.sub(gems_regexp, gems_replace) }
  backtrace&.each { |line| @logger.error("  #{line}") }

  @logger.error('Please submit this as an issue at https://github.com/spaquet/mailcatcher')
end

#optionsObject



103
104
105
# File 'lib/mail_catcher.rb', line 103

def options
  @options
end

#parse!(arguments = ARGV, defaults = @defaults) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/mail_catcher.rb', line 115

def parse!(arguments = ARGV, defaults = @defaults)
  @defaults.dup.tap do |options|
    OptionParser.new do |parser|
      parser.banner = 'Usage: mailcatcher [options]'
      parser.version = VERSION
      parser.separator ''
      parser.separator "MailCatcher NG v#{VERSION}"
      parser.separator ''

      parser.on('--ip IP', 'Set the ip address of both servers') do |ip|
        options[:smtp_ip] = options[:http_ip] = ip
      end

      parser.on('--smtp-ip IP', 'Set the ip address of the smtp server') do |ip|
        options[:smtp_ip] = ip
      end

      parser.on('--smtp-port PORT', Integer, 'Set the port of the smtp server') do |port|
        options[:smtp_port] = port
      end

      parser.on('--smtp-ssl', 'Enable SSL/TLS support for SMTP') do
        options[:smtp_ssl] = true
      end

      parser.on('--smtp-ssl-cert PATH', 'Path to SSL certificate file (required with --smtp-ssl)') do |path|
        options[:smtp_ssl_cert] = path
      end

      parser.on('--smtp-ssl-key PATH', 'Path to SSL private key file (required with --smtp-ssl)') do |path|
        options[:smtp_ssl_key] = path
      end

      parser.on('--smtp-ssl-verify-peer', 'Verify client SSL certificates') do
        options[:smtp_ssl_verify_peer] = true
      end

      parser.on('--smtps-port PORT', Integer, 'Set the port for direct TLS SMTP server (default: 1465)') do |port|
        options[:smtps_port] = port
      end

      parser.on('--http-ip IP', 'Set the ip address of the http server') do |ip|
        options[:http_ip] = ip
      end

      parser.on('--http-port PORT', Integer, 'Set the port address of the http server') do |port|
        options[:http_port] = port
      end

      parser.on('--messages-limit COUNT', Integer,
                'Only keep up to COUNT most recent messages') do |count|
        options[:messages_limit] = count
      end

      parser.on('--persistence', 'Store messages in a persistent SQLite database file') do
        options[:persistence] = true
      end

      parser.on('--http-path PATH', String, 'Add a prefix to all HTTP paths') do |path|
        clean_path = Rack::Utils.clean_path_info("/#{path}")

        options[:http_path] = clean_path
      end

      parser.on('--forward-smtp-host HOST', 'SMTP server for forwarding messages') do |host|
        options[:forward_smtp_host] = host
      end

      parser.on('--forward-smtp-port PORT', Integer, 'SMTP port for forwarding messages') do |port|
        options[:forward_smtp_port] = port
      end

      parser.on('--forward-smtp-user USER', 'SMTP username for forwarding messages') do |user|
        options[:forward_smtp_user] = user
      end

      parser.on('--forward-smtp-password PASSWORD', 'SMTP password for forwarding messages') do |password|
        options[:forward_smtp_password] = password
      end

      parser.on('--[no-]forward-smtp-tls', 'Enable/disable TLS for forwarding SMTP (default: enabled)') do |tls|
        options[:forward_smtp_tls] = tls
      end

      parser.on('--no-quit', "Don't allow quitting the process") do
        options[:quit] = false
      end

      parser.on('--mcp', 'Enable MCP server for Claude integration') do
        options[:mcp_enabled] = true
      end

      parser.on('--plugin', 'Enable Claude Plugin endpoints') do
        options[:plugin_enabled] = true
      end

      unless windows?
        parser.on('-f', '--foreground', 'Run in the foreground') do
          options[:daemon] = false
        end
      end

      if browsable?
        parser.on('-b', '--browse', 'Open web browser') do
          options[:browse] = true
        end
      end

      parser.on('-v', '--verbose', 'Be more verbose') do
        options[:verbose] = true
      end

      parser.on_tail('-h', '--help', 'Display this help information') do
        puts parser
        exit
      end

      parser.on_tail('--version', 'Display the current version') do
        puts "MailCatcher v#{VERSION}"
        exit
      end
    end.parse!
  end
end

#quit!Object



320
321
322
323
324
# File 'lib/mail_catcher.rb', line 320

def quit!
  MailCatcher::Bus.push(type: 'quit')

  EventMachine.next_tick { EventMachine.stop_event_loop }
end

#quittable?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/mail_catcher.rb', line 111

def quittable?
  options[:quit]
end

#run!(options = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/mail_catcher.rb', line 240

def run!(options = nil)
  # If we are passed options, fill in the blanks
  options &&= @defaults.merge options
  # Otherwise, parse them from ARGV
  options ||= parse!

  # Stash them away for later
  @options = options

  # Validate SSL configuration if enabled
  validate_ssl_config!

  # If we're running in the foreground sync the output.
  $stdout.sync = $stderr.sync = true unless options[:daemon]

  @logger.info("Starting MailCatcher NG v#{VERSION}")

  Thin::Logging.debug = development?
  Thin::Logging.silent = !development?
  @logger.level = development? ? Logger::DEBUG : Logger::INFO

  # Configure SSL/TLS if enabled
  configure_smtp_ssl!

  # One EventMachine loop...
  EventMachine.run do
    # Set up an SMTP server to run within EventMachine
    rescue_port options[:smtp_port] do
      EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
      @logger.info("==> #{smtp_url}")
    end

    # Set up direct TLS (SMTPS) server if SSL is enabled
    if options[:smtp_ssl]
      rescue_port options[:smtps_port] do
        EventMachine.start_server options[:smtp_ip], options[:smtps_port], SmtpTls
        @logger.info("==> #{smtps_url}")
      end
    end

    # Let Thin set itself up inside our EventMachine loop
    # Faye connections are hijacked but continue to be supervised by thin
    rescue_port options[:http_port] do
      @http_server = Thin::Server.new(options[:http_ip], options[:http_port], Web, signals: false)
      @http_server.start
      @logger.info("==> #{http_url}")
    end

    # Start integrations (MCP, Plugins, etc.)
    if options[:mcp_enabled] || options[:plugin_enabled]
      Integrations.start(options)
    end

    # Make sure we quit nicely when asked
    # We need to handle outside the trap context, hence the timer
    trap('INT') { EM.add_timer(0) { quit! } }
    trap('TERM') { EM.add_timer(0) { quit! } }
    trap('QUIT') { EM.add_timer(0) { quit! } } unless windows?

    # Open the web browser before detaching console
    if options[:browse]
      EventMachine.next_tick do
        browse http_url
      end
    end

    # Daemonize, if we should, but only after the servers have started.
    if options[:daemon]
      EventMachine.next_tick do
        if quittable?
          @logger.info('MailCatcher runs as a daemon by default. Go to the web interface to quit.')
        else
          @logger.info('MailCatcher is now running as a daemon that cannot be quit.')
        end
        Process.daemon
      end
    end
  end
end

#which?(command) ⇒ Boolean

Returns:

  • (Boolean)


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

def which?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    File.executable?(File.join(directory, command.to_s))
  end
end

#windows?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/mail_catcher.rb', line 47

def windows?
  RbConfig::CONFIG['host_os'].match?(/mswin|mingw/)
end