-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail_ip_address.py
46 lines (34 loc) · 1.2 KB
/
email_ip_address.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright (c) Matthew David Miller. All rights reserved.
# Licensed under the MIT License.
# Run the script as root.
# Libraries to import.
import smtplib
import urllib.request
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import subprocess
# Variables to edit
ip_address = urllib.request.urlopen("https://api.ipify.org").read().decode('utf-8')
email_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465)
email_address = 'matthew.miller.secondary@gmail.com'
email_address_to_send_to = 'matthewdavidmiller1@gmail.com'
email_subject = 'IP address of Raspberry Pi'
email_body = ip_address
email_password = 'Password'
full_email = MIMEMultipart()
full_email['From'] = email_address
full_email['To'] = email_address_to_send_to
full_email['Subject'] = email_subject
full_email.attach(MIMEText(email_body, 'plain'))
email = full_email.as_string()
# Prints date
date = subprocess.check_output(["date", "+'%m/%d/%Y %H:%M:%S'"])
print(date)
# Script to email ip address.
try:
email_ssl.login(email_address, email_password)
email_ssl.sendmail(email_address, email_address_to_send_to, email)
email_ssl.close()
print('Email successfully sent.')
except:
print('Error detected.')