Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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"
share|improve this question
    
The default ubuntu command line mail user-agent mail (aka mailx) is MIME ignorant therefore cannot handle attachments in any way that isn't 1980ish. This is a good question, I'm looking for the canonical Canonical MUA; more to come. –  msw Oct 10 '10 at 1:17
add comment

2 Answers

up vote 9 down vote accepted

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.

share|improve this answer
    
Great answer, thanks –  conorgriffin Oct 10 '10 at 2:39
    
Great question, thanks. You prompted me to get mail working on my box. I used nullmailer MTA to blindly relay to my ISP, and the example mutt command worked as advertised. Incidentally nullmailer has a fake sendmail front-end so that most programs that expect to talk to sendmail get shuffled off to a real MTA with zero effort from me. –  msw Oct 10 '10 at 3:13
    
msw, I'm not quite sure why you're claiming exim4 is the only one canonical supports, postfix is also in main and is what is enabled if you select the Mail Server task selection from the ubuntu-server install. I believe exim4is the debian default MTA, and in the past, packaging preferences may have caused it to be pulled in by default, but for a while now, postfix is definitely the preferred MTA in Ubuntu. –  Steve Beattie Nov 5 '10 at 6:57
    
Oh, and also note that if you went with bsd mail/mailx as your (non-mime aware) MUA, you would still need an MTA of some kind to actually send the mail. But mutt's better for you anyway. :-) –  Steve Beattie Nov 5 '10 at 6:59
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.