What OS/Distro are you using?
Many distros are commonly using BSD mailx
as an alias to the mail
command, but then there might be an alternate or when your script runs, it has a different PATH and therefore uses a different program to the one you expect. (As perhaps your OS has some other ideas for what mail
for users should do.)
Try from your terminal:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Then the same in your script to see if there's something different about the PATHS
BSD mailx
will be in somewhere like /usr/bin/mailx
It looks like it has different options to whatever mail
command you are using, though. (See man mailx
/ man mail
). Are you trying to get:
Subject: Message Subject
X-Priority: 1
Or the subject, then an additional header:
Subject: Message Subject
X-Priority: 1
bash
will strip newlines from variables if you try to use them like that to invoke another command.
If it's mailx
:
- -a Specify additional header fields on the command line such as "X-Loop: foo@bar" etc.
I'd try something like:
#!/bin/bash
echo "Message Body" | /usr/bin/mailx -a "X-Priority: 1" -s "Message Subject" -r [email protected] -c [email protected] -- [email protected]
I don't think there is an (easy) way to get the stock mail/mailx to add attachments, though. It's not a thing it can do.
You could use an alternative like mutt
or neomutt
:
options:
-- Special argument forces NeoMutt to stop option parsing and treat
remaining arguments as addresses even if they start with a dash
-a <file> Attach one or more files to a message (must be the last option)
Add any addresses after the '--' argument
-c <address> Specify a carbon copy (Cc) recipient
-s <subject> Specify a subject (must be enclosed in quotes if it has spaces)
Sounds like it will do more like what you want anyway.
echo "Message Body" | mutt -s "Message Subject" \
-e "my_hdr X-Priority: 1" \
-e "my_hdr From: [email protected]" \
-c [email protected] \
-a /path/to/logFile.log \
-- [email protected]
echo "Message Body" | mutt -s "Message Subject" -e "my_hdr X-Priority: 1" -e "my_hdr From: [email protected]" -c [email protected] -a /path/to/logFile.log -- [email protected]