我知道如何从命令行(脚本)发送电子邮件
echo "body" | mail -s "subject" my@email.com
Run Code Online (Sandbox Code Playgroud)
是否也可以从命令行(脚本)发送附件?
我heirloom-mailx在 Debian Wheezy 上使用。
rus*_*ush 20
简单的方法:使用uuencode(sharutils包的一部分)。任何格式或正文文本都不可用。只是一封带有附件和自定义主题的电子邮件。
uuencode /path/to/file file_name.ext | mail -s subject my@email.com
Run Code Online (Sandbox Code Playgroud)
复杂的方法:使用sendmail和 html 格式:
v_mailpart="$(uuidgen)/$(hostname)"
echo "To: my@email.com
Subject: subject
Content-Type: multipart/mixed; boundary=\"$v_mailpart\"
MIME-Version: 1.0
This is a multi-part message in MIME format.
--$v_mailpart
Content-Type: text/html
Content-Disposition: inline
<html><body>Message text itself.</body></html>
--$v_mailpart
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name=file_name.ext
Content-Disposition: attachment; filename=file_name.ext
`base64 /path/to/file`
--$v_mailpart--" | /usr/sbin/sendmail -t
Run Code Online (Sandbox Code Playgroud)
如果有几个附件,最后一部分可能会重复。
tkr*_*nwa 14
随着mutt代替mail您只需拨打
echo "body" | mutt -s "subject" -a attachment0 attachment1 [...] -- my@email.com
Run Code Online (Sandbox Code Playgroud)
这里attachmentN是您要附加的文件列表。