shell mail and sendmail

孟高峰
2023-12-01

shell mail and sendmail

mail

man mail ,for exampl:

echo 'body' | mail -s "Subject" -t  mail@domain.com    -a From:mail@domain.com

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="mail-1@domain.com,mail-2@domain.com"
mail_form="mail-1@domain.com"
# 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
 类似资料:

相关阅读

相关文章

相关问答