Vixie-cron(debian) 및 msmtp에서 이메일 헤더를 사용자 정의하는 방법은 무엇입니까?

Vixie-cron(debian) 및 msmtp에서 이메일 헤더를 사용자 정의하는 방법은 무엇입니까?

cron을 사용하여 이메일을 보내는 데 몇 가지 문제가 있었습니다. 내 ISP에서는 "보낸 사람:" 필드가 이메일을 보내는 이메일 주소와 일치하도록 요구합니다. 그렇지 않으면 이메일이 거부됩니다. cron이 "보낸 사람:" 필드를 "루트(Cron 데몬)"로 하드코딩하므로 이러한 이메일은 전송되지 않습니다.

msmtp를 설정했고 명령줄에서 문제 없이 이메일을 보낼 수 있습니다. 사실 저는 이메일 자체를 보내는 스크립트에 크론 작업 중 일부를 래핑했습니다. 이것은 잘 작동하지만 좀 더 우아한 솔루션을 원합니다.

나는 원래 cron이 msmtp를 사용하여 이메일을 보낼 수 있도록 msmtp-mta심볼릭 링크만 사용하는 /usr/lib/sendmail패키지 를 사용했습니다 . /usr/bin/msmtp작동하지 않았기 때문에 패키지를 제거하고 /usr/lib/sendmail대신 표준 입력에서 읽고 올바른 헤더가 포함된 이메일을 보내는 bash 스크립트를 넣었습니다.

#!/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-mtaMTA 역할을 하는 간단한 bash 스크립트를 직접 작성했습니다. 에 배치하면 /usr/sbin/sendmailFrom 헤더를 대체하고 이메일을 보냅니다.

#!/bin/bash

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

이것이 문제에 대한 가벼운 솔루션을 원하는 다른 사람에게 도움이 되기를 바랍니다.

답변2

메일 헤더의 소스를 알 필요가 없습니다(이전 게시물 보낸 사람: 루트(Cron 데몬)):

#!/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]

관련 정보