Skip to content

Instantly share code, notes, and snippets.

@not-much-io
Last active November 4, 2015 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save not-much-io/6cd20ef28ba2aa408407 to your computer and use it in GitHub Desktop.
Save not-much-io/6cd20ef28ba2aa408407 to your computer and use it in GitHub Desktop.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import socket
from time import sleep
conf = dict(
# Any email address
toEmail='your_email@gmail.com',
# Google e-mail address
fromEmail='your_email@gmail.com',
# Google password
password='your_password'
)
class Mailer:
def __init__(self, subject, body):
self.subject = subject
self.body = body
def send_mail(self):
from_addr = conf['fromEmail']
to_addr = conf['toEmail']
password = conf['password']
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = self.subject
msg.attach(MIMEText(self.body, 'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, text)
server.quit()
if __name__ == '__main__':
last_ip = ""
while True:
# Might fail on linux, returning localhost
curr_ip = socket.gethostbyname(socket.gethostname())
if curr_ip != last_ip:
newMail = Mailer("IP has changed!",
"New IP: " + curr_ip)
newMail.send_mail()
last_ip = curr_ip
print("Sent new IP: ", curr_ip)
else:
print("ZZzz..")
sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment