Last active
November 4, 2015 08:39
-
-
Save not-much-io/6cd20ef28ba2aa408407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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