Created
October 31, 2020 17:35
-
-
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
This file contains hidden or 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
#!/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