Last active
May 5, 2019 18:54
-
-
Save chowmean/d994dae449c55c3d5e14fb4ad1e8a670 to your computer and use it in GitHub Desktop.
Basic flask server for sending mails through gmail.
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
| ''' How to run | |
| ## install virtual env | |
| sudo apt-get install virtualenv | |
| ## intialize env | |
| virtualenv venv | |
| ## activate Virtualenv | |
| source venv/bin/activate | |
| ## insatll Flask | |
| pip install flask | |
| ##Run server | |
| python send_email.py | |
| ``` | |
| # Python code to illustrate Sending mail from | |
| # your Gmail account | |
| def send_mail_to_gmail(sender_email,rec_email,data): | |
| import smtplib | |
| # creates SMTP session | |
| s = smtplib.SMTP('smtp.gmail.com', 587) | |
| # start TLS for security | |
| s.starttls() | |
| # Authentication Replace these values | |
| s.login("sender_email","sender_password") | |
| # message to be sent | |
| message =data | |
| # sending the mail | |
| s.sendmail(sender_email, rec_email, message) | |
| # terminating the session | |
| s.quit() | |
| from flask import Flask, jsonify, request, abort | |
| app = Flask(__name__) | |
| @app.route('/', methods=['POST']) | |
| def index(): | |
| if not request.json or not 'sender_email' in request.json: | |
| abort(400) | |
| sender_email = request.json.get("sender_email") | |
| message = request.json.get("data") | |
| rec_email = request.json.get("rec_email") | |
| send_mail_to_gmail(sender_email, rec_email, message) | |
| return jsonify({'message': "email sent"}) | |
| if __name__ == '__main__': | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment