shell mail and sendmail

shell mail and sendmail

mail

man mail ,for exampl:

echo 'body' | mail -s "Subject" -t  [email protected]    -a From:[email protected]

sendmail

send mail with body and attachment, for example(support Chinese):

filename=test.text
mail_subject="=?UTF-8?B?`echo \"测试主题\" | base64`?="
mail_body="测试正文信息,详见附件。"
mail_to="[email protected],[email protected]"
mail_form="[email protected]"
# mail boundary
boundary="=_$(uuidgen)"

#mail header
message="To: ${mail_to}\n"
message="${message}From: ${mail_form}\n"
message="${message}Subject: ${mail_subject}\n"
message="${message}MIME-Version: 1.0\n"
message="${message}Content-Type: multipart/mixed; boundary=${boundary}\n"
message="${message}\n"

#mail body Content-Transfer-Encoding: 8bit
message="${message}--${boundary}\n"
message="${message}Content-Transfer-Encoding: 8bit\n"
message="${message}Content-Type: text/plain; charset=UTF-8; format=flowed\n"
message="${message}\n"
message="${message}${mail_body}\n"
message="${message}\n"

#mail attachment format;
#more attachment file,more attachment format;follow...follow...
size=$(du -b ${filename} | awk '{print $1}')
file_mime_type=$(file ${filename} --mime-type | awk -F ': ' '{print $2}')
file_mime_encoding=$(file ${filename} --mime-encoding | awk -F ': ' '{print $2}')
message="${message}--${boundary}\n"
message="${message}Content-Transfer-Encoding: base64\n" 
message="${message}Content-Type: ${file_mime_type}; ${file_mime_encoding}; name=\"${filename}\"\n"
message="${message}Content-Disposition: attachment; filename=\"${filename}\"; size=${size}\n"
message="${message}\n$(base64 -w 0 ${filename})"

echo -e ${message} | /usr/sbin/sendmail -t
发布了236 篇原创文章 · 获赞 145 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/u011944141/article/details/103682877