---
Step 1: Install and Configure msmtp
Install `msmtp` to handle email sending:
Code: Select all
apt update
apt install msmtp -y
Code: Select all
nano /etc/msmtprc
Code: Select all
defaults
auth on
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
account default
host mail.yourdomain.com
port 465
from [[email protected]](mailto:[email protected])
user [[email protected]](mailto:[email protected])
password YourSecurePassword
Code: Select all
chmod 600 /etc/msmtprc
chown root:root /etc/msmtprc
Code: Select all
echo -e "Subject: TestnnThis is a test email." | msmtp [[email protected]](mailto:[email protected])
---
Step 2: Create the Boot Notification Script
Create the script:
Code: Select all
nano /usr/local/bin/boot-notify.sh
Code: Select all
#!/bin/bash
sleep 10
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
HOSTNAME=$(hostname)
IP_ADDRESS=$(curl -4 -s ifconfig.me)
MESSAGE_ID="boot-notify-$(date +%s)@$HOSTNAME"
DATE=$(date -R)
echo "[$TIMESTAMP] Executing boot notification script" >> /var/log/boot-notify.log
[ -z "$IP_ADDRESS" ] && {
IP_ADDRESS="Not available"
echo "[$TIMESTAMP] Warning: Could not retrieve public IP." >> /var/log/boot-notify.log
}
cat << EOF > /tmp/boot-notify-message.txt
Subject: $HOSTNAME Booted Successfully
From: Server Notification [[email protected]](mailto:[email protected])
Reply-To: [[email protected]](mailto:[email protected])
Date: $DATE
Message-ID: <$MESSAGE_ID>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html>
<html><body style="font-family: Arial; background: #f4f4f9; padding: 20px;">
<div style="max-width: 600px; margin: auto; background: white; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
<div style="background: #4CAF50; color: white; padding: 15px; text-align: center;">
<h2>Server Boot Notification</h2>
</div>
<div style="padding: 20px;">
<p><strong>Host:</strong> $HOSTNAME ($IP_ADDRESS)</p>
<p><strong>Status:</strong> Successfully booted</p>
<p><strong>Time:</strong> $TIMESTAMP</p>
</div>
<div style="background: #f4f4f9; padding: 10px; text-align: center; font-size: 12px; color: #777;">
This is an automated notification from your server.
</div>
</div>
</body></html>
EOF
msmtp [[email protected]](mailto:[email protected]) < /tmp/boot-notify-message.txt >> /var/log/boot-notify.log 2>&1
STATUS=$?
[ $STATUS -eq 0 ] && echo "[$TIMESTAMP] Email sent." >> /var/log/boot-notify.log || echo "[$TIMESTAMP] Email failed. Status: $STATUS" >> /var/log/boot-notify.log
rm -f /tmp/boot-notify-message.txt
exit 0
Code: Select all
chmod +x /usr/local/bin/boot-notify.sh
Step 3: Create the systemd Service (Debian 12 Fix Applied)
Create service file:
Code: Select all
nano /etc/systemd/system/boot-notify.service
Code: Select all
[Unit]
Description=Send boot email notification
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/boot-notify.sh
[Install]
WantedBy=multi-user.target
Code: Select all
systemctl daemon-reexec
systemctl enable boot-notify.service
Step 4: Test It
Reboot:
Code: Select all
reboot
Code: Select all
cat /var/log/boot-notify.log
Let me know if it fails and I’ll help debug
