Blogmark

Postfix Setup for Action Mailbox

via jbranchaud@gmail.com

https://stackoverflow.com/a/61629911/535590

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:

  1. Find the email that Hatchbox sent ([Hatchbox] Your new server password) with the password for the deploy user to be able to run commands as root with sudo. This is needed for a couple things.

  2. Hatchbox / Hetzner do not have postfix installed, so it needs to be done manually.

    $ sudo apt-get update
    $ sudo apt-get install postfix
    
  3. 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 the catch-all user exists.

  4. Create the catch-all user.

    $ sudo useradd --create-home --shell /sbin/nologin catch-all
    
  5. 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:
    
  6. Compile virtual and transport into Berkley DB files with postmap. 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 a virtual.db and transport.db file.

  7. Add this email forwarding script (with the command mentioned in the Rails Action Mailbox docs for Postfix) to /usr/local/bin in a file like email_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 the INGRESS_PASSWORD that Rails will be configured with, such as with the RAILS_INBOUND_EMAIL_PASSWORD env var.

  8. 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 the user=deploy:deploy as is.

  9. 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
    
  10. Then to make sure that Postfix is aware of all the latest changes, I reload it.

    $ sudo postfix reload
    $ sudo systemctl reload postfix