Search code examples
shellfileemailunixmailx

Send multiple files by email and also add a body message to the email (Unix Korn Shell)


I'm trying to send multiple files by email but also include a body message in the email, I've tried couple of ways with no luck, the following code is for send multiple files:

(uuencode file1.txt file1.txt ; uuencode file2.txt file2.txt) | mailx -s "test" email@test.com

I've tried this option with no luck:

echo "This is the body message" | (uuencode file1.txt file1.txt ; uuencode file2.txt file2.txt) | mailx -s "test" email@test.com

any idea how could be the code?


Solution

  • Try this:

    (echo "This is the body message"; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt) | mailx -s "test" email@test.com
    

    The issue with your command is that you are piping the output of echo into the subshell and it is getting ignored as uuencode isn't reading from stdin.

    You can use { ... } to avoid the subshell:

    { echo "This is the body message"; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt; } | mailx -s "test" email@test.com
    

    If you are doing this in a script and you want it to look more readable, then:

    {
      echo "This is the body message"
      uuencode file1.txt file1.txt
      uuencode file2.txt file2.txt
    } | mailx -s "test" email@test.com