2016-08-05 105 views
1

我想在bash中使用sendmail命令發送電子郵件。電子郵件應該通過讀取Input_file_HTML來獲得它的身體,它也應該發送與附件相同的輸入文件。要做到這一點我已經試過如下:Sendmail命令發送文件作爲電子郵件正文以及附件

sendmail_touser() { 
cat - ${Input_file_HTML} << EOF | /usr/sbin/sendmail -oi -t 
From: ${MAILFROM} 
To: ${MAILTO} 
Subject: $1 
Content-Type: text/html; charset=us-ascii 
cat ${Input_file_HTML} 
Content-Transfer-Encoding: 7bit 
MIME-Version: 1.0 
Content-Disposition: attachment; filename: ${Input_file_HTML} 
EOF 
} 

上面的命令是給一封電子郵件,只有Input_file_HTML的附件,它不是在電子郵件正文中寫它。你能幫助/指導我嗎?我使用Outlook作爲電子郵件客戶端。我甚至在上面的命令中刪除了cat命令,但它也不起作用。

+0

您可以通過點擊向下投票按鈕下方的複選框來接受答案... – Questionmark

回答

2

改爲使用mutt

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- [email protected] 

要在Debian系統上安裝mutt

sudo apt-get install -y mutt 

編輯試試這個,如果你只能使用sendmail

sendmail_attachment() { 
    FROM="$1" 
    TO="$2" 
    SUBJECT="$3" 
    FILEPATH="$4" 
    CONTENTTYPE="$5" 

    (
    echo "From: $FROM" 
    echo "To: $TO" 
    echo "MIME-Version: 1.0" 
    echo "Subject: $SUBJECT" 
    echo 'Content-Type: multipart/mixed; boundary="GvXjxJ+pjyke8COw"' 
    echo "" 
    echo "--GvXjxJ+pjyke8COw" 
    echo "Content-Type: text/html" 
    echo "Content-Disposition: inline" 
    echo "<p>Message contents</p>" 
    echo "" 
    echo "--GvXjxJ+pjyke8COw" 
    echo "Content-Type: $CONTENTTYPE" 
    echo "Content-Disposition: attachment; filename=$(basename $FILEPATH)" 
    echo "" 
    cat $FILEPATH 
    echo "" 
    ) | /usr/sbin/sendmail -t 
} 

使用這樣的:

sendmail_attachment "[email protected]" "[email protected]" "Email subject" "/home/user/file.txt" "text/plain" 
+0

Hello Nick, 感謝您的回覆。我不是根,我沒有mutt命令。你能否在這裏引導我,會感激你。 – RavinderSingh13

+1

你可以讓系統管理員安裝它嗎?這是一個非常簡單的方法來實現你想要的。 –

+0

@RavinderSingh更新可能適用於您。 –

相關問題