email from bash script - bash

#!/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.

Related

Return to append on previous line after a command in a bash script

I'm using echo to output text and curl to get my ip from a website:
echo -n "IP: "; curl ifconfig.co/x-real-ip; echo -e "\b test"
what I noticed is that when the curl command is executed it automatically changes to a new line, and with the escape \b i thought maybe i could backspace to the previous line and output some more text there. Though that is not working apparently. Is there any way to do that?
Once the line is incremented, echoing a backspace will not help. Try this:
printf "IP: $(curl -s ifconfig.co/x-real-ip) anything-else\n"
This should work:
ip=$( curl -s ifconfig.co/x-real-ip ); echo -n "IP: $ip "; echo "more stuff"
With ip stored in variable, it is possible to avoid printing newline using the -n parameter.

$user or $whoami not working in a bash shell script

I am learning basic unix shell scripting now. I was trying a code from here to write my username but it is not working.
The code is:
#
# Script to print user information who is currently logged in , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0
I tried $whoami instead of $user, but still it is not showing my username. What can be the issue here? I am using vim editor in Ubuntu.
If $USER is not working try, $LOGNAME. If you have already learned about command substitution then you can use $(whoami) or $(id -n -u). Ref
\c in echo wont work unless you specify with -e (stands for enable interpretation of backslash escapes).
echo -e "Today is \c ";date
It seems you want to prevent the trailing new line character introduced by echo. Another way to achieve this is to just add -n. Then you don't need -e and \c.
echo -n "Today is "; date
I tried `$whoami`
What you probably mean to do is `whoami` or $(whoami).
See Command Substitution.

Using sendmail from bash script for multiple recipients

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

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

Resources