Using sendmail from bash script for multiple recipients - bash

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

Related

Sending mail with html file output as body

I'm receiving file hc.html as an attachment. But I wanted the output of hc.html as Body in Lotus Notes 8. I was using sendmail earlier and it was working fine for Outlook. In this new environment I cannot use sendmail. I can only use mailx.
Can anything can be done for this?
(cat body.txt; uuencode $OP $OP) | mailx -s 'Subject' xxx#yy.com
OP=/tmp/hc.html
Try this:
mailx -s "subject" emailaddress < $OP

bash mail: command output in subject

I try send a mail with mail command, with the output of other command in the subject
subj="hello from $(hostname -s) $(date)"
echo "data" | mail -s $subj mail#mail
but I only get the first part of subject (hello from).
why?
You need to quote your subject, like this:
echo "data" | mail -s "$subj" mail#mail
If you don't quote it, the mail program will not know where your subject ends and will take the first "word" (hello) as the subject and everything else as an address.
In general, it is good practice to always quote your variables.
/youScriptOrOutput.sh | mail -s "Subject from host $(hostname -s) $(date)" my#email.com

How to cc a maillist in mailx command

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

Shell script hangs on mail command

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

email from bash script

#!/bin/bash
MESSAGE="Line one. /n"
MESSAGE="$MESSAGE Line two. /n"
MESSAGE="$MESSAGE Line three."
echo $MESSAGE | mail -s "test" "example#example.com"
Is that how I should get each line, on its own line?
Use a heredoc.
mail -s "test" "example#example.com" << END_MAIL
Line one.
Line two.
Line three.
END_MAIL
Change:
echo $MESSAGE | mail -s "test" "example#example.com"
To:
echo -e $MESSAGE | mail -s "test" "example#example.com"
The heredoc advice is good, plus you might want to consider using mailx for which there exists a Posix standard or perhaps sendmail which will exist if the mailer is either sendmail or postfix. (I'm not sure about qmail.)
Unless using sendmail, it's also a good idea to set the MAILRC variable to /dev/null to bypass the user's configuration script, if any.

Resources