(Note: I have seen this question, but it does not work for me)

I am attempting to send an email with an attachment using Bash on Debian Stretch, using the mailx package (not mutt). My implementation does not support the -A parameter (it's an invalid option), and the -a parameter is for adding headers.

I have tried many variations of the following, but they fail for me:

mail -s "Test" -a /home/user/filename.xlsx user@example.com < /root/emailbody.txt

The end effect is a plain-text email with the filename as the first line, the header content, then the data inside /root/emailbody.txt:

MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit

Hi there

I do not have uuencode, and many threads are reporting that's the "old" way of sending attachments. I cannot install mutt on this server. What are my options?

Update with uuencode - I tried running it as the following, but only got "Hi there" as the email result, no attachment:

uuencode /home/user/filename.xlsx test.tlsx | mail -s "Test" user@example.com < /root/emailbody.txt
  • Are you against installing and using uuencode?
    – Jeff Schaller
    Jun 14 '19 at 17:44
  • @JeffSchaller Tried that, then ran it but it just showed the /root/emailbody.txt file in the body, and no attachment. I'll update the question. Jun 14 '19 at 18:00
  • You wrote that you didn't have it, so I wasn't sure if an answer with it would be acceptable ...?
    – Jeff Schaller
    Jun 14 '19 at 18:38
  • @JeffSchaller I didn't want extra packages if I could help it, and I saw threads that uuencode is the old way. That's the main reason I wanted to avoid it. Jun 14 '19 at 19:06

Your original command will work if you have the heirloom-mailx package installed.

sudo apt-get install heirloom-mailx

Then you can:

mailx -s "Test" -a /home/user/filename.xlsx user@example.com < /root/emailbody.txt

To "attach" a uuencoded file really just means providing that as the body of the email; as a result, you cannot also redirect a body in from a file; you'd use:

uuencode /home/user/filename.xlsx filename.xlsx | mail -s "Test" user@example.com
  • So I tried this, but everytime the attachment comes through, Excel reports it's corrupted. New question? Jun 14 '19 at 19:44
  • Possibly; the encoding/decoding should be fine, but any number of things could go wrong along the way.
    – Jeff Schaller
    Jun 14 '19 at 19:47
  • Dang... It opens, but has that warning dialog. I'll need to look into it. Thanks for your help! Jun 14 '19 at 20:01
  • Sure! If you can, for the new question, describe the steps that the email (well, the attachment) takes on its way to Excel. You might consider SuperUser as a more appropriate site, if the issue ends up more on the Windows side.
    – Jeff Schaller
    Jun 14 '19 at 20:02
  • I posted the question here Jun 14 '19 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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