Skip to content

Instantly share code, notes, and snippets.

@dev-drprasad
Created April 11, 2019 14:47
Show Gist options
  • Save dev-drprasad/92be4c05a8dd97f238e0ee3942b396bd to your computer and use it in GitHub Desktop.
Save dev-drprasad/92be4c05a8dd97f238e0ee3942b396bd to your computer and use it in GitHub Desktop.
Send email via mailgun using python3 standard library (urllib)
import base64
import urllib
MAILGUN_API_URL = "<api-url>"
MAILGUN_API_TOKEN = "<api-token>"
def send_mail(from_email, to_email, subject, message):
data = urllib.parse.urlencode({
"from": from_email,
"to": to_email,
"subject": subject,
"text": message,
}, doseq=True).encode()
request = urllib.request.Request(MAILGUN_API_URL, data=data)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
encoded_token = base64.b64encode(("api:" + MAILGUN_API_TOKEN).encode("ascii")).decode("ascii")
request.add_header("Authorization", "Basic {}".format(encoded_token))
try:
response = urllib.request.urlopen(request)
print(response.read())
except Exception as err:
print(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment