Created
April 11, 2019 14:47
-
-
Save dev-drprasad/92be4c05a8dd97f238e0ee3942b396bd to your computer and use it in GitHub Desktop.
Send email via mailgun using python3 standard library (urllib)
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
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