Webux Lab - Blog
Webux Lab Logo

Webux Lab

By Studio Webux

Search

By Tommy Gingras

Last update 2022-04-16

EmailDebianRaspberry piAWS

Configure Lite Debian image on a Raspberry Pi to send email through AWS SES

Prerequisites

Use raspi-config command to setup the basics and update your pi to latest version

Installation

sudo apt-get update
sudo apt-get install bsd-mailx netcat -y

Configuration

Exim4

Configure Exim basics

sudo dpkg-reconfigure exim4-config

Answers:

Replace everything using your information.

1. mail sent by smarthost; received via SMTP or fetchmail
2. mail.studiowebux.com
3. 127.0.0.1 ; ::1
4. mail.studiowebux.com
5. Empty
6. email-smtp.ca-central-1.amazonaws.com::587
7. Yes
8. studiowebux.com
9. No
10. mbox format in /var/mail/
11. No
12. [email protected]

Authentication

sudo nano /etc/exim4/passwd.client

Fill the Username and Password based on the information provided by AWS SES.

# password file used when the local exim is authenticating to a remote
# host as a client.
#
# see exim4_passwd_client(5) for more documentation
#
# Example:
### target.mail.server.example:login:password

*.amazonaws.com:AWS_SMTP_USERNAME:AWS_SMTP_PASSWORD

Configure SES with Exim

sudo cp /etc/exim4/exim4.conf.template /etc/exim.conf.local
sudo nano /etc/exim.conf.local
...

begin routers

send_via_ses:
driver = manualroute
domains = ! +local_domains
transport = ses_smtp
route_list = * email-smtp.ca-central-1.amazonaws.com;

...

begin transports

ses_smtp:
driver = smtp
port = 587
hosts_require_auth = *
hosts_require_tls = *

...

begin authenticators

ses_login:
driver = plaintext
public_name = LOGIN
client_send = : AWS_SMTP_USERNAME : AWS_SMTP_PASSWORD

Apply changes

sudo /etc/init.d/exim4 restart

Tests

echo 'Test the AWS SES Configuration' | mailx -v -s 'Welcome !' -r "$HOSTNAME<[email protected]>" [email protected]
echo 'Test the AWS SES Configuration' | mailx -v -s 'Welcome !' -r "$HOSTNAME<[email protected]>" [email protected]

The goal of all this

I want to monitor the Raspberry Pi ip addresses.

Here 2 bash scripts to help me with that:

Don't forget to update the values to fit your environment

cat <<EOF > /usr/bin/retrieve-ip.sh
#!/bin/sh

echo "retrieve-ip.sh started" > /tmp/ip_debug.log
ifconfig | tee -a /tmp/ip_debug.log

until [ \$(netcat -z -w 5 google.com 443 && echo 1 || echo 0) -eq 1 ]; do
  echo "Waiting for internet connection..." | tee -a /tmp/ip_debug.log
  sleep 2
done

echo "Internet Connection up and running." | tee -a /tmp/ip_debug.log
echo "Waiting 30 seconds..." | tee -a /tmp/ip_debug.log
sleep 30

ifconfig | tee -a /tmp/ip_debug.log

echo "" > /tmp/ip.log
host myip.opendns.com resolver1.opendns.com >> /tmp/ip.log
ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' >> /tmp/ip.log
uptime >> /tmp/ip.log

cat /tmp/ip.log | mailx -v -s 'Raspberry PI has booted !' -r "\$HOSTNAME<[email protected]>" [email protected] [email protected] | tee -a /tmp/ip_debug.log

currentIp=\$(host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has address" | cut -d' ' -f4) 
echo \$currentIp > /tmp/last_public_ip.log

sleep 10
EOF

cat <<EOF > /etc/systemd/system/wl-ip.service
[Unit]
Description=Send Network Information by mail
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/retrieve-ip.sh
User=tgingras
RemainAfterExit=no

[Install]
WantedBy=multi-user.target
EOF

ln -s /etc/systemd/system/wl-ip.service /usr/lib/systemd/system/wl-ip.service
chmod 644 /etc/systemd/system/wl-ip.service
sudo systemctl enable wl-ip.service

cat <<EOF > /usr/bin/check-ip.sh
#!/bin/sh

currentIp=\$(host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has address" | cut -d' ' -f4) 

savedIp=\$(cat /tmp/last_public_ip.log)

echo "[check-ip.sh] Current Public IP: '\$currentIp' vs. '\$savedIp'" | tee /dev/kmsg
if [ "\$currentIp" != "\$savedIp" ]; then
    echo \$currentIp > /tmp/last_public_ip.log
    echo "Public IP has changed: \${savedIp} -> \${currentIp}" | mailx -v -s 'Raspberry PI public Ip has changed !' -r "\$HOSTNAME<[email protected]>" [email protected] [email protected]
    echo "[check-ip.sh] Notification sent !" | tee /dev/kmsg
fi
EOF

chmod +x /usr/bin/retrieve-ip.sh
chmod +x /usr/bin/check-ip.sh

crontab -e
0 * * * * /usr/bin/check-ip.sh

At reboot the pi will send by email the information and every hour it will check if the public IP has changed, if so it will send the new IP by mail.

Sources