Last active
December 15, 2015 13:39
-
-
Save troyscott/5269337 to your computer and use it in GitHub Desktop.
Bottle server that allows you to test files (e.g. csv files) forwarded by Mail Gun. Setup mailgun account on Mail Client or Tablet/Smartphone or
for larger deployment setup SMTP Relay: http://documentation.mailgun.net/user_manual.html#smtp-relay Run this server on a desktop or laptop using a service like pagekite:
http://pagekite.net Make the pa…
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
| from bottle import route, run, post, get, request | |
| import bottle | |
| """ | |
| Setup mailgun account on Mail Client or Tablet/Smartphone or | |
| for larger deployment setup SMTP Relay: | |
| http://documentation.mailgun.net/user_manual.html#smtp-relay | |
| Run this server on a desktop or laptop using a service like pagekite: | |
| http://pagekite.net | |
| Make the pagekite somewhat secure: | |
| pagekite.py 8080 user.pagekite.me +password/user=paswd | |
| Setup a route in mailgun: | |
| http://documentation.mailgun.net/user_manual.html#routes | |
| """ | |
| @route('/') | |
| def home(): | |
| body = """ | |
| <html> | |
| <body> | |
| <h1>MailGun Test Server</h1> | |
| <h3>Processed by Python Bottle Server</h3> | |
| <br><br> | |
| <a href="/email">Mail Gun Simulation</a> | |
| <a href="/gunlog">Mail Gun Generated Data</a> | |
| </body> | |
| </html> | |
| """ | |
| return body | |
| @route('/email') | |
| def email(): | |
| """ | |
| Simulates the POST sent by mailgun. Checkout receiving and | |
| parsing email in the mailgun quickstart guide. | |
| http://documentation.mailgun.net/quickstart.html | |
| """ | |
| print "test emailt" | |
| body = """ | |
| <h2>Mail<h2> | |
| <form method="POST" action="/process" enctype="multipart/form-data"> | |
| To: | |
| <input name="recipient" type="text" /> <br><br> | |
| From: | |
| <input name="sender" type="text" /> <br><br> | |
| Subject: | |
| <input name="subject" type="text" /> <br><br> | |
| Attachment: <input name="attachment-1" type="file" /> <br><br> | |
| <input type="submit" /> | |
| </form> | |
| """ | |
| return body | |
| @post('/process') | |
| def process_email(): | |
| """ | |
| Field Storage: | |
| form.name The name of the field, if it is specified | |
| form.filename If an FTP transaction, the client-side filename | |
| form.value The value of the field as a string | |
| form.file file object from which data can be read | |
| form.type The content type, if applicable | |
| form.headers All of the HTTP headers returned as a dictionary | |
| """ | |
| print "Process E-mail\n" | |
| sender = request.forms.get('sender') | |
| recipient = request.forms.get('recipient') | |
| subject = request.forms.get('subject', '') | |
| attachments = request.files.allitems() | |
| for k,v in attachments: | |
| form = v | |
| print "Headers: %s" % form.headers | |
| print "Attachment Name: %s " % form.name | |
| print "File Object: %s" % form.file | |
| print "Filename: %s" % form.filename | |
| print "File Type: %s" % form.type | |
| file = open("c:\\Temp\\import.csv", "w") | |
| # You may need to change the logic depending on | |
| # the file format e.g. data[:-1] | |
| for data in form.file: | |
| # Process the data here | |
| print data | |
| file.write(data) | |
| file.close() | |
| return "OK" | |
| print "Start Server" | |
| run(host='0.0.0.0',port=8080,debug=True) | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are using pagekite with @mailgun make sure you use https for the url. Example below includes basic auth:
https://user:[email protected]