我使用以下脚本从Unix服务器发送电子邮件。我需要附加一个文件,该文件位于脚本可用的相同位置 - /home/app111/attachment.csv。
你能帮我解决一下如何发送附件中的文件吗?
`CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
awk ' BEGIN {
print "To: XXXX@gmail.com"
print "From: YYYY@gmail.com"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: PO file '$CUR_DATE'"
print "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
print "<br>Thanks,</br></font></body></html>"
} ' | sendmail -t`
答案 0 :(得分:1)
1
如果支持mailx -a功能,请使用mailx -a
2
使用uuencode filenm filenm | mailx s@abc.com
3
mutt -a filenm a@abc.com
答案 1 :(得分:0)
如果你真的想使用sendmail(而不是邮件或mutt),你必须在base64中对附件进行编码,并将其连接到你的消息,以及边界和整个九码。有一篇很棒的文章用一个代码示例来描述你想要做什么: http://backreference.org/2013/05/22/send-email-with-attachments-from-script-or-command-line/
如果您使用的是具有mutt或邮件的Unix或Linux,我肯定会推荐其中一个而不是sendmail,因为它会更容易(并且这些解决方案也在发布中描述)文章)。以下是如何使用邮件执行此操作的示例:
CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
to="XXXX@gmail.com"
from="YYYY@gmail.com"
content_type="text/html"
file_to_attach="/home/app111/attachment.csv"
subject="PO file '$CUR_DATE'"
read -r -d '' body << 'EOF'
<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>
<br>Thanks,</br></font></body></html>
EOF
mail -A "$file_to_attach" --content-type "$content_type" -s "$subject" -r "$from" "$to" <<< "$body"
答案 2 :(得分:0)
试试这个:
MAILFROM="YYYY@gmail.com"
MAILTO="XXXX@gmail.com"
SUBJECT="PO file '$CUR_DATE'"
MAILPART_BODY=q1w2e3r4t5 ## Generates Unique ID
MAILPART=q1qw2ew3r4t35 ## Generates Unique ID
ATTACH="/home/app111/attachment.csv"
(
echo "From: $MAILFROM"
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
echo ""
echo "--$MAILPART"
echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
echo ""
echo "--$MAILPART_BODY"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "You need to enable HTML option for email"
echo "--$MAILPART_BODY"
echo "Content-Type: text/html; charset=ISO-8859-1"
echo "Content-Disposition: inline"
echo "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
echo "<br>Thanks,</br></font></body></html>"
echo "--$MAILPART_BODY--"
echo "--$MAILPART"
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
echo ""
uuencode $ATTACH $(basename $ATTACH)
echo "--$MAILPART--"
) | sendmail -t