Sendmail命令将文件作为电子邮件正文以及附件发送 [英] Sendmail command to send a file as an email body as well as attachment
问题描述
我想使用bash
中的sendmail命令发送电子邮件.电子邮件应通过阅读Input_file_HTML
来获取其内容,并且还应发送相同的输入文件作为附件.为此,我尝试了以下方法.
I want to send an email using sendmail command in bash
. The email should get it's body by reading Input_file_HTML
and it should send same input file as an attachment too. To do so I have tried the following.
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
命令,但是它也无法正常工作.
The above command is giving an email with only the attachment of Input_file_HTML
and it is not writing it in the body of email. Could you please help/guide me on same? I am using outlook as the email client. I have even removed the cat
command in above command, but it also is not working.
推荐答案
使用mutt
代替?
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com
要在Debian系统上安装mutt
:
To install mutt
on Debian systems:
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 "to@example.com" "from@example.com" "Email subject" "/home/user/file.txt" "text/plain"
这篇关于Sendmail命令将文件作为电子邮件正文以及附件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!