Blogmark
Postfix Setup for Action Mailbox
via jbranchaud@gmail.com
This stackoverflow answer provides a ton of good detail how on to get Postfix setup with Rails' Action Mailbox.
I also wanted to document the steps I took with the added detail that this is for a Hatchbox and Hetzner configuration:
Find the email that Hatchbox sent ([Hatchbox] Your new server password) with the password for the
deploy
user to be able to run commands asroot
withsudo
. This is needed for a couple things.Hatchbox / Hetzner do not have
postfix
installed, so it needs to be done manually.$ sudo apt-get update $ sudo apt-get install postfix
Create the virtual mailbox mapping file which tells Postfix what our catch-all email recipient will be —
sudo vi /etc/postfix/virtual
.mydomain.tld anything @mydomain.tld catch-all@mydomain.tld
The first line tells Postfix that we accept email for our domain. The second line is the catch-all line which takes any mail that doesn't match a specific address and sends it to
catch-all@mydomain.tld
assuming thecatch-all
user exists.Create the
catch-all
user.$ sudo useradd --create-home --shell /sbin/nologin catch-all
Add Postfix transport file which indicates the name of the thing that is going to forward emails to Rails —
sudo vi /etc/postfix/transport
:mydomain.tld forward_to_rails:
Compile
virtual
andtransport
into Berkley DB files withpostmap
. This can be done from whatever directory you're already in.$ sudo postmap /etc/postfix/virtual $ sudo postmap /etc/postfix/transport
Notice in
ls /etc/postfix
that there is now avirtual.db
andtransport.db
file.Add this email forwarding script (with the command mentioned in the Rails Action Mailbox docs for Postfix) to
/usr/local/bin
in a file likeemail_forwarder.sh
:#!/bin/sh cd /home/deploy/visualmode-dev-rails-app/current && bin/rails action_mailbox:ingress:postfix URL='https://mydomain.tld/rails/action_mailbox/relay/inbound_emails' INGRESS_PASSWORD='ingress_password'
Note that this command needs a
URL
which is your fully-qualified URL followed by/rails/action_mailbox/relay/inbound_emails
. It also needs theINGRESS_PASSWORD
that Rails will be configured with, such as with theRAILS_INBOUND_EMAIL_PASSWORD
env var.Then at the end of the
master.cf
file, I added the following lines, ensuring not to modify anything else in there —sudo vi /etc/postfix/master.cf
:forward_to_rails unix - n n - - pipe flags=Xhq user=deploy:deploy argv=/usr/local/bin/email_forwarder.sh ${nexthop} ${user}
Since my user is already called
deploy
, I can leave theuser=deploy:deploy
as is.Update the
main.cf
file with mapping and transport files —sudo vi /etc/postfix/main.cf
:transport_maps = hash:/etc/postfix/transport virtual_alias_maps = hash:/etc/postfix/virtual
Then to make sure that Postfix is aware of all the latest changes, I reload it.
$ sudo postfix reload $ sudo systemctl reload postfix