【问题标题】:How to send excel sheet as attachment in HTML email in shell script如何在shell脚本的HTML电子邮件中将excel表作为附件发送
【发布时间】:2022-01-25 03:26:56
【问题描述】:

我想在 shell 脚本中以 HTML 电子邮件的附件形式发送员工详细信息 excel 表。我尝试使用下面的代码,但无法做到。

请帮忙。

    if [[ -f /x/project/Emp_Report.xlsx ]]; then

    echo "Employee Report exists !!"
    (
        echo "From: abc@gmail.com"
        echo "To: xyz@gmail.com"
        echo "cc: pqr@gmail.com"
        echo "Subject:Employee Report"
        echo "Content-Type: TEXT/HTML, multipart/mixed, text/html, application/octet-stream"
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: attachment;filename=/x/project/Emp_Report.xlsx"
        echo "<html>"
        echo "<body>"
        echo "<p>Hi Team,</p>"
        echo "<p>Please find the Employee details in attachment.</p>"
        echo "<p>In case of any queries, kindly reach out to 'abc@gmail.com'</p>"
        echo "<b>Regards,</b><br>"
        echo "Abc"
        echo "</body></html>"
    ) |  /usr/sbin/sendmail -t
    
    echo "Employee Report Mail hase been sent successfully !!"
  fi

【问题讨论】:

    标签: shell sh


    【解决方案1】:
    1. 在标题和正文之间需要一个空行。
    2. 附件需要 MIME 电子邮件,因此您需要边界

    以下是我工具库中发送电子邮件的 shell 脚本的 sn-p:您需要更改 text/plain

        local boundary="_====-boundary-${$}-$(date +%Y%m%d%H%M%S)-====_"
    
        {
            echo "From: $from"
            echo "To: $to"
            echo "Cc: $cc"
            echo "Subject: $subject"
            echo "Content-Type: multipart/mixed; boundary=\"$boundary\""
            echo "Mime-Version: 1.0"
            echo
            echo "This is a multi-part message in MIME format."
            echo
            printf -- "--%s\n" "$boundary"
            echo "Content-Type: text/plain; charset=ISO-8859-1"
            echo
            echo "$body"
            echo
            for filename in "${attachments[@]}"; do
                # attach it if it's readable and non-zero size
                if [[ -r "$filename" ]] && [[ -s "$filename" ]]; then
                    printf -- "--%s\n" "$boundary"
                    echo "Content-Transfer-Encoding: base64"
                    echo "Content-Type: application/octet-stream; name=$(basename "$filename")"
                    echo "Content-Disposition: attachment; filename=$(basename "$filename")"
                    echo
                    base64 "$filename"
                    echo
                fi
            done
            printf -- "--%s--\n" "$boundary"
        } | /usr/lib/sendmail -oi -t
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2012-06-25
      • 2015-09-09
      • 1970-01-01
      相关资源
      最近更新 更多