我在运行Linux shell的服务器上。我需要将一个简单的文件发送给收件人。如何做到这一点,最好只使用mail命令?
更新:找到了一个好的解决方案,使用mutt代替:
$ echo | mutt -a syslogs.tar.gz admin@domain.org
使用 uuencode 的例子:
uuencode surfing.jpeg surfing.jpeg | mail sylvia@home.com
参考文章:
http://www.shelldorado.com/articles/mailattachments.html
你可以使用apt install sharutils
命令安装uuencode
命令。
mutt
的解决方案可以很好地实现这一点,并且具有合理的可移植性。 - tripleeemail
在我尝试过的所有现代Linux版本中都可以完成。无需其他软件:
matiu@matiu-laptop:~$ mail -a doc.jpg someone@somewhere.com
Subject: testing
This is a test
EOT
当您输入完毕时,请使用ctrl+d。
mail
支持此功能,但它们肯定不是“普通的mail
”,而是某种现代化的版本或变体。如果您能指明您正在使用哪个平台上的哪个版本,将会更有帮助。 - tripleee$ echo | mutt -a syslogs.tar.gz admin@domain.org
但它使用mutt,而不是mail(或mailx)。
echo | mutt admin@domain.org -a syslogs.tar.gz
。我使用的是1.5.20版本的mutt。 - Marcusmailx
不支持-a
选项(在CentOS上的mailx-8.1.1-44.2.2软件包中)。 - einpoklum-a
(OS X 10.7.5) - Stefan Schmidt除了邮件之外,我的答案还需要base64,但是一些uuencode版本也可以使用-m进行base64编码,或者您可以忽略mime并使用普通的uuencode输出...
FROM=me@mydomain.com
TO=someone@mydomain.com
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
sendmail -t
。 - Kyle MacFarlanemail
不会因为 MIME 输入而出错的情况下,但如果出现问题,你可以在管道末尾切换到 | sendmail -oi -t
。此时,你不再需要 mail
包装器提供的任何功能。 - tripleeeecho "--$boundary--"
,末尾有两个破折号来标记这是最终的、结束的边界。 - tripleeeContent-Disposition: inline
是符合RFC标准的,但显然对Yahoo有问题(但是什么不是呢);它完全是可选的,如果考虑到这一点,也许可以将其删除。另请参见https://stackoverflow.com/questions/48436217/mutt-on-linux-has-empty-text-attachments-that-appear-in-yahoo-mail - tripleeemailx -a /path/to/file email@address
您可以进入交互模式(它会提示您输入“主题:”然后是一行空白),输入主题,然后输入正文并按下Ctrl+D(EOT)完成。
mailx: illegal option -- a
- isomorphismesmailx
不是 GNU coreutils 的一部分。你可能可以找到一个支持这种用法的 OSX 版本的 mailx
,但如果没有提供链接,这并没有什么帮助。有多个版本,其中许多不支持这种用法。 - tripleeempack -a \
-s"Hey: might this serve as your report?" \
-m 0 -c application/x-tar-gz \
survey_results.tar.gz \
hesco@example.net
mpack
和munpack
与metamail
一起配合使用,扩展了mailx
的功能,在处理现代电子邮件中充斥着HTML标记和附件时非常有用。echo | mutt -a myfile.zip -- admin@domain.org
mutt
同样适用。 - Stefan Schmidtmailx
的所有其他典型特性。#!/bin/ksh
# Get the date stamp for temporary files
DT_STAMP=`date +'%C%y%m%d%H%M%S'`
# Create a multi-line body
echo "here you put the message body
which can be split across multiple lines!
woohoo!
" > body-${DT_STAMP}.mail
# Add several attachments
uuencode File1.pdf File1.pdf > attachments-${DT_STAMP}.mail
uuencode File2.pdf File2.pdf >> attachments-${DT_STAMP}.mail
# Put everything together and send it off!
cat body-${DT_STAMP}.mail attachments-${DT_STAMP}.mail > out-${DT_STAMP}.mail
mailx -s "here you put the message subject" nobody@test-address.com < out-${DT_STAMP}.mail
# Clean up temporary files
rm body-${DT_STAMP}.mail
rm attachments-${DT_STAMP}.mail
rm out-${DT_STAMP}.mail
这里有很多回答使用mutt或mailx,或者说邮件不支持“-a”。
首先,在Ubuntu 14.0.4中,mailutils的邮件支持此功能:
mail -A 文件名 -s "主题" 邮件地址
其次,我发现通过使用“man mail”命令并搜索“attach”关键词。
mail
/mailx
的几个不兼容版本,希望能够对这里和其他地方的多个冲突答案做出一些解释。 - tripleee