Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Replacing current email service with some emai service like sendgrid #7 #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your-email-address>
MAIL_PASSWORD=<your-mail-password>
SECRET_KEY=<secret-key>
ADMIN_EMAIL=<your-email-address>

PUSHER_APP_ID = <pusher-app-id>
PUSHER_KEY = <pusher-key>
PUSHER_SECRET = <pusher-secret>
Expand Down
8 changes: 3 additions & 5 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
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
from app.pusher import Pusher

# extensions
db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'auth.login'
mail = Mail()
moment = Moment()
mail = SendGridAPIClient(Config.SENDGRID_API_KEY)
pusher = Pusher()



def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(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)
pusher.init_app(app)

Expand Down
2 changes: 0 additions & 2 deletions app/auth/email.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
)
18 changes: 12 additions & 6 deletions app/email.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
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):
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()
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Config:
SQLALCHEMY_TRACK_MODIFICATIONS = False
POSTS_PER_PAGE = 25
# mail config
ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL')
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
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
Expand Down
24 changes: 23 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
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
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
pusher==2.1.4
PyJWT==1.7.1
pylint==2.3.1
python-dotenv==0.10.3
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