How to customise email headers from Vixie-cron (debian) and msmtp?
Eventually I came to the following solution. Rather than using mstmp-mta
, I wrote my own simple bash script that acts as my MTA. Placed in /usr/sbin/sendmail
, it replaces the From header and sends the email on.
#!/bin/bash
sed -e "s/From: root (Cron Daemon)/From: WHATEVER YOU LIKE/" | msmtp $BASH_ARGV
Hopefully this helps anybody else who wants a lightweight solution to the problem.
It doesn't need to know source from mail header (previous posts it From: root (Cron Daemon)):
#!/bin/bash
# /usr/sbin/sendmail
# We write the sent letter to the stdin variable
stdin=$(cat)
# Text to which we will replace the From header:
__REPLACE_WITH="sender name <[email protected]>"
# Find the text between From: and To :, write it to the __FIND_WHAT variable.
__FIND_WHAT=$(echo $stdin | grep -o -P '(?<=From: ).*(?=To:)')
# grep command (above) adds a space to the variable at the end of the line. It must be deleted, otherwise the text replacement will not work.
# Remove the space at the end of the variable
__FIND_WHAT=$( echo $__FIND_WHAT | sed -e 's/\s$//g' )
# Replace the text __FIND_WHAT with __REPLACE_WITH
mail=$(echo "$stdin" | sed -e "s/$__FIND_WHAT/$__REPLACE_WITH/" )
# Send a letter, with the correct sender in the header of the letter.
echo -e "$mail" | msmtp $BASH_ARGV