如何在 Linux 上使用邮件命令附加文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/902591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to attach a file using mail command on Linux?
提问by Seiti
I'm on a server running a Linux shell. I need to mail a simple file to a recipient. How to do this, prefereably using only the mailcommand?
我在运行 Linux shell 的服务器上。我需要将一个简单的文件邮寄给收件人。如何做到这一点,最好只使用邮件命令?
UPDATE: got a good solution, using mutt instead:
更新:得到了一个很好的解决方案,使用 mutt 代替:
$ echo | mutt -a syslogs.tar.gz [email protected]
采纳答案by Jon
Example using uuencode:
使用 uuencode 的示例:
uuencode surfing.jpeg surfing.jpeg | mail [email protected]
and reference article:
和参考文章:
http://www.shelldorado.com/articles/mailattachments.html
http://www.shelldorado.com/articles/mailattachments.html
Note:
笔记:
you may apt install sharutils
to have uuencode
command
你可能apt install sharutils
有uuencode
命令
回答by njsf
My answer needs base64 in addition to mail, but some uuencode versions can also do base64 with -m, or you can forget about mime and use the plain uuencode output...
我的回答除了邮件之外还需要 base64,但是某些 uuencode 版本也可以使用 -m 来执行 base64,或者您可以忘记 mime 并使用普通的 uuencode 输出...
[email protected]
[email protected]
SUBJECT="Auto emailed"
MIME="application/x-gzip" # Adjust this to the proper mime-type of file
FILE=somefile.tar.gz
ENCODING=base64
boundary="---my-unlikely-text-for-mime-boundary---$$--"
(cat <<EOF
From: $FROM
To: $REPORT_DEST
Subject: $SUBJECT
Date: $(date +"%a, %b %e %Y %T %z")
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"
Content-Disposition: inline
--$boundary
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
This email has attached the file
--$boundary
Content-Type: $MIME;name="$FILE"
Content-Disposition: attachment;filename="$FILE"
Content-Transfer-Encoding: $ENCODING
EOF
base64 $FILE
echo ""
echo "--$boundary" ) | mail
回答by David Winslow
回答by Seiti
$ echo | mutt -a syslogs.tar.gz [email protected]
But it uses mutt, not mail (or mailx).
但它使用 mutt,而不是 mail(或 mailx)。
回答by Hugh Esco
mpack -a -s"Hey: might this serve as your report?" -m 0 -c application/x-tar-gz survey_results.tar.gz [email protected]
mpack -a -s“嘿:这可以作为你的报告吗?” -m 0 -c 应用程序/x-tar-gzsurvey_results.tar.gz [email protected]
mpack and munpack work together with metamail to extend mailx and make it useful with modern email cluttered with html mark up and attachments.
mpack 和 munpack 与 metamail 一起工作以扩展 mailx 并使其在现代电子邮件中变得有用,这些电子邮件充斥着 html 标记和附件。
Those four packages taken together will permit you to handle any email you could in a gui mail client.
这四个包一起将允许您处理您可以在 gui 邮件客户端中处理的任何电子邮件。
回答by autonymous
mailx -a /path/to/file email@address
You might go into interactive mode (it will prompt you with "Subject: " and then a blank line), enter a subject, then enter a body and hit Ctrl+D(EOT) to finish.
您可能会进入交互模式(它会提示您“主题:”,然后是一个空行),输入主题,然后输入正文并按Ctrl+ D(EOT) 完成。
回答by Antony Gibbs
Using ubuntu 10.4, this is how the mutt solution is written
使用ubuntu 10.4,mutt解决方案是这样写的
echo | mutt -a myfile.zip -- [email protected]
echo | mutt -a myfile.zip -- [email protected]
回答by matiu
mail
on every version of modern Linux that I've tried can do it. No need for other software:
mail
在我尝试过的每个现代 Linux 版本上都可以做到。不需要其他软件:
matiu@matiu-laptop:~$ mail -a doc.jpg [email protected]
Subject: testing
This is a test
EOT
ctrl+d when you're done typing.
输入完毕后按 ctrl+d。
回答by yokeho
With mailx you can do:
使用 mailx,您可以:
mailx -s "My Subject" -a ./mail_att.csv -S [email protected] [email protected] < ./mail_body.txt
This worked great on our GNU Linux servers, but unfortunately my dev environment is Mac OsX which only has a crummy old BSD version of mailx. Normally I use Coreutils to get better versions of unix commands than the Mac BSD ones, but mailx is not in Coreutils.
这在我们的 GNU Linux 服务器上效果很好,但不幸的是,我的开发环境是 Mac OsX,它只有一个糟糕的旧 BSD 版本的 mailx。通常我使用 Coreutils 来获得比 Mac BSD 更好的 unix 命令版本,但是 mailx 不在 Coreutils 中。
I found a solution from notpeter in an unrelated thread (https://serverfault.com/questions/196001/using-unix-mail-mailx-with-a-modern-mail-server-imap-instead-of-mbox-files) which was to download the Heirloom mailx OSX binary package from http://www.tramm.li/iWiki/HeirloomNotes.html. It has a more featured mailx which can handle the above command syntax.
我在一个不相关的线程(https://serverfault.com/questions/196001/using-unix-mail-mailx-with-a-modern-mail-server-imap-instead-of-mbox-files) 这是从http://www.tramm.li/iWiki/HeirloomNotes.html下载 Heirloom mailx OSX 二进制包。它有一个功能更强大的 mailx,可以处理上述命令语法。
(Apologies for poor cross linking linking or attribution, I'm new to the site.)
(对于糟糕的交叉链接或归因,我是该网站的新手,我深表歉意。)
回答by mbrandeis
There are a lot of answers here using mutt or mailx or people saying mail doesn't support "-a"
这里有很多使用 mutt 或 mailx 的答案,或者有人说邮件不支持“-a”
First, Ubuntu 14.0.4 mail from mailutils supports this:
首先,来自 mailutils 的 Ubuntu 14.0.4 邮件支持:
mail -A filename -s "subject" [email protected]
mail -A 文件名 -s "subject" [email protected]
Second, I found that by using the "man mail" command and searching for "attach"
其次,我发现通过使用“man mail”命令并搜索“attach”