Browse Source

Fix sending emails via celery

sh4nks 8 years ago
parent
commit
b356e0035d
3 changed files with 10 additions and 7 deletions
  1. 2 1
      flaskbb/app.py
  2. 3 3
      flaskbb/auth/views.py
  3. 5 3
      flaskbb/email.py

+ 2 - 1
flaskbb/app.py

@@ -171,7 +171,8 @@ def configure_extensions(app):
     @babel.localeselector
     @babel.localeselector
     def get_locale():
     def get_locale():
         # if a user is logged in, use the locale from the user settings
         # if a user is logged in, use the locale from the user settings
-        if current_user.is_authenticated and current_user.language:
+        if current_user and \
+                current_user.is_authenticated and current_user.language:
             return current_user.language
             return current_user.language
         # otherwise we will just fallback to the default language
         # otherwise we will just fallback to the default language
         return flaskbb_config["DEFAULT_LANGUAGE"]
         return flaskbb_config["DEFAULT_LANGUAGE"]

+ 3 - 3
flaskbb/auth/views.py

@@ -149,7 +149,7 @@ def register():
         user = form.save()
         user = form.save()
 
 
         if flaskbb_config["ACTIVATE_ACCOUNT"]:
         if flaskbb_config["ACTIVATE_ACCOUNT"]:
-            send_activation_token(user)
+            send_activation_token.delay(user)
             flash(_("An account activation email has been sent to %(email)s",
             flash(_("An account activation email has been sent to %(email)s",
                     email=user.email), "success")
                     email=user.email), "success")
         else:
         else:
@@ -172,7 +172,7 @@ def forgot_password():
         user = User.query.filter_by(email=form.email.data).first()
         user = User.query.filter_by(email=form.email.data).first()
 
 
         if user:
         if user:
-            send_reset_token(user)
+            send_reset_token.delay(user)
             flash(_("Email sent! Please check your inbox."), "info")
             flash(_("Email sent! Please check your inbox."), "info")
             return redirect(url_for("auth.forgot_password"))
             return redirect(url_for("auth.forgot_password"))
         else:
         else:
@@ -220,7 +220,7 @@ def request_activation_token(token=None):
     form = RequestActivationForm()
     form = RequestActivationForm()
     if form.validate_on_submit():
     if form.validate_on_submit():
         user = User.query.filter_by(email=form.email.data).first()
         user = User.query.filter_by(email=form.email.data).first()
-        send_activation_token(user)
+        send_activation_token.delay(user)
         flash(_("A new account activation token has been sent to "
         flash(_("A new account activation token has been sent to "
                 "your email address."), "success")
                 "your email address."), "success")
         return redirect(url_for("auth.activate_account"))
         return redirect(url_for("auth.activate_account"))

+ 5 - 3
flaskbb/email.py

@@ -16,6 +16,7 @@ from flaskbb.extensions import mail, celery
 from flaskbb.utils.tokens import make_token
 from flaskbb.utils.tokens import make_token
 
 
 
 
+@celery.task
 def send_reset_token(user):
 def send_reset_token(user):
     """Sends the reset token to the user's email address.
     """Sends the reset token to the user's email address.
 
 
@@ -38,6 +39,7 @@ def send_reset_token(user):
     )
     )
 
 
 
 
+@celery.task
 def send_activation_token(user):
 def send_activation_token(user):
     """Sends the activation token to the user's email address.
     """Sends the activation token to the user's email address.
 
 
@@ -61,8 +63,8 @@ def send_activation_token(user):
 
 
 
 
 @celery.task
 @celery.task
-def send_async_email(msg):
-    mail.send(msg)
+def send_async_email(*args, **kwargs):
+    send_email(*args, **kwargs)
 
 
 
 
 def send_email(subject, recipients, text_body, html_body, sender=None):
 def send_email(subject, recipients, text_body, html_body, sender=None):
@@ -79,4 +81,4 @@ def send_email(subject, recipients, text_body, html_body, sender=None):
     msg = Message(subject, recipients=recipients, sender=sender)
     msg = Message(subject, recipients=recipients, sender=sender)
     msg.body = text_body
     msg.body = text_body
     msg.html = html_body
     msg.html = html_body
-    send_async_email.delay(msg)
+    mail.send(msg)