发送带有附件和表格格式邮件正文的邮件

发送带有附件和表格格式邮件正文的邮件

我需要发送一封带有表格格式的附件和邮件正文的邮件。我使用下面的代码来发送邮件。但无法附加文件。

 (echo "From: ";    
  echo "Subject: testing of table using awk";  
  echo "Content-type: text/html";    
  echo;     
  awk 'BEGIN{print "<table>"} {print "<tr><tr>";for(i=1;i<=NF;i++)print "<td><td>" $i"</td></td>";print  "</tr></tr>"} END{print "</table>"}' input1.txt;
 ) | sendmail [email protected]

答案1

尝试使用支持(附件) switch的mail命令版本。-a这是更可靠的解决方案。

蜗牛有它 !

email=foo@base

mail -v -s subject foo@bar -a file.txt -q - <<EOF 
$(awk '
    BEGIN{print "<table>"}
    {
        print "<tr><tr>";
        for(i=1;i<=NF;i++) {
            print "<td><td>" $i"</td></td>";
            print  "</tr></tr>";
        }
    }
    END{print "</table>"}' input1.txt
)

.
EOF

检查man mail是否需要添加标题等

答案2

使用sendmail

TO_ADDRESS="[email protected]"
SUBJECT="Test Mail"

cat << --OEF--
Subject: $SUBJECT
TO: $TO_ADDRESS
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="MAIL_BOUNDARY"


--MAIL_BOUNDARY
Content-Type: multipart/alternative;
boundary="MAIL_BOUNDARY2"

--MAIL_BOUNDARY2
Content-Type: text/plain; charset=utf-8

$SUBJECT

--MAIL_BOUNDARY2
Content-Type: text/html; charset=utf-8

--OEF--
cat MailBody.html 

cat << --OEF--

--MAIL_BOUNDARY2--

--MAIL_BOUNDARY
Content-Type: application/zip; name=file.zip
Content-Disposition: attachment; filename=file.zip
Content-Transfer-Encoding: base64

--OEF--
base64  /opt/file.zip
cat << --OEF--

--MAIL_BOUNDARY--
--OEF--
) |  /usr/sbin/sendmail $TO_ADDRESS 

相关内容