Posts Tagged ‘sSMTP’

Send Email when IP Address Changes

Posted: 6 Μαρτίου 2018 in linux
Ετικέτες: , , , ,

1. Setup Gmail and mSMTP

This time we are using mSMTP because it’s easy, and Gmail because it’s free.

I would prefer to create a new Gmail account specific only for this purpose. Use your creativity to create long and obfuscated password, e.g. with mkpasswd -m sha-512 yOuRp4ssW0rD you can get a long string which is should be very nice for password.

Next, install mSMTP and its mail “client”:

https://wiki.debian.org/msmtp

sudoapt-get install msmtp mailutils

Then edit the config file at /etc/msmtp/ssmtp.conf:

 

root=youremail@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=youremail@gmail.com
AuthPass=yOuRfUnKyP4ssWorD
UseTLS=YES
UseSTARTTLS=YES
AuthMethod=LOGIN

mSMTP is not a daemon, so don’t worry about starting the service or such.

Next, test your setup:

echo"test message"| mail -s "testing msmtp"yourothermail@gmail.com

Note the blank like after the subject, everything after this line is the body of the email. When you’re finished, press Ctrl-D. mSMTP may take a few seconds to send the message before closing.

apt-get install curl

2. The Script

#!/bin/bash
 
#Script to report public IP address change
#By: Soultidis D. Christos
 

curl ipinfo.io/ip > /home/username/emailscript/erxeteip.txt


oldip=`cat /home/username/emailscript/oldip.txt`
erxeteip=`cat /home/username/emailscript/erxeteip.txt`

if [[ $erxeteip = $oldip ]]
then

    #echo $oldip > /home/username/emailscript/oldip.txt
    echo "Kamia allagi"

else
    echo $erxeteip > /home/username/emailscript/newip.txt
echo $erxeteip > /home/username/emailscript/oldip.txt


    cat /home/username/emailscript/newip.txt | mailx -s "Dynamic Public IP Address" youremail@gmail.com

fi

This time we use Bash, because you might not realize that you’re already fluent with Bash.

Save this anywhere in your home folder. I personally have my own /home/username/emailscript/

 

3. Cron
To make this run periodically, add the script as a cron job. More detail on cron you can STFG (Search The Fine Google).

crontab -e

Then add this to run the script every 30 minutes

#*/30 * * * * /home/username/emailscript/ip.sh >/dev/null 2>&1

ΥΓ. θα πρέπει να δημιουργήσω το oldip.txt

 

Cron daemon is not running. I really screwed up with this some months ago.

Type:

pgrep cron 

If you see no number, then cron is not running. sudo /etc/init.d/cron start can be used to start cron.

EDIT: Rather than invoking init scripts through /etc/init.d, use the service utility, e.g.

sudo service cron start

How to install msmtp to Debian 10 for sending emails with gmail

 by caupo

I am making this post because last 2 days I tried to install msmtp to my Debian 10 server. Previously I used ssmtp where I did not have any problems but it cannot be installed at the time of making this tutorial. Because nobody is maintaining the package anymore or something like this as far as I understood.

I spend countless hours to the error that said my username and password is not accepted by Gmail. So here is my tutorial to how fully install it and configure it.

Step 1: Installing required packages.

sudo apt-get install msmtp msmtp-mta

Step 2: Create an app password in Google for msmtp

This (Step 2.* in this tutorial) was the missing piece of the puzzle which weren’t in any of the msmtp tutorials I did through. I found out about it in one of the Postfix tutorials where it was included after many hours of trial and error. Because at first, I thought the msmtp config file where was password field had to be filled with the same password with what you log in as a person to your account.

Go to: https://myaccount.google.com and log into the email account you want to start sending the emails.

Step 2.1: Select Security from the side menu and turn 2-Step Verification ON.

Step 2.2: After that click on App passwords

Step 2.3: Next up select Other (Custom name)

Step 2.4: Fill in the name as msmtp for example

Step 2.5: Get the password for your msmtp configuration file

Save this password to some notepad temporarily so you can copy & paste it later to a configuration file because once you click DONE you cannot see the password again. At least I have not found a way to re-open this popup.

Step 3: Create a configuration file

sudo nano /etc/msmtprc

Step 4: Configure msmtprc file

defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log
account        gmail
host           smtp.gmail.com
port           587
from           username@gmail.com
user           username
password       password
account default : gmail

The username@gmail.com has to be changed in the config file with the email you used to create an app password

The user field where is username has to be changed as well. For example, if your email is john.doe@gmail.com then change username to john.doe.

defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log
account        gmail
host           smtp.gmail.com
port           587
from           john.doe@gmail.com
user           john.doe
password       fkelpwkdymcwslwo
account default : gmail

Step 5: Testing the email configuration

echo "Message" | mail -s "Title" youremailaddress@gmail.com

After that command, you should receive an email from your configured account.