3

(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 [email protected] < /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:

/home/user/filename.xlsx
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" [email protected] < /root/emailbody.txt
Canadian Luke
  • 1,056
  • 2
  • 12
  • 26
  • Are you against installing and using [uuencode](https://unix.stackexchange.com/q/102092/117549)? – 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. – Canadian Luke 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. – Canadian Luke Jun 14 '19 at 19:06

2 Answers2

4

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 [email protected] < /root/emailbody.txt
HackSlash
  • 461
  • 3
  • 10
  • If I replace `mailx` with `heirloom-mailx`, it works! – Canadian Luke Jun 14 '19 at 21:15
  • 1
    FYI for any Ubuntu users who stumble upon this thread.... you should use `s-nail` instead, as `heirloom-mailx` was deprecated in Ubuntu as of 18.04 http://manpages.ubuntu.com/manpages/xenial/en/man1/s-nail.1.html – MrPotatoHead Jan 10 '21 at 20:52
2

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" [email protected]
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
  • So I tried this, but everytime the attachment comes through, Excel reports it's corrupted. New question? – Canadian Luke 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! – Canadian Luke 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](https://superuser.com/questions/1448907/emailed-excel-workbook-appears-broken-after-being-attached) – Canadian Luke Jun 14 '19 at 20:08