Skip to content

Instantly share code, notes, and snippets.

@omiq
Created May 2, 2017 19:23
Show Gist options
  • Select an option

  • Save omiq/855fe83c8f3f6bb86e470c5fa0cbcd2a to your computer and use it in GitHub Desktop.

Select an option

Save omiq/855fe83c8f3f6bb86e470c5fa0cbcd2a to your computer and use it in GitHub Desktop.
Send IP address via Python / Mailgun
import subprocess
import requests
# load API key from file
inFile = open('/home/pi/mailgun-api.txt', 'r')
api_key = inFile.readline().rstrip()
inFile.close()
# get host and IP
this_host = subprocess.check_output("hostname", shell=True).rstrip()
IP_line = subprocess.check_output("hostname -I", shell=True).rstrip()
# send via Mailgun
def send_message():
return requests.post(
"https://api.mailgun.net/v3/chrisg.mailgun.org/messages",
auth=("api", api_key),
data={"from": "Bot <[email protected]>",
"to": ["[email protected]"],
"subject": "MY IP: " + IP_line,
"text": this_host + " = host and " + IP_line + " is my IP!"})
print send_message()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment