From 1407c4eec04e9f48a938fb22c7ff9e8b4da6713a Mon Sep 17 00:00:00 2001 From: 0x6f736f646f Date: Wed, 9 Oct 2019 13:40:20 +0300 Subject: [PATCH 1/4] Adding sendgrid for email service --- app/__init__.py | 8 +++----- app/auth/email.py | 2 -- app/email.py | 19 +++++++++++++------ config.py | 3 ++- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 7cc9520..07dae3c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,19 +1,17 @@ from flask import Flask from flask_login import LoginManager -from flask_mail import Mail from flask_migrate import Migrate from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from config import Config - +from sendgrid import SendGridAPIClient # extensions db = SQLAlchemy() migrate = Migrate() login = LoginManager() login.login_view = 'auth.login' -mail = Mail() moment = Moment() - +mail = SendGridAPIClient(Config.SENDGRID_API_KEY) def create_app(config_class=Config): @@ -23,7 +21,7 @@ def create_app(config_class=Config): db.init_app(app) migrate.init_app(app, db) login.init_app(app) - mail.init_app(app) + mail = SendGridAPIClient(Config.SENDGRID_API_KEY) moment.init_app(app) from app.errors import bp as errors_bp diff --git a/app/auth/email.py b/app/auth/email.py index f7d2894..849f24a 100644 --- a/app/auth/email.py +++ b/app/auth/email.py @@ -1,6 +1,5 @@ from threading import Thread from flask import render_template, current_app -from flask_mail import Message from app.email import send_email def send_password_reset_email(user): @@ -9,6 +8,5 @@ def send_password_reset_email(user): "[Bootcamp] Reset your password", sender=current_app.config["ADMIN_EMAIL"], recipients=[user.email], - text_body=render_template('auth/email/reset_password.txt', user=user, token=token), html_body=render_template('auth/email/reset_password.html', user=user, token=token) ) \ No newline at end of file diff --git a/app/email.py b/app/email.py index 3226ea0..8cd4d25 100644 --- a/app/email.py +++ b/app/email.py @@ -1,14 +1,21 @@ from threading import Thread from flask import current_app -from flask_mail import Message from app import mail +from sendgrid.helpers.mail import Mail def send_async_email(app, msg): with app.app_context(): - mail.send(msg) + try: + response = mail.send(msg) + except Exception as e: + print(e) -def send_email(subject, sender, recipients, text_body, html_body): - msg = Message(subject, sender=sender, recipients=recipients) - msg.body = text_body - msg.html = html_body +def send_email(subject, sender, recipients, html_body): + print(sender, "\n", recipients) + msg = Mail( + from_email = sender, + to_emails = recipients, + subject = subject, + html_content = html_body + ) Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start() \ No newline at end of file diff --git a/config.py b/config.py index 955eb63..93a5d17 100644 --- a/config.py +++ b/config.py @@ -14,4 +14,5 @@ class Config: MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') - ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL') \ No newline at end of file + ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL') + SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY') \ No newline at end of file From b244db626cee4cfdc2015b85b201abc697fac05b Mon Sep 17 00:00:00 2001 From: 0x6f736f646f Date: Wed, 9 Oct 2019 13:41:32 +0300 Subject: [PATCH 2/4] Adding sendgrid requirements --- requirements.txt | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 07c1780..2c93acb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,7 @@ +alembic==1.2.1 +astroid==2.3.1 +blinker==1.4 +Click==7.0 Flask==1.1.1 Flask-Login==0.4.1 Flask-Mail==0.9.1 @@ -5,6 +9,24 @@ Flask-Migrate==2.5.2 Flask-Moment==0.9.0 Flask-SQLAlchemy==2.4.0 Flask-WTF==0.14.2 +isort==4.3.21 +itsdangerous==1.1.0 +Jinja2==2.10.3 +lazy-object-proxy==1.4.2 +Mako==1.1.0 +MarkupSafe==1.1.1 +mccabe==0.6.1 +pkg-resources==0.0.0 PyJWT==1.7.1 pylint==2.3.1 -python-dotenv==0.10.3 \ No newline at end of file +python-dateutil==2.8.0 +python-dotenv==0.10.3 +python-editor==1.0.4 +python-http-client==3.2.1 +sendgrid==6.1.0 +six==1.12.0 +SQLAlchemy==1.3.9 +typed-ast==1.4.0 +Werkzeug==0.16.0 +wrapt==1.11.2 +WTForms==2.2.1 From 6d430db527325e7537974cdefbbe644311ba10ae Mon Sep 17 00:00:00 2001 From: 0x6f736f646f Date: Wed, 9 Oct 2019 13:46:30 +0300 Subject: [PATCH 3/4] Removing unnecessary environment variables from the gmail email service --- README.md | 6 +----- config.py | 5 ----- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/README.md b/README.md index 8f2edbe..e017b4e 100644 --- a/README.md +++ b/README.md @@ -30,13 +30,9 @@ A Minimal social networking application using Flask ``` FLASK_APP=run.py FLASK_DEBUG=true - MAIL_SERVER=smtp.googlemail.com - MAIL_PORT=587 - MAIL_USE_TLS=1 - MAIL_USERNAME= - MAIL_PASSWORD= SECRET_KEY= ADMIN_EMAIL= + SENDGRID_API_KEY= ``` diff --git a/config.py b/config.py index 93a5d17..445e45f 100644 --- a/config.py +++ b/config.py @@ -9,10 +9,5 @@ class Config: SQLALCHEMY_TRACK_MODIFICATIONS = False POSTS_PER_PAGE = 25 # mail config - MAIL_SERVER = os.environ.get('MAIL_SERVER') - MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) - MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None - MAIL_USERNAME = os.environ.get('MAIL_USERNAME') - MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL') SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY') \ No newline at end of file From 5f7736e9a549b2ebbf74bc8a4933c0fe798da343 Mon Sep 17 00:00:00 2001 From: 0x6f736f646f Date: Wed, 9 Oct 2019 13:46:59 +0300 Subject: [PATCH 4/4] Removing print for debugging --- app/email.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/email.py b/app/email.py index 8cd4d25..1acf5ad 100644 --- a/app/email.py +++ b/app/email.py @@ -11,7 +11,6 @@ def send_async_email(app, msg): print(e) def send_email(subject, sender, recipients, html_body): - print(sender, "\n", recipients) msg = Mail( from_email = sender, to_emails = recipients,