Skip to content

Instantly share code, notes, and snippets.

@sj26
Last active September 23, 2021 05:47
Show Gist options
  • Save sj26/8f638252ab64b1d0ecbb to your computer and use it in GitHub Desktop.
Save sj26/8f638252ab64b1d0ecbb to your computer and use it in GitHub Desktop.
Example mailcatcher setup with foreman

MailCatcher with Bundler and Foreman

Unforunately bundler likes to hijack all of the rubies everywhere. If you start foreman with a bundle exec foreman it will make sure every single ruby program executed (even things like hub, gh, and vagrant) will all load up your bundle, and complain if they're not also inside the bundle.

The best solution is to use a system-installed foreman without bundle exec, and only bundle exec (or use binstubs for) the tools you want to load within the bundle environment.

A workaround is provided here which cleans the bundler env before starting the required subprocess.

This is a complicated issue but feel free to reach out to me: @sj26 or sj26@sj26.com

Running this example

Something like:

$ git clone https://gist.github.com/8f638252ab64b1d0ecbb.git mailcatcher-test
Cloning into 'mailcatcher-test'...
...
$ cd mailcatcher-test
$ bundle
Installing/Using ...
...
Your bundle is complete!
$ bundle exec foreman start
14:45:16 web.1         | started with pid 54321
14:45:16 mailcatcher.1 | started with pid 54322
...
14:45:17 web.1         | [2014-05-07 14:45:17] INFO  WEBrick::HTTPServer#start: pid=89203 port=9292
...

Go to http://localhost:9292

require "sinatra"
require "mail"
Mail.defaults do
delivery_method :smtp, address: "127.0.0.1", port: "1025"
end
class Thrower < Sinatra::Application
enable :inline_templates
get "/" do
erb :index
end
get "/throw" do
Mail.new(erb(:mail, layout: false)).deliver
erb :throw
end
end
run Thrower
__END__
@@ layout
<html>
<head>
<title>Thrower</title>
</head>
<body>
<%= yield %>
</body>
</html>
@@ index
<p><a href="/throw">Throw a mail</a></p>
@@ throw
<p>Great, now <a href="http://localhost:1080">go catch it!</a></p>
@@ mail
To: someone@example.com
From: someone-else@example.com
Subject: Catch me!
Hey there!
Thought you might like to know it's <%= Time.now %>.
source "https://rubygems.org"
gem "foreman"
gem "rack"
gem "sinatra"
gem "mail"
GEM
remote: https://rubygems.org/
specs:
dotenv (0.7.0)
foreman (0.67.0)
dotenv (~> 0.7.0)
thor (~> 0.17.0)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
polyglot (0.3.4)
rack (1.5.2)
rack-protection (1.5.3)
rack
sinatra (1.4.5)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
thor (0.17.0)
tilt (1.4.1)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
PLATFORMS
ruby
DEPENDENCIES
foreman
mail
rack
sinatra
web: rackup
mailcatcher: ruby -rbundler/setup -e "Bundler.clean_exec('mailcatcher', '--foreground')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment