![](/img/trans.png)
[英]How to write a shell script to pick the excel sheet from particular path and mail it as attachment?
[英]How to send excel sheet as attachment in HTML email in shell script
我想在 shell 腳本中將員工詳細信息 excel 表作為附件發送到 HTML email 中。 我嘗試使用以下代碼但無法做到。
請幫忙解決這個問題。
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 腳本的片段:您需要更改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
我瀏覽了幾個來源並編寫了以下代碼。
它非常適合我。
export ATTACHMENT=/x/project/Emp_Report.xlsx
EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}
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 "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
echo '--${EMAIL_BOUNDARY_STRING}'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
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>"
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACHMENT)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACHMENT)'"'
uuencode -m $ATTACHMENT $(basename $ATTACHMENT)
) | /usr/sbin/sendmail -t
echo "Employee Report Mail hase been sent successfully !!"
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.