For those of us having a server at home with a basic home internet connection, the issue is how to easily bypass the problem of having port 25 blocked by our ISP. To quickly go around it you can setup an msmtp client to login to your personal email to send useful information from your running server. In this case is sending the public IP address just in case the IP address gets renewed so you can promptly update your DNS records for example if no automatic alternative is present. As far as I know wordpress DNS configuration does not have a way to be updated by an API so there you go…
Manual Configuration
For manual ip/dns public ip configuration and update, set up a msmtp client in your server and configure it using your external email provider credentials. This is in the scenario where you are unable to auto update your ip in your dns records. We’ll use msmtp-mta to setup an email event reminder so you can always be aware of any changes to act accordingly.
sudo apt-get install msmtp-mta
Create or edit the config file for msmtp. msmtp will look for a file named .msmtprc in your home folder by default (.file means it is an occult file and wont be displayed with ls unless specified). This file must contain the instructions and your personal credentials so that msmtp can use them to send your ip address to your own email securely with tls. This file creates an “account” readable by msmtp to be used to send email. In thiis example we will be storing the password in plain text inside the configuration file so it must be in a secure account only accessible to the owner of the external email service used to send emails. TLS will be enabled.
nano /home/user/.msmtprc
Edit with the following template:
account admin
host #EMAIL_DOMAIN E.g. google.com
port 587 # verify port functionality. Port 25 is usually blocked by your ISP.
auth on
user admin@google.com #Try with username@yourmailserver.com first
password TYPE_YOUR_EMAIL_PASSWORD_FOR_USER_ADMIN
from admin@google.com
tls on
tls_starttls on
tls_fingerprint #Get the SHA256 fingerprint from your email provider
tls_trust_file /etc/ssl/certs/ca-certificates.crt #verify this location on your system.
Be aware that if the information is not correctly typed, msmtp might give you misleading error codes which will make troubleshooting very hard. Check the correct details of our email provider before. An example of this is the real root server of your email provider might not be the .net or .com domain extension but rather a .us or pl or any other extemsion if they have the root email services in a foreign country. Next, create and configure a script to be used by msmtp in your home folder. Install curl if is not installed already.
If problems arise with permissions, make sure to set permissions 600 for your user to the .msmtp file.
sudo apt-get install curl
sudo nano /home/user/.msmtprc_email_new_pub_ip.sh
The following script will ask your public ip to ifconfig.me using the curl command and be stored in the variable IP_ADDRESS. This value will be in the subject of your email and passed to msmtp. msmtp will load the account details and send the email wherever you want to receive it. To make further comparisons the old ip address will be saved to a file to be compared the next time this script is executed.
#!/bin/bash
# Get current IP address
IP_ADDRESS=$(curl -s ifconfig.me)
# Define variables
MSMTP_ACCOUNT=john
RECIPIENT_EMAIL=john@gmail.com
MSMTP_CONFIG_FILE=/home/john/.msmtprc
IP_HISTORY_FILE=/home/john/.msmtprc_ip
# Check if the IP history file exists; if not, create it
if [[ ! -f $IP_HISTORY_FILE ]]; then
echo "$IP_ADDRESS" > "$IP_HISTORY_FILE"
exit 0 # Exit since there's no previous IP to compare
fi
# Read the historic IP address
IP_HISTORIC=$(cat "$IP_HISTORY_FILE")
# Compare the current IP address with the historic one
if [[ "$IP_HISTORIC" != "$IP_ADDRESS" ]]; then
# Send the email
echo "Subject: UPDATE $IP_ADDRESS" | msmtp -a "$MSMTP_ACCOUNT" "$RECIPIENT_EMAIL"
# Update the historic IP address
echo "$IP_ADDRESS" > "$IP_HISTORY_FILE"
else
echo "IP address has not changed. No email sent."
fi
Save the file, make it executable and test if is working correctly.
sudo chmod +x .msmtprc_email_new_pub_ip.sh
sudo ./home/user/.msmtprc_email_new_pub_ip_update.sh
Automate the task to send your public ip by email every 12 hours
If all went well, continue creating a msmtprc_email_new_pub_ip.service and a msmtprc_email_new_pub_ip.timer files to run them as systemd services to automate sending emails right after system boot and every 12 hours.
sudo nano /etc/systemd/system/msmtprc_email_new_pub_ip.service
[Unit]
Description=Runs a script to send public ip address via email.
[Service]
Type=simple
User=admin
WorkingDirectory=/home/admin/
ExecStart=/home/user/.msmtprc_email_new_pub_ip.sh
[Install]
WantedBy=multi-user.target
Save and exit.
sudo nano /etc/systemd/system/msmtprc_email_new_pub_ip.timer
[Unit]
Description=Timer for msmtprc_email_new_pub.service file
[Timer]
OnCalendar=--* 5,18:00
RandomizedDelaySec=12h
Persistent=true
[Install]
WantedBy=timers.target
Save file and exit.
Reload the daemon
sudo systemctl daemon-reload
Enable both services
sudo systemctl enable msmtprc_email_new_pub_ip.service msmtprc_email_new_pub_ip.timer
Verify both services are running correctly with “sudo status name_of_service”
Reboot your system and verify again.
Check your email. If it’s successful, wait 12 hours for the next email.
Another better option is using only one service file to manage the execution of the script and the timer. The timer here is going to trigger the execution of the script every hour and right after boot. I’m doing this now just because I can 🙂
[Unit]
Description=Sends public ip address to specified email address using msmtp.
After=network-online.target
[Service]
Type=simple
User=user
WorkingDirectory=/home/user
ExecStart=/home/user/.msmtprc_email_new_pub_ip.sh
Restart=always
RestartSec=3600
StartLimitInterval=0
[Install]
WantedBy=multi-user.target