This question already has answers here:
How can I send an email through the UNIX mailx command?
(10 answers)
Closed 3 years ago.
What's the best way to send mail from the shell with one line of code?
mailx -s "subject" foo#bar.com
Enters into line by line entry of the body and the message is only sent with termination of Ctrl+D or '.'
Since I am accessing the system command through another program, I'd like to be able to do something like:
mailx -s "subject" foo#bar.com \n Body Text \n .
in one go.
Thank you!
mailx -s "subject" foo#bar.com <<<$'\n Body Text \n'
Thank you to both Alfe and Joachim PileBorg, I now have two solutions! The one Alfe provided works, as does:
echo "body" | mailx -s "subject" foo#bar.com
which, though my Unix is rusty, I imagine does basically the same thing.
Related
This question already has answers here:
How does "cat << EOF" work in bash?
(12 answers)
Closed 6 years ago.
I'm new to Bash scripting. I came across this link and noticed an unusual syntax for cat.
cat << !
HOPE THIS WORKS
This sample E-mail message demonstrates how one can attach
files when sending messages with the Unix sendmail utility.
!
uuencode ${file_1} ${file_1}
uuencode ${file_2} ${file_2}
uuencode ${file_3} ${file_3}
!
What does the << mean? What does the ! mean? How come the cat has an opening and closing !, but uuencode doesn't?
EDIT: Thanks for all the help! The last outstanding question I have is why is there no opening and marker for the uuencode section. From what I'm understanding, cat has a << ! indicating it's a HEREDOC. uuencode however doesn't seem like a HEREDOC. What gives?
Its called heredoc, and its constructed like this:
command << MARKER
Literal text here, can contain $variables
and newlines,
leading spaces
and tabs...
MARKER
And works with everything between the MARKER is going as stdin to the specified command. What marker you use is optional and in your example ! is being used. And if you feed the command cat with input on stdin it will simply print it to stdout:
cat << !
all this text will go to stdout
!
This question already has answers here:
Setting an argument with bash [duplicate]
(2 answers)
Closed 7 years ago.
I'm getting stuck on something that surely has a simple solution. I'm using mail from GNU mailutils to periodically send a log file to relevant people.
mail -s "The Subject" someone#example.com <<< "Message body"
Now I want to incorporate that in a script that performs a bunch of tasks, but I'm getting stuck with the expansion and the quotes.
This is what I'm trying:
subject="The Subject"
sender=From:Sender\<someone#example.com\>
recipient_list=me#example.com,another#example.com,yetanother#example.com
report="mail -s "$subject" -a $sender $recipient_list"
...
$report <<< "A log message from a section of script"
...
$report <<< "A different log message"
The trouble is with $subject.
Calling mail -s "$subject" -a $sender $recipient_list <<< "My Message" works nicely, but wrapping it into a variable as I've done above ends up sending the email with only the first word of "The Subject" getting sent, not the whole string.
I'm going to go the proper function route, but now I'm curious.
I'm sure some escape/expansion trick will do it, but I have not been able to find the correct one (or reason it out).
Thanks!
The answer, of course, is use an array.
report=(mail -s "$subject" -a "$sender" "$recipient_list")
...
"${report[#]}" <<< "A log message from a section of script"
I'm running a bash script in cron to send mail to multiple recipients when a certain condition is met.
I've coded the variables like this:
subject="Subject"
from="user#example.com"
recipients="user1#mail.example user2#mail.example"
mail="subject:$subject\nfrom:$from\nExample Message"
And the actual sending:
echo -e $mail | /usr/sbin/sendmail "$recipients"
The problem is that only user2#mail.example is receiving the email. How can I change this so all the recipients receive the email?
NOTE: The solution has to be with sendmail, I'm using jailshell and it seems to be the only available method
Try doing this:
recipients="user1#mail.example,user2#mail.example,user3#mail.example"
And another approach, using shell here-doc:
/usr/sbin/sendmail "$recipients" <<EOF
subject:$subject
from:$from
Example Message
EOF
Be sure to separate the headers from the body with a blank line as per RFC 822.
Use option -t for sendmail.
in your case - echo -e $mail | /usr/sbin/sendmail -t
and add your recipient list to message itself like To: someone#example.com someother#nowhere.example right after the line From:.....
-t option means -
Read message for recipients. To:, Cc:, and Bcc: lines will be scanned for recipient addresses. The Bcc: line will be deleted before transmission.
to use sendmail from the shell script
subject="mail subject"
body="Hello World"
from="me#example.com"
to="recipient1#example.com,recipient2#example.com"
echo -e "Subject:${subject}\n${body}" | sendmail -f "${from}" -t "${to}"
For postfix sendmail, I am adding one line command useful for scripting
I had problem adding recipients in default position in the end of sendmail command in RHEL (Undisclosed Recipients) and piping echo command saved the day.
Option -f found from http://www.postfix.org/sendmail.1.html
Please note that syntax in echo is important, try echo to a file to check before attempting with sendmail.
echo -e "To:receiver1#domain1, receiver2#domain2 \nSubject:Subject of email \n\nBody of email.\n" | /usr/sbin/sendmail -f sender#domain -F sendername -it
I had prepared a cc maillist as below:
/appl/tracker/TEST> more abc.maillist
xyz#yahoo.com, a123#gmail.com, abc#xyz.com
And a to maillist as below:
/appl/tracker/TEST> more Servicedesk.maillist
123#gmail.com
My script is ab.sh which will call the mailx command and send an email. This should send email to cc_list keeping the id's in cc and to_list placing the id provided in the list in to list.
/appl/tracker/TEST> more ab.sh
#!/bin/ksh
l_date=`date +%d%m%y`
CC_LIST=`cat /appl/tracker/TEST/abc.maillist`
TO_LIST=`cat /appl/tracker/TEST/Servicedesk.maillist`
MY_Q="'"
cc_list="$MY_Q$CC_LIST$MY_Q"
echo $cc_list
BODYFILE='Please find attached file having my details.Test mail'
echo $CC_LIST
echo $TO_LIST
mailx -s 'HI' -c $cc_list $TO_LIST <<-EOF
`echo $BODYFILE`
EOF
/appl/tracker/TEST>
Output:
There is an error being occured stating that there is an unbalanced ".
Can anyone please help me getting the solution for this.
I don't get that error message. Are you sure you have pasted everything correctly?
Anyway, an immediate problem is that you need to quote any variable interpolations. It's not clear why you need variables for this at all, besides. Here is a much simplified refactoring of your script.
#!/bin/sh
CC_LIST=`cat /appl/tracker/TEST/abc.maillist`
TO_LIST=`cat /appl/tracker/TEST/Servicedesk.maillist`
BODYFILE='Please find attached file having my details. Test mail'
echo "$CC_LIST"
echo "$TO_LIST"
echo "$BODYFILE" | mailx -s 'HI' -c "$CC_LIST" "$TO_LIST"
Variables must be double quoted to avoid expanding as list:
mailx -s 'HI' -c "$cc_list" "$TO_LIST" <<-EOF
$BODYFILE
EOF
I'm finding that a call to the mail command is causing a script to suspend without error. To close the script I have to ctrl-c or issue a kill command on the process id.
The pertinent section of the script is below:
EMAIL_TO="my#email.com"
if [ -f /www/archives/pdf/pdf_201207021048.tar ]; then
echo "file exists"
else
echo "file does not exist"
fi
echo "sending mail next..."
mail -s "pdfbackup" "$EMAIL_TO"
echo "mail sent?"
When running this, I'm seeing the text "sending mail next..." and nothing more. It never returns to prompt.
I can see the script is still in memory with ps -ax | grep myscript.sh.
I've tried using quotes around the subject and email, and again without. The same result is produced either way.
What am I doing wrong?
From man mail:
Sending Mail
To send a message to one or more people, mail can be invoked with arguments which are the names of people to whom the mail will be sent. You are then expected to type in your message, followed by a at the beginning of a line. The section below Replying To or Originating Mail, describes some features of mail available to help you compose your letter.
It's expecting a body of the message. Use <C-d> for newlines and end the message with a blank line and <C-d>. Alternatively you can supply the body and pipe to the command...
echo "This is the body" | mail -s "Subject" "recipient#example.com"
or
mail -s "Subject" "recipient#example.com" <<< "This is the body"
Or your can read the body from a file..
mail -s "Subject" "recipient#example.com" < body_in_file.txt