The below line of code/command is working as expected if I run it from Unix terminal, but not if I include it in a script and run the script - exact same.
I am expecting the X-Priority to show the mail as high priority in Outlook. If I run it from command line, I see the email is showing correctly as high priority and I see the file is attached and the body appear as supposed to. However, if I write it a shell script and run the script, the mail come formatted as text and attachment is appended to the body of the email and the email is shown as normal in outlook.
Not sure what I am doing wrong. Any help is appreciated.
Shell Script
#!/bin/sh
echo "Message Body" | mail -r "sender@mycom.com" -c "ccrecipient@mycom.com" -s "$(echo -e "Message Subject\nX-Priority: 1")" -a "logFile.log" "torecipient@mycom.com"
Also tested with Shell Script
#!/bin/bash
echo "Message Body" | /usr/bin/mail -r "sender@mycom.com" -c "ccrecipient@mycom.com" -s "$(echo -e "Message Subject\nX-Priority: 1")" -a "logFile.log" "torecipient@mycom.com"
Command line
echo "Message Body" | mail -r "sender@mycom.com" -c "ccrecipient@mycom.com" -s "$(echo -e "Message Subject\nX-Priority: 1")" -a "logFile.log" "torecipient@mycom.com"
EDIT: More Details/Observation on further R&D:
In Subject, in my actual command, there is a variable assignment like MyApp : ${HostName} : Restarted\nX-Priority: 1
.
HostName
is a variable i defined in the script.
If I replace the variable hard-coded value like MyApp : myserver : Restarted\nX-Priority: 1
, the command line and script behave the exact same.
If I keep the variable in command line, it works as expected (the X-Priority part) but the subject comes as MyApp : : Restarted
as expected as the variable is not defined.
I tried to add an undefined variable in script and it still did not work.
Hope this gives more insights into the issue.
Note: I originally asked this question in stackoverflow. But I was advised to ask it here instead - https://stackoverflow.com/questions/75585298/mail-not-working-in-script-same-way-it-is-working-from-command-line
--