Here is an example of the script that has been used in the APCUPSD addon for emailing when the UPS goes on to battery support
#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when the UPS
# goes on batteries.
# We send an email message to root to notify him.
#
FROM="enter from email address"
TO="enter to email address"
HOSTNAME=`hostname`
MSG="$HOSTNAME Power Failure on UPS $1"
#
(
echo From: $FROM
echo To: $TO
echo Subject: $MSG
echo " "
echo "$MSG"
echo " "
echo -ne $(echo "\r\n"; while read line; do echo "$line\r\n"; done < <(echo "$(/sbin/apcaccess status)"))
) | sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0
This might help you to use it in your script.
You could also look at how the DA package has been used for the Suricata alert mailing that was added in CU198 which has been in Testing and will be released shortly.
This is not a correctly formatted email here. You will need some MIME headers, too. DMA will try to deliver it, but I don’t think you will have a good chance to get it through all sorts of filters.
I will have another look at it when I can get some time then. However it is working currently. Every time the status of my UPS changes I get the appropriate email. Maybe I have just been lucky.
Thanks for the replies! I modified the sample script for my purposes and unsurprisingly the mail was rejected by the SMTP server at my email provider, because there’s no authentication:
Oct 28 18:52:04 ipfire dma[100012]: new mail from user=nobody uid=99 envelope_from=<VALID_EMAIL_REDACTED>
Oct 28 18:52:04 ipfire dma[100012]: mail to=<VALID_EMAIL_REDACTED> queued as 100012.144fd5d0
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: <VALID_EMAIL_REDACTED> trying delivery
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: using smarthost (mail.gandi.net:587)
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: trying remote delivery to mail.gandi.net [2001:4b98:e00::9] pref 0
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: connect to mail.gandi.net [2001:4b98:e00::9] failed: Cannot assign requested address
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: trying remote delivery to mail.gandi.net [217.70.178.9] pref 0
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: Server greeting successfully completed
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: Server supports STARTTLS
Oct 28 18:52:04 ipfire dma[100012.144fd5d0]: Server supports LOGIN authentication
Oct 28 18:52:05 ipfire dma[100012.144fd5d0]: SSL initialization successful
Oct 28 18:52:05 ipfire dma[100012.144fd5d0]: Server greeting successfully completed
Oct 28 18:52:05 ipfire dma[100012.144fd5d0]: Server does not support STARTTLS
Oct 28 18:52:05 ipfire dma[100012.144fd5d0]: Server supports LOGIN authentication
Oct 28 18:52:05 ipfire dma[100012.144fd5d0]: using SMTP authentication for user VALID_EMAIL_REDACTED
Oct 28 18:52:07 ipfire dma[100012.144fd5d0]: remote delivery failed: Authentication failed: 535 5.7.8 Error: authentication failed: (reason unavailable)
Oct 28 18:52:07 ipfire dma[100012.144fd5d0]: remote delivery failed: SMTP login failed: Success
Oct 28 18:52:07 ipfire dma[100012.144fd5d0]: delivery failed, bouncing as 100470
I’m not aware of any means to provide auth credentials to sendmail when used in this way.