How can I send attachments from the command line?

Using mail from the command line: if you want to send true attachments (that show up in graphical mail clients as attachments), you need something like "mutt" or CleanCode Email.

If it's just arbitrary text or a file you want to send, and don't care about it looking like a real attachment, you can use ordinary "mail" ("mailx" on some Unixes) at the command line. However, that's NOT a mime attachment - it's just your file mixed in to the mail message. However, some mail clients will see it and treat it as a real attachment, so this may be "good enough":


mailx -s "This is it" someone@someplace < somefile

or

echo "text
more text" | mailx somebody somebodyelse fred@fred.com

You can use uuencode to send a binary file the same way, and some mail clients may even display that as they would a "real" attachment.

However: that's NOT an attachment. Every time somebody asks this question on the newsgroup, someone is sure to insist that it is, but a simple visual examination of actual messages should show them their error. The confusion is, of course, that some mail clients will act as though the uuencoded text were a proper attachment. That's fine if the people you are sending to use one of those clients, but if there is any chance they do not, be safe: use Mutt.

You can also use mpack to put a file into MIME format.



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> How can I send email attachments from the command line?

12 comments



Increase ad revenue 50-250% with Ezoic


email -s "Attachment" -a coolpic.jpg user@domain.com

You can also redirect text:

email -s "Attachement" -a coolpic.jpg user@domain.com < somefile

You can send an empty email with just the attachment:

email -s "Attachment" -a coolpic.jpg -b user@domain.com

Email can also send to SMTP servers that expect authentication (SMTP AUTH) and has neat features such as an address book and dynamic signature files. Email ALSO works well with GPG/PGP for encryption.


--

Thanks: See http://aplawrence.com/Reviews/cleancodeemail.html

--TonyLawrence


---December 30, 2004

I use "mpack", which I just found in debian repository.

e.g.:
mpack -s "My Subject" /tmp/myfile user@domain.com

Edward van Kuik

---


---December 30, 2004







Mon Apr 16 19:36:06 2007: 2958   weiss1rogerscom


I have been using MUTT to send email with pdf or htm attachments from the command line. Works well as long as you have a proper .mime file setup.

Cheers



Mon May 19 20:46:07 2008: 4218   anonymous


I just found nail which is a command line replacement for mail and supports attachments.



Mon Nov 17 23:13:51 2008: 4782   fractil



I use the following to send files as attachments using the mail command:

uuencode <~/path/filename> <rename file> | mail -s "subject" <recipient>

For example:

uuencode /etc/apache2/apache2.config apache.config | mail -s "Apache Config" jon@doe.com



Mon Nov 17 23:55:15 2008: 4783   TonyLawrence

gravatar
No, that's NOT an attachment. It's just a uuencoded file embedded in your message.

Read the text.



Fri Nov 21 10:06:47 2008: 4791   anonymous


thanks edwards.. mpack worked for me..



Fri Jul 17 11:45:02 2009: 6640   anonymous

gravatar
mutt -s "test mail" -a /tmp/a.txt abc@xyz.com



Sun Mar 28 14:32:03 2010: 8289   Joseph

gravatar


Here's a good solution: SendEmail
An Email Program for Sending SMTP Mail from a Command Line

It's a single perl script, and it doesn't make any [bad] assumptions such as sendmail running and configured properly. It supports attachments.

(link)



Sat May 22 20:33:28 2010: 8628   1base

gravatar


Using nail (mailx) version 12.4 I can no longer
send receipients name with his mail account.
nail breaks the argument like
nail -r sender_name_and_mail-account -s "xyzx aaaa sss" to-addr <text_in_file
where to-addr = "name1 name2 mail-account"
into mail to 3 mail accounts, where two will fail since they do not represent
real mail-account.
It also means the the receiver may believe, that I as a sender dot not know his name.
It works fine on nail version 12.2 but not on 12.4.
Weird
Ejler



Tue Dec 14 19:20:58 2010: 9165   anonymous

gravatar


sudo mutt -s "test" -a /tmp/foo.txt -- user@domain.com






Tue Dec 14 19:31:49 2010: 9166   TonyLawrence

gravatar


Yes, I did forget to include an example of usage, but why on earth would you use sudo???



Tue Dec 14 20:04:57 2010: 9167   anonymous

gravatar


Indeed no need to use sudo !



Tue Feb 1 15:19:13 2011: 9267   anonymous

gravatar


Mailx on RHEL 6 has an option similar to MUTT for adding attachments to outgoing emails.

e.g. echo hello | mailx -s 'testing 1 2 3' -a vg1.tar.gz fred@nowhere

------------------------
Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Linux source code is freely and easily available to all of us. Understanding it is much harder. (Tony Lawrence)





This post tagged:

FAQ