Skip to content

Instantly share code, notes, and snippets.

@benleov
Last active April 5, 2016 03:06
Show Gist options
  • Save benleov/9ae6040166de7227130755aa1a2362c2 to your computer and use it in GitHub Desktop.
Save benleov/9ae6040166de7227130755aa1a2362c2 to your computer and use it in GitHub Desktop.
This script writes emails piped from postfix into separate files.
#!/bin/bash
#
# This script writes emails piped from postfix into separate files. It also removes
# new line delimiters which causes issues when viewing in Outlook.
#
# Setup:
#
# It requires lines in master.cf that looks something like this:
#
# to_filesystem unix - n n - - pipe
# flags=FR user=emailuser argv=/root/mail_write_script.sh ${recipient} ${queue_id}
#
# And for main.cf to route to this output, e.g:
#
# default_transport = to_filesystem
#
# Finally, a user called "emailuser" will need to be able to execute this script and write to the
# directory /opt/mail.
#
# Script:
# read from stdin
DATA=$(cat)
# variables passed from postfix
RECIPIENT=$1
QUEUE_ID=$2
# remove =\n and replace with \n
PARSED=$(echo "$DATA" | sed ':a;N;$!ba;s/=\n//g')
# calculate directory
DESTINATION_DIR=/opt/mail/${RECIPIENT}_${QUEUE_ID}.eml
echo "$PARSED" > "$DESTINATION_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment