Skip to content

Instantly share code, notes, and snippets.

@tomazursic
Last active June 30, 2017 21:39
Show Gist options
  • Save tomazursic/7ea76f3a185a080b86c2ebd46f23b0b2 to your computer and use it in GitHub Desktop.
Save tomazursic/7ea76f3a185a080b86c2ebd46f23b0b2 to your computer and use it in GitHub Desktop.
Python - Mailer
#!/usr/bin/env python
# crontab -e
# @reboot python /home/pi/code/startup_mailer.py
import time
import subprocess
import smtplib
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = '[email protected]'
gmail_user = '[email protected]'
gmail_password = 'yourpassword'
today = datetime.date.today()
tries = 0
while True:
if (tries > 60):
exit()
try:
smtpserver = smtplib.SMTP('smtp.gmail.com', 587, timeout=30)
smtpserver.ehlo() # Says 'hello' to the server
smtpserver.starttls()
smtpserver.ehlo()
break
except Exception as e:
tries = tries + 1
time.sleep(1)
smtpserver.login(gmail_user, gmail_password)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment