Skip to content

Instantly share code, notes, and snippets.

@AyeGill
Created October 31, 2020 17:35
Show Gist options
  • Save AyeGill/6e0494fa639032bd683e948d84422392 to your computer and use it in GitHub Desktop.
Save AyeGill/6e0494fa639032bd683e948d84422392 to your computer and use it in GitHub Desktop.
Sendmail.py: A simple script that uses mailgun.org to send an email
#!/usr/bin/env python3
import requests
import sys
import os
## Send stdin as an email with first cmdline arg as subject
# could also hardcode thes
apikey = os.environ['MAILGUN_API_KEY']
mgdomain = os.environ['MAILGUN_DOMAIN']
targetmail = os.environ['TARGET_EMAIL']
name = "Mailgun bot"
text = sys.stdin.read()
subject = sys.argv[1]
url = "https://api.mailgun.net/v3/" + mgdomain + "/messages"
sender = name + " <mailgunbot@" + mgdomain + ">"
r = requests.post(
url,
auth=("api",apikey),
data={
"from": sender,
"to": [targetmail],
"subject": subject,
"text": text
}
)
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment