Instantly share code, notes, and snippets.

Embed
What would you like to do?
raspberry pi - send email on startup (will need gmail account to send from)
import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = 'me@example.com'
gmail_user = 'test@gmail.com'
gmail_password = 'yourpassword'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
# Very Linux Specific
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src')+1]
my_ip = 'Your ip is %s' % ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
@johnantoni

This comment has been minimized.

Show comment
Hide comment
@johnantoni

johnantoni Dec 31, 2013

create code dir

mkdir ~/code

save in /home/pi/code/startup_mailer.py

make it executable

cd ~/code
sudo chmod +x startup_mailer.py

add to boot.rc

sudo nano /boot/boot.rc

this...

#Script to email ip address upon reboot
python /home/pi/Code/startup_mailer.py

and if on raspbian

sudo nano /etc/rc.local

this...

# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  python /home/pi/code/startup_mailer.py
fi
exit 0
Owner

johnantoni commented Dec 31, 2013

create code dir

mkdir ~/code

save in /home/pi/code/startup_mailer.py

make it executable

cd ~/code
sudo chmod +x startup_mailer.py

add to boot.rc

sudo nano /boot/boot.rc

this...

#Script to email ip address upon reboot
python /home/pi/Code/startup_mailer.py

and if on raspbian

sudo nano /etc/rc.local

this...

# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  python /home/pi/code/startup_mailer.py
fi
exit 0
@johnantoni

This comment has been minimized.

Show comment
Hide comment
Owner

johnantoni commented Dec 31, 2013

@johnantoni

This comment has been minimized.

Show comment
Hide comment
@johnantoni

johnantoni Dec 31, 2013

default raspberry pi login

username: pi
password: raspberry
Owner

johnantoni commented Dec 31, 2013

default raspberry pi login

username: pi
password: raspberry
@johnantoni

This comment has been minimized.

Show comment
Hide comment
@johnantoni

johnantoni Dec 31, 2013

set locale

vim ~/.bashrc

add....

export LC_ALL=C
Owner

johnantoni commented Dec 31, 2013

set locale

vim ~/.bashrc

add....

export LC_ALL=C
@a3poify

This comment has been minimized.

Show comment
Hide comment
@a3poify

a3poify Dec 31, 2013

This is awesome, might use it for something, can't think of a use yet.

a3poify commented Dec 31, 2013

This is awesome, might use it for something, can't think of a use yet.

@johnantoni

This comment has been minimized.

Show comment
Hide comment
@johnantoni

johnantoni Jan 12, 2014

or via a cronjob

crontab -e

then

@reboot python /home/pi/code/startup_mailer.py
Owner

johnantoni commented Jan 12, 2014

or via a cronjob

crontab -e

then

@reboot python /home/pi/code/startup_mailer.py
@johnantoni

This comment has been minimized.

Show comment
Hide comment
@johnantoni

johnantoni Jan 12, 2014

looking at it the cron job is more reliable, placing it in the rc.local file fails on wifi

Owner

johnantoni commented Jan 12, 2014

looking at it the cron job is more reliable, placing it in the rc.local file fails on wifi

@anassar

This comment has been minimized.

Show comment
Hide comment
@anassar

anassar May 21, 2016

Thanks for the code you posted. On Raspberry Pi 3, Model B, I had to slightly change your startup_mailer.py script when I invoke it using Cron at booting time:

tries = 0
while True:
    if (tries > 60):
        exit()
    try:
        smtpserver = smtplib.SMTP('smtp.gmail.com', 587, timeout=30) # Server to use.
        break
    except Exception as e:
        tries = tries + 1
        time.sleep(1)

anassar commented May 21, 2016

Thanks for the code you posted. On Raspberry Pi 3, Model B, I had to slightly change your startup_mailer.py script when I invoke it using Cron at booting time:

tries = 0
while True:
    if (tries > 60):
        exit()
    try:
        smtpserver = smtplib.SMTP('smtp.gmail.com', 587, timeout=30) # Server to use.
        break
    except Exception as e:
        tries = tries + 1
        time.sleep(1)
@karlpap

This comment has been minimized.

Show comment
Hide comment
@karlpap

karlpap Jun 8, 2016

anassar's workaround worked for me. Then I realized I was calling the script from cron @reboot - without allowing for additional tries, apparently everything the python script needed had not loaded when the script ran and the script failed.
Another solution that works (and if leave anassar's code in the script and print out tries, it ALWAYS only needs 1 try) is to delay the running of the script. Can do in the cron command:
@reboot sleep 300 && python /path/to/python_script.py

karlpap commented Jun 8, 2016

anassar's workaround worked for me. Then I realized I was calling the script from cron @reboot - without allowing for additional tries, apparently everything the python script needed had not loaded when the script ran and the script failed.
Another solution that works (and if leave anassar's code in the script and print out tries, it ALWAYS only needs 1 try) is to delay the running of the script. Can do in the cron command:
@reboot sleep 300 && python /path/to/python_script.py

@bereshr1

This comment has been minimized.

Show comment
Hide comment
@bereshr1

bereshr1 Jul 1, 2016

Hey There. Thanks for this. Will make remote monitoring of my Pi much easier. Having an issue though.
Gmail refused the sign-in attempt. Sent me an email saying an app without modern security protocols is accessing my account.
Anyone experienced this or found a workaround?

bereshr1 commented Jul 1, 2016

Hey There. Thanks for this. Will make remote monitoring of my Pi much easier. Having an issue though.
Gmail refused the sign-in attempt. Sent me an email saying an app without modern security protocols is accessing my account.
Anyone experienced this or found a workaround?

@shahvineet98

This comment has been minimized.

Show comment
Hide comment
@shahvineet98

shahvineet98 Jul 6, 2016

Hey @bereshr1! Is there any chance you are using two step verification on your gmail account? That could be the issue.

shahvineet98 commented Jul 6, 2016

Hey @bereshr1! Is there any chance you are using two step verification on your gmail account? That could be the issue.

@nikmart

This comment has been minimized.

Show comment
Hide comment
@nikmart

nikmart Aug 23, 2016

@bereshr1, I just had this issue and needed to allow less secure devices to access gmail. You can look here for that setting: here

nikmart commented Aug 23, 2016

@bereshr1, I just had this issue and needed to allow less secure devices to access gmail. You can look here for that setting: here

@yun14u

This comment has been minimized.

Show comment
Hide comment
@yun14u

yun14u Dec 19, 2016

found the solution and modified johnantoni's script. just tested and worked !

yun14u commented Dec 19, 2016

found the solution and modified johnantoni's script. just tested and worked !

@TheMole3

This comment has been minimized.

Show comment
Hide comment
@TheMole3

TheMole3 Dec 28, 2017

Hello! Sorry if i bring up this old thread but i get this error:

Traceback (most recent call last):
  File "/home/pi/code/startup_mailer.py", line 21, in <module>
    ipaddr = split_data[split_data.index('src')+1]
ValueError: 'src' is not in list

Any idea of how to fix it?

TheMole3 commented Dec 28, 2017

Hello! Sorry if i bring up this old thread but i get this error:

Traceback (most recent call last):
  File "/home/pi/code/startup_mailer.py", line 21, in <module>
    ipaddr = split_data[split_data.index('src')+1]
ValueError: 'src' is not in list

Any idea of how to fix it?

@ChristopherLu

This comment has been minimized.

Show comment
Hide comment
@ChristopherLu

ChristopherLu Jul 22, 2018

The 'src' is not in list is caused by python3. Try python2 instead.

ChristopherLu commented Jul 22, 2018

The 'src' is not in list is caused by python3. Try python2 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment