Skip to content

Commit

Permalink
fix the email problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lesliebinbin committed Aug 22, 2018
1 parent edb9848 commit 3d67220
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 20 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.DS_Store
.env
.flaskenv
*.pyc
*.pyo
env/
env*
dist/
build/
*.egg
*.egg-info/
_mailinglist
.tox/
.cache/
.pytest_cache/
.idea/
docs/_build/

# Coverage reports
htmlcov/
.coverage
.coverage.*
*,cover
Binary file modified __pycache__/config.cpython-36.pyc
Binary file not shown.
Binary file modified app/__pycache__/email.cpython-36.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions app/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask import current_app, render_template
from threading import Thread
from flask_mail import Message
from . import mail

def send_async_email(app, msg):
with app.app_context():
mail.send(msg)

def send_email(to, subject, template, **kwargs):
app = current_app._get_current_object()
msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=to)
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()
return thr
Binary file modified app/main/__pycache__/views.cpython-36.pyc
Binary file not shown.
36 changes: 17 additions & 19 deletions app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@
from .forms import NameForm
from .. import db, mail
from ..models import User
from flask_mail import Message
from threading import Thread


def send_async_email(app, msg):
with app.app_context():
mail.send(msg)


def send_mail(to, subject, template, **kwargs):
msg = Message(
main.config['MAIL_SUBJECT_PREFIX'] + subject,
sender=main.config['FLASKY_MAIL_SENDER'],
recipients=to)
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
thr = Thread(target=send_async_email, args=[main, msg])
thr.start()
return thr
from ..email import send_email
from flask import current_app


@main.route("/", methods=['GET', 'POST'])
Expand All @@ -36,6 +19,21 @@ def index():
db.session.add(user)
db.session.commit()
session['known'] = False
if current_app._get_current_object().config['FLASKY_ADMIN']:
send_email(
[current_app._get_current_object().config['FLASKY_ADMIN']],
'New user',
'mail/new_user',
user=user)
else:
send_email(
[
'[email protected]',
'[email protected]',
],
'New user',
'mail/new_user',
user=user)
else:
session['known'] = True
session['name'] = form.name.data
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Config:
SECRET_KEY = os.environ.get('SECRET_KEY', 'hard to guess string')
MAIL_SERVER = os.environ.get('MAIL_SERVER', 'stmp.googlemail.com')
MAIL_SERVER = os.environ.get('MAIL_SERVER', 'smtp.googlemail.com')
MAIL_PORT = int(os.environ.get('MAIL_PORT', '587'))
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS',
'true').lower() in ['true', 'on', '1']
Expand Down
Binary file modified data-dev.sqlite
Binary file not shown.

0 comments on commit 3d67220

Please sign in to comment.