35

Is it possible to email attachments from the command line?

If possible, I'd like something as simple as:

mail -a myfile.txt -t me@example.com -s "Here's my file"
conorgriffin
  • 1,007

7 Answers7

19

Of all the mail user agents in the Ubuntu repository, it appears that mutt is the command-line MUA that is blessed with Long Term Support.

According to the manual, you can do something exactly like:

mutt -a myfile.txt -s "Here's my file" -- me@example.com

except it won't go anywhere since one also needs a Mail Transfer Agent. Popular ones are:

  • the venerable sendmail
  • postfix
  • exim4
  • qmail
  • nullmailer

and the only ones that Canonical seems to support are postfix (thanks for the correction Steve) and exim4.

One could also say that xdg-email is also a proper Ubuntu MUA, but it is a bare-bones front end which only executes your preferred MUA on your behalf.

If you'd like advice on which MTA might be suitable for your use, perhaps open another question here.

msw
  • 4,696
14

I had bad trouble with sending attachment files, too. When I sent an email without attachment, it was successful but was not with attachment. This problem was existent with sendemail, mutt, mail, mailx , uuencode commands.

Fortunately, It was solved funnily. I use Gmail for sending email. You can configure your gmail for sending emails via commands in terminal as declared at http://www.linuxandlife.com/2013/01/send-email-from-linux-terminal.html .

You can send a text email using:

mail -s "hello" RECEIVE@mail.com < /home/masoud/YOURFILE.txt 

but you can't send same file as attachment as below:

mail -s "hello" RECEIVE@mail.com -a /home/masoud/YOURFILE.txt

or:

mail -s "hello" -a /home/masoud/YOURFILE.txt RECEIVE@mail.com

Finally, I understood that only this format could send an attachment:

echo "your message here" | mail -s "title" -a /home/masoud/YOURFILE.txt RECEIVE@mail.com

Funnily, the difference is existence of the "echo" command.

Update: 201808

Seems that the -a option has been changed with Uppercase -A for attachment now. The -a seems to be for changing header according to doc

Masoud
  • 141
6

I found that the command and parameters have been changed recently.

If your want to send your attachments with this command:

mail -s "your subject" you@gmail.com -A /your/attachment/absolute/path < /home/you/message.txt

The option attachment should be -A which is CAPITAL A.

Alex
  • 61
3

You may want to send a file from the shell, but otherwise use Thunderbird.

In this case, try thunderbird -remote ... is useful - assuming thunderbird is usually running:

The command opens a mail compose window of a running thunderbird instance.
The "From" address is your default address configured in thunderbird.
Also, the existing account settings are used, there is no separate setup needed.

For a mail adressed to you@example.com, with subject "S", body "B", and an attachment /some/absolute/file.txt, the command is

thunderbird -remote "xfeDoCommand(composeMessage,subject='S',to='you@example.com',body='B',attachment='/some/absolute/file.txt')"

There are two problems:

The attached file needs to be given by an absolute path, which is tedious in practice. That can be handeled by using readlink -f to resolve relative paths:

thunderbird -remote "xfeDoCommand(composeMessage,subject='S',to='you@example.com',body='B',attachment='$(readlink -f file.txt)')"

Also, the command is to long. Use a shell script or shell function, with four arguments:

thunderbird-compose () {
    thunderbird -remote "xfeDoCommand(composeMessage,subject='$1',to='$2',body='$3',attachment='$(readlink -f $4)')"
}

With this function, the command becomes readable:

thunderbird-compose 'Some Subject' test@example.com 'Body of message' file1

will open a thunderbird "Write" window with the attachment, and From, To, Subject, and body text filled in. It can be edited before sending it.

Volker Siegel
  • 13,295
2

heirloom-mailx package in debian provides mailx command to send mime attachments easily. following works for me;

 mailx -a attachment.zip -s subject rctp@domain.to
0

My version of mail, BSD mail, does not seem to support attachments (it's not mentioned anywhere in the manpage).

A hacky workaround is to send the attachment as uuencoded stream. Gmail will interpret this as two attachments, one called "noname" which is the uuencoded text and the other called "-" which carries the decoded version that can be opened by any program that recognizes the file type by its binary data. I don't know if this behavior is documented or applies for any other mail client, but if you don't want to install anything this is a quick hack.

uuencode image.jpg - |  mail -s "Subject line" user@example.net 
qwr
  • 2,969
0

I wish to add another answer which is used to add body text along with the attachment. Cheers!!

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com
Anandu M Das
  • 2,303