如何自訂 Vixie-cron (debian) 和 msmtp 的電子郵件標頭?

如何自訂 Vixie-cron (debian) 和 msmtp 的電子郵件標頭?

我在讓 cron 發送電子郵件時遇到了一些麻煩。我的 ISP 要求「寄件者:」欄位與發送電子郵件的電子郵件地址匹配,否則電子郵件將被拒絕。由於 cron 將“寄件者:”欄位硬編碼為“root(Cron 守護程序)”,因此不會發送這些電子郵件。

我已經設定了 msmtp 並且可以從命令列毫無問題地發送電子郵件。事實上,我已經將一些 cron 作業封裝在一個發送電子郵件本身的腳本中。這工作正常,但我想要一個更優雅的解決方案。

我最初使用了 package msmtp-mta,它只是符號鏈接/usr/lib/sendmail/usr/bin/msmtp以便 cron 將使用 msmtp 發送電子郵件。由於這不起作用,我刪除了該包並放入了一個 bash 腳本,/usr/lib/sendmail該腳本應該只從標準輸入讀取並發送帶有正確標題的電子郵件:

#!/bin/bash

HEADERS="To: <myemail>
From: Cron <myotheremail>
Subject: Vixie-cron snooper ($@)

"

INPUT=$( cat /dev/stdin )

echo -e "$HEADERS""Stdin:\n$INPUT\n" | msmtp <myemail>
echo "$HEADERS""Stdin:\n$INPUT\n" > /tmp/vixielog

然而,這並沒有達到預期的效果。我剛剛收到一封幾乎空的電子郵件,其中/tmp/vixielog包含相同的內容:

To: <myemail>
From: Cron <myotheremail>
Subject: Vixie-cron snooper (-i -FCronDaemon -oem <myemail>)

Stdin:

電子郵件在正確的時間發送,因此我知道 cron 作業正在正常運行,但我沒有得到輸出。我該如何調整這種方法來獲取電子郵件中命令的輸出?

答案1

最終我得出了以下解決方案。我沒有使用 ,而是mstmp-mta編寫了自己的簡單 bash 腳本來充當我的 MTA。放置在 中/usr/sbin/sendmail,它會替換 From 標頭並發送電子郵件。

#!/bin/bash

sed -e "s/From: root (Cron Daemon)/From: WHATEVER YOU LIKE/" | msmtp $BASH_ARGV

希望這可以幫助任何想要輕量級解決方案的人。

答案2

它不需要從郵件標頭知道來源(先前的帖子來自: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

答案3

我為系統郵件做了一個小包裝,它解析 /etc/aliases 並以正確的格式發送郵件(透過 /usr/sbin/sendmail):

#!/bin/bash
stdin=$(cat)

mail=$(echo "$stdin" | sed "s/From: root (Cron Daemon)/From: ${4} ${4}@domain.com/g")

if [ -f /etc/aliases ]; then
        if [ "grep ${1} /etc/aliases" != "" ]; then
                exec < /etc/aliases
                while read line
                do
                        if [[ $line =~ ^${1}:\ (.*)$ ]] ; then
                        addr=${BASH_REMATCH[1]}
                        else
                                if [[ $line =~ ^default:\ (.*)$ ]] ; then
                                        addr=${BASH_REMATCH[1]}
                                fi  
                        fi  
                done
                echo $addr
        fi 
else 
addr="[email protected]"
fi

mail=$(echo "$mail" | sed "s/To: ${4}/To: ${addr} /g")

echo -e "$mail" | msmtp --auto-from=on --read-envelope-from --maildomain=domain.com -t 

只需變更預設郵件和郵件網域

答案4

如果您不介意從 msmtp-mta 發送的所有電子郵件都具有相同的「寄件者」標頭,則可以使用以下設定。別名檔案很有用,因此本地收件者(例如root:)可以工作

/etc/msmtprc:

account default
...
from [email protected]
# or from %[email protected]
set_from_header on

aliases /etc/aliases

/etc/別名

default: [email protected]

相關內容