如何在Linux上使用mail命令附加文件?(復制)

[英]How to attach a file using mail command on Linux? [duplicate]


This question already has an answer here:

這個問題已經有了答案:

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 mail command?

我在一個運行Linux shell的服務器上。我需要把一個簡單的文件寄給收件人。如何做到這一點,最好只使用mail命令?

UPDATE: got a good solution, using mutt instead:

更新:得到一個好的解決方案,使用mutt代替:

$ echo | mutt -a syslogs.tar.gz admin@domain.org

13 个解决方案

#1


46  

Example using uuencode:

示例使用一種編碼的程式:

uuencode surfing.jpeg surfing.jpeg | mail sylvia@home.com

and reference article:

和參考文章:

http://www.shelldorado.com/articles/mailattachments.html

http://www.shelldorado.com/articles/mailattachments.html

#2


20  

$ echo | mutt -a syslogs.tar.gz admin@domain.org

But it uses mutt, not mail (or mailx).

但它使用的是mutt,而不是mail(或mailx)。

#3


17  

Plain old mail can do it. No need for other software:

普通的舊郵件可以做到。不需要其他軟件:

matiu@matiu-laptop:~$ mail -a doc.jpg someone@somewhere.com
Subject: testing

This is a test
EOT

ctrl+d when you're done typing.

輸入完畢后按ctrl+d鍵。

#4


13  

mailx might help as well. From the mailx man page:

mailx也可能有幫助。來自mailx man頁面:

-a file
     Attach the given file to the message.

Pretty easy, right?

很簡單,對吧?

#5


11  

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輸出……

   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

#6


6  

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)完成。

#7


4  

mpack -a -s"Hey: might this serve as your report?" -m 0 -c application/x-tar-gz survey_results.tar.gz hesco@example.net

嘿:這能作為你的報告嗎?- m0 -c應用/x-tar-gz測量結果。廣州hesco@example.net

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郵件客戶端的任何電子郵件。

#8


1  

Using ubuntu 10.4, this is how the mutt solution is written

使用ubuntu 10.4,這就是mutt解決方案的編寫方式

echo | mutt -a myfile.zip -- admin@domain.org

我的文件。zip——admin@domain.org

#9


1  

The following is a decent solution across Unix/Linux installations, that does not rely on any unusual program features. This supports a multi-line message body, multiple attachments, and all the other typical features of mailx.

下面是一個不錯的解決方案,跨Unix/Linux安裝,它不依賴於任何不尋常的程序特性。它支持多行消息主體、多個附件和mailx的所有其他典型特性。

Unfortunately, it does not fit on a single line.

不幸的是,它不適合任何一行。

#!/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

#10


0  

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@example.com

郵件-文件名-s "subject" email@example.com

Second, I found that by using the "man mail" command and searching for "attach"

其次,我發現通過使用“man mail”命令並搜索“attach”

#11


0  

On Linux I would suggest,

在Linux上,

# FILE_TO_BE_ATTACHED=abc.gz

# FILE_TO_BE_ATTACHED = abc.gz

uuencode abc.gz abc.gz > abc.gz.enc # This is optional, but good to have
                                    # to prevent binary file corruption.
                                    # also it make sure to get original 
                                    # file on other system, w/o worry of endianness

# Sending Mail, multiple attachments, and multiple receivers.
echo "Body Part of Mail" | mailx -s "Subject Line" -a attachment1 -a abc.gz.enc "youremail@domain.com anotheremail@domain.com"

Upon receiving mail attachment, if you have used uuencode, you would need uudecode

在收到郵件附件時,如果您使用了uuencode,您將需要uudecode

uudecode abc.gz.enc

# This will generate file as original with name as same as the 2nd argument for uuencode.

這將生成與uuencode的第二個參數相同的原始文件名。

#12


0  

I use mailutils and the confusing part is that in order to attach a file you need to use the capital A parameter. below is an example.

我使用mailutils,令人困惑的部分是,為了附加一個文件,您需要使用大寫的參數。下面是一個例子。

echo 'here you put the message body' | mail -A syslogs.tar.gz admin@domain.org

If you want to know if your mail command is from mailutils just run "mail -V".

如果您想知道您的郵件命令是否來自mailutils,請運行“mail -V”。

root@your-server:~$ mail -V
mail (GNU Mailutils) 2.99.98
Copyright (C) 2010 Free Software Foundation, inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

#13


0  

With mailx you can do:

使用mailx你可以做到:

mailx -s "My Subject"  -a ./mail_att.csv -S from=noreply@foo.com  recipient@bar.com < ./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)中找到了一個解決方案。它有一個更有特色的mailx,可以處理上面的命令語法。

(Apologies for poor cross linking linking or attribution, I'm new to the site.)

(不好意思交聯鏈接或歸屬不好,我是這個網站的新手。)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/23/e210ab7bbf59e77349d7424d493904ac.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com