SHARE
TWEET

Untitled

a guest Apr 21st, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import base64
  2. import urllib
  3.  
  4. MAILGUN_API_URL = "<api-url>"
  5. MAILGUN_API_TOKEN = "<api-token>"
  6.  
  7. def send_mail(from_email, to_email, subject, message):
  8.  
  9.   data = urllib.parse.urlencode({
  10.     "from": from_email,
  11.     "to": to_email,
  12.     "subject": subject,
  13.     "text": message,
  14.   }, doseq=True).encode()
  15.  
  16.   request = urllib.request.Request(MAILGUN_API_URL, data=data)
  17.   request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  18.   encoded_token = base64.b64encode(("api:" + MAILGUN_API_TOKEN).encode("ascii")).decode("ascii")
  19.   request.add_header("Authorization", "Basic {}".format(encoded_token))
  20.  
  21.   try:
  22.     response = urllib.request.urlopen(request)
  23.     print(response.read())
  24.   except Exception as err:
  25.     print(err)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top