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 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:

/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" user@example.com < /root/emailbody.txt

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 user@example.com < /root/emailbody.txt
HackSlash
  • 471
  • 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" user@example.com
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255