How to mount a file using the mail command on Linux?

I am on a server on which the Linux shell is installed. I need to send a simple file to the recipient. How to do this, it is preferable to use only the mail command?

UPDATE : got a good solution, using mutt instead:

$ echo | mutt -a syslogs.tar.gz admin@domain.org 
+57
linux unix shell email
May 23 '09 at 10:05 p.m.
source share
13 answers

An example of using uuencode:

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

and reference article:

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

Remarks:

You can apt install sharutils to use the uuencode command

+48
May 23 '09 at 10:09 PM
source share
— -
 $ echo | mutt -a syslogs.tar.gz admin@domain.org 

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

+20
May 23 '09 at 22:49
source share

mail on every version of modern Linux I tried can do this. 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.

+18
May 29 '14 at 15:46
source share

mailx can also help. On the mailx man page:

 -a file Attach the given file to the message. 

Pretty easy, right?

+12
May 23 '09 at 10:24 a.m.
source share

My answer requires base64 in addition to mail, but some versions of uuencode can also do base64 with -m, or you can forget about mime and use simple uuencode output ...

  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 
+11
May 23 '09 at
source share
 mailx -a /path/to/file email@address 

You can go online (it will offer you “Subject:” and then an empty line), enter the subject, then enter the body and press Ctrl + D (EOT) to finish.

+7
Apr 25 2018-12-12T00:
source share

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

mpack and munpack work together with metamalia for the mailx extension and make it useful with modern email, cluttered with html markup and attachments.

These four packages, taken together, will allow you to process any email that you could use with the gui email client.

+4
Oct 18 '11 at 18:00
source share

Using ubuntu 10.4, this is how mutt solution is written

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

+2
Sep 23 '13 at 8:28
source share

There are many answers here using mutt or mailx or people who say that mail does not support "-a"

Firstly, Ubuntu 14.0.4 mail from mailutils supports this:

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

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

+1
Mar 14 '16 at 20:40
source share

The following is a decent solution for Unix / Linux installations, which is independent of any unusual program features. It supports multi-line message, multiple attachments and all other typical mailx functions.

Unfortunately, it does not fit on one 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 
+1
Jan 31 '18 at 18:00
source share

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 development environment is Mac OsX, which only has the old version of mailx for BSD installed. I usually use Coreutils to get better versions of Unix commands than for Mac BSD, but mailx is missing from Coreutils.

I found a solution from notpeter in an unrelated topic ( https://serverfault.com/questions/196001/using-unix-mail-mailx-with-a-modern-mail-server-imap-instead-of-mbox-files ) which was supposed to download the Heirloom mailx OSX binary package from http://www.tramm.li/iWiki/HeirloomNotes.html . It has a more functional mailx that can handle the above command syntax.

(Sorry for the bad cross-link or attribution, I'm new to the site.)

0
Aug 05 '15 at 22:47
source share

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" 

After receiving the email attachment, if you used uuencode, you will need uudecode

 uudecode abc.gz.enc 

# This will generate the file as the original with the same name as the second argument to uuencode.

0
Nov 02 '16 at 10:45
source share

I use mailutils , and the confusing part is that to attach the file you need to use the capital parameter A. 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. 
-one
Aug 23 '17 at 9:28
source share



All Articles