Development Tip

Linux에서 메일 명령을 사용하여 파일을 첨부하는 방법은 무엇입니까?

yourdevel 2020. 12. 7. 20:58
반응형

Linux에서 메일 명령을 사용하여 파일을 첨부하는 방법은 무엇입니까?


Linux 셸을 실행하는 서버에 있습니다. 받는 사람에게 간단한 파일을 보내야합니다. 이를 수행하는 방법, 선호하는 경우 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 sharutilsuuencode명령 을 가질 수 있습니다


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

그러나 그것은 mail (또는 mailx)이 아닌 mutt를 사용합니다.


mail내가 시도한 최신 Linux의 모든 버전에서 할 수 있습니다. 다른 소프트웨어가 필요하지 않습니다.

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

This is a test
EOT

입력이 끝나면 ctrl + d를 누르세요.


mailx도 도움이 될 수 있습니다. mailx man 페이지에서 :

-a file
     Attach the given file to the message.

꽤 쉽죠?


내 대답에는 메일 외에도 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

mailx -a /path/to/file email@address

대화 형 모드로 들어가서 ( "제목 :"과 빈 줄이 표시됨) 제목을 입력 한 다음 본문을 입력하고 Ctrl+ D(EOT)를 눌러 완료 할 수 있습니다.


mpack -a -s "이봐 :이게 당신의 보고서가 될까요?" -m 0 -c application / x-tar-gz survey_results.tar.gz hesco@example.net

mpack 및 munpack은 메타 메일과 함께 작동하여 mailx를 확장하고 html 마크 업 및 첨부 파일로 어수선한 최신 이메일에 유용합니다.

이 네 가지 패키지를 함께 사용하면 GUI 메일 클라이언트에서 할 수있는 모든 이메일을 처리 할 수 ​​있습니다.


우분투 10.4를 사용하여 이것은 mutt 솔루션을 작성하는 방법입니다.

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


There are a lot of answers here using mutt or mailx or people saying mail doesn't support "-a"

First, Ubuntu 14.0.4 mail from mailutils supports this:

mail -A filename -s "subject" email@example.com

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


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.

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

On Linux I would suggest,

# 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

uudecode abc.gz.enc

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


With mailx you can do:

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.

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.

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


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.

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".

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.

참고URL : https://stackoverflow.com/questions/902591/how-to-attach-a-file-using-mail-command-on-linux

반응형