Reputation: 626
I followed the instructions from the offical Ruby on Rails Guide. I can run a script on incoming mail.
But I have no clue how to invoke the action_mailbox:ingress:postfix command. Somehow I have to switch into the workspace and then run bin/rails. Is there some best practice for sending the mail to rails?
Upvotes: 3
Views: 1221
Reputation: 369
You can follow the steps to configure postfix with action mailbox in production server:
Step 1: Create bash script
Create a script inside /usr/local/bin/
to forward all incoming emails to our rails app:
$ nano email_forwarder.sh
Add following to the script:
#!/bin/sh
export HOME=YOUR_HOME_PATH
export PATH=YOUR_PATH
export RBENV_ROOT=YOUR_RBENV_PATH
cd /path/to/your/project && bin/rails action_mailbox:ingress:postfix URL='https://truemark.com.np/rails/action_mailbox/relay/inbound_emails' INGRESS_PASSWORD='YOUR_INGRESS_PASSWORD'
Replace value of HOME
, PATH
, RBENV_ROOT
, URL
and INGRESS_PASSWORD
as described below:
HOME
$ cd
$ pwd
$ $PATH
$ which rbenv
Copy the password you added to credentials
or your ENV
/ application.yml
file for INGRESS_PASSWORD
For URL, if your application lived at https://example.com, the full command would look like this:
bin/rails action_mailbox:ingress:postfix URL='https://example.com/rails/action_mailbox/relay/inbound_emails' INGRESS_PASSWORD='YOUR_STRONG_PASSWORD'
Step 2: Configure Postfix to Pipe Incoming emails to script
/etc/postfix/virtual_aliases
to add a catch-all alias; localuser needs to be an existing local user:# /etc/postfix/virtual_aliases
@mydomain.tld [email protected]
/etc/postfix/transport
to add a transport mapping. forward_to_rails can be whatever you want; it will be used later in master.cf
# /etc/postfix/transport
mydomain.tld forward_to_rails:
transport
and virtual_aliases
need to be compiled into berkeley db files:$ sudo postmap /etc/postfix/virtual_aliases
$ sudo postmap /etc/postfix/transport
/etc/postfix/master.cf
# /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}
We should specify user so script is run by that user and not postfix or nobody. user=deploy:deploy
~ user=user:group
/etc/postfix/main.cf
# /etc/postfix/main.cf
transport_maps = hash:/etc/postfix/transport
virtual_alias_maps = hash:/etc/postfix/virtual_aliases
You can view postfix log with tail -f /var/log/mail.log
.
And done! You should be able to receive the email now in you action mailbox.
You can read about the solution in detail here: https://thedevpost.com/blog/setup-action-mailbox-with-postfix-part-2/
For setting up action mailbox and testing in development, you can read about it in detail here: https://thedevpost.com/blog/setup-action-mailbox-with-postfix-part-1/
Upvotes: 4
Reputation:
From the guide:
Configure Postfix to pipe inbound emails to
bin/rails action_mailbox:ingress:postfix
, providing theURL
of the Postfix ingress and theINGRESS_PASSWORD
you previously generated. If your application lived athttps://example.com
, the full command would look like this:$ bin/rails action_mailbox:ingress:postfix URL=https://example.com/rails/action_mailbox/relay/inbound_emails INGRESS_PASSWORD=...
So postfix is the one running that command
Upvotes: 0