Home > Net >  Using sendmail do not show recipient address
Using sendmail do not show recipient address

Time:04-14

I am trying to check as server status in the Linux and thus want to send an e-mail if it's not run which is pretty simple to do, while I'm creating an HTML body to send the e-mail over outlook while setting it up in the cron.

Apparently it works all as desired, but the only problem is I could not see To and CC mail address while checking on the outlook.

Below as a small snippet.

STATUS="$(systemctl is-active smb.service)"
if [ "${STATUS}" = "active" ]; then
    echo "SAMBA Server is running fine"
else
   export MAILTO="[email protected]"
   export CC="[email protected]"
   export SUBJECT="Critical:: SAMBA Service Status $(hostname)"
   export FROM="[email protected]"
   export EMAIL_TEMPLATE="""\
          <html>
            <head>
            <style>
            table, th, td {{font-size:9pt; border:1px solid black; border-collapse:collapse; text-align:left; background-color:LightGray;}} th, td {{padding: 5px;}}
            </style>
            </head>
            <body>
              Dear Team,<br><br>
              SAMBA Service is not running on <strong>$(hostname)</strong> which is Critical for business hence look into it and Fix it Urgently!<br><br>
              You may check the status of the service like: <strong>systemctl status smb.service</strong> , Please take further steps as required!
              <br><br>
              <br><br>
              Kind regards.<br>
              Support Mailman.
            </body>
          </html>"""
   (
    echo "Subject: $SUBJECT"
    echo "From: $FROM"
    echo "MIME-Version: 1.0"
    echo "Content-Type: text/html"
    echo "Content-Disposition: inline"
    echo '<HTML><BODY><PRE>'
    echo $EMAIL_TEMPLATE
    echo '</PRE></BODY></HTML>'
   ) | /usr/sbin/sendmail $MAILTO $CC

    exit 1
fi

Result:

enter image description here

As you see, it does not exhibit the To mail address, Please let me know if you have any hint.

CodePudding user response:

You need echo the To: and CC: headers, just like everything else. Addresses that are on the command line but not in the header are blind copies.

   (
    echo "To: $MAILTO"
    echo "Cc: $CC"
    echo "Subject: $SUBJECT"
    echo "From: $FROM"
    echo "MIME-Version: 1.0"
    echo "Content-Type: text/html"
    echo "Content-Disposition: inline"
    echo '<HTML><BODY><PRE>'
    echo $EMAIL_TEMPLATE
    echo '</PRE></BODY></HTML>'
   ) | /usr/sbin/sendmail "$MAILTO" "$CC"

And if you don't want to have to repeat them as arguments to sendmail, you can use the -t option. That tells it to get the recipient addresses from the headers.

  • Related