-
-
Save yun14u/0331d77854baab2880bcdc12c18aa478 to your computer and use it in GitHub Desktop.
raspberry pi - send email on startup (will need gmail account to send from)
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 httplib2 | |
import os | |
import subprocess | |
import datetime | |
import oauth2client | |
from oauth2client import client, tools | |
import base64 | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from apiclient import errors, discovery | |
SCOPES = 'https://www.googleapis.com/auth/gmail.send' | |
CLIENT_SECRET_FILE = 'client_secret.json' | |
APPLICATION_NAME = 'Gmail API Quickstart' | |
def get_credentials(): | |
home_dir = os.path.expanduser('~') | |
credential_dir = os.path.join(home_dir, '.credentials') | |
if not os.path.exists(credential_dir): | |
os.makedirs(credential_dir) | |
credential_path = os.path.join(credential_dir, 'gmail-python-email-send.json') | |
store = oauth2client.file.Storage(credential_path) | |
credentials = store.get() | |
if not credentials or credentials.invalid: | |
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) | |
flow.user_agent = APPLICATION_NAME | |
credentials = tools.run_flow(flow, store) | |
print('Storing credentials to ' + credential_path) | |
return credentials | |
def SendMessage(sender, to, subject, msgPlain): | |
credentials = get_credentials() | |
http = credentials.authorize(httplib2.Http()) | |
service = discovery.build('gmail', 'v1', http=http) | |
message1 = CreateMessage(sender, to, subject, msgPlain) | |
SendMessageInternal(service, "me", message1) | |
def SendMessageInternal(service, user_id, message): | |
try: | |
message = (service.users().messages().send(userId=user_id, body=message).execute()) | |
print('Message Id: %s' % message['id']) | |
return message | |
except errors.HttpError as error: | |
print('An error occurred: %s' % error) | |
def CreateMessage(sender, to, subject, message_text): | |
msg = MIMEText(message_text) | |
msg['To'] = to | |
msg['From'] = sender | |
msg['Subject'] = subject | |
return {'raw': base64.urlsafe_b64encode(msg.as_string())} | |
def main(): | |
today = datetime.date.today() | |
to = "to_address@gmail.com" | |
sender = "from_address@gmail.com" | |
subject = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y') | |
# Very Linux Specific | |
arg='ip route list |grep metric' | |
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 | |
msgPlain = my_ip | |
SendMessage(sender, to, subject, msgPlain) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
% python startup_mailer