-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailgun.py
More file actions
executable file
·57 lines (40 loc) · 1.46 KB
/
mailgun.py
File metadata and controls
executable file
·57 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
import cgi
#import cgitb
#cgitb.enable()
import requests
from requests.auth import HTTPBasicAuth
print ("Content-Type: text/html")
print()
#get vars from apache
form = cgi.FieldStorage()
mail_to = form.getvalue('to')
mail_subject = form.getvalue('subject')
mail_body = form.getvalue('body')
mail_send_key = form.getvalue('key')
captcha_key = form.getvalue('g-recaptcha-response')
#captcha
captcha_url = 'https://www.google.com/recaptcha/api/siteverify'
captcha_postdata = {'secret': 'YOUR reCAPTCHA SECRET', 'response': captcha_key}
captcha_request = requests.post(captcha_url, params=captcha_postdata)
if captcha_request.json()['success'] == True and mail_send_key == 'CUSTOM KEY':
# send mail
mail_from = 'mail script <YOUR "FROM" ADDRESS>'
url = 'https://api.mailgun.net/v3/YOUR-MAILGUN-DOMAIN/messages'
postdata = {'from': mail_from, 'to': mail_to, 'subject': mail_subject, 'text': mail_body, 'html': mail_body}
mail = requests.post(url, params=postdata, auth=HTTPBasicAuth('api', 'YOUR MAILGUN API KEY'))
elif captcha_request.json()['success'] != True:
print ('ERROR - mail not sent - CAPTCHA incorrect <br><br>')
else:
print ('ERROR - mail not sent <br><br>')
# html-formatted output
print ('<style> body {font-size:20px;} </style>')
print ('to: ' + mail_to)
print ('<br>')
print ('from: ' + mail_from)
print ('<br>')
print ('subject: ' + mail_subject)
print ('<br>')
print ('body: ' + mail_body)
print('<br><br>')
print (mail.json()['message'])