Skip to content

Instantly share code, notes, and snippets.

@yun14u
Forked from johnantoni/startup_mailer.py
Last active December 20, 2016 17:32
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 yun14u/0331d77854baab2880bcdc12c18aa478 to your computer and use it in GitHub Desktop.
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)
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()
@yun14u
Copy link
Author

yun14u commented Dec 20, 2016

  1. Complete Step 1, Step 2, and Step 4 of this QuickStart Guide (URL: https://developers.google.com/gmail/api/quickstart/python)
  2. Save the attached python script above and run it
    % python startup_mailer
  3. Follow johnantoni's crontab setup and reboot the raspberry pi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment