Search code examples
pythondjangosmtpgmailcelery

Gmail authentication error when sending email via django celery


TL;DR: I get a SMTPAuthenticationError from Gmail when trying to send emails using Celery/RabbitMQ tasks from my Django app, despite the credentials passed in are correct. This does not happen when the emails are sent normally, without Celery.

Hi,

I am trying to send email to a user asynchronously using Celery/RabbitMQ in my Django application. I have the following code to send the email:

from grad.celery import app
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context
from grad import gradbase
from time import sleep
from grad import settings

@app.task
def send_transaction_email(student_data, student_email):
    html_email = get_template('grad/student_email.html')
    context = Context({
        'name': student_data['name'],
        'university': student_data['university'],
        'data': student_data,
        'shorturl':  gradbase.encode_graduation_data(student_data)
    })
    msg = EmailMultiAlternatives('Subject Here',
                             'Message Here',
                             '[email protected]',
                             [student_email])
    msg.attach_alternative(html_email.render(context), "text/html")
    msg.send()

When I call the method normally:

tasks.send_transaction_email(entry, stud_email)

The emails are sent fine, but if I delegate the call to a Celery task like so:

tasks.send_transaction_email.delay(entry, stud_email)

I get the following trace:

Traceback (most recent call last):
File "/Users/albydeca/Gradcoin/venv/lib/python2.7/site-        packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/Users/albydeca/Gradcoin/venv/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/Users/albydeca/Gradcoin/grad/tasks.py", line 27, in   send_transaction_email
msg.send()
File "/Users/albydeca/Gradcoin/venv/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Users/albydeca/Gradcoin/venv/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/Users/albydeca/Gradcoin/venv/lib/python2.7/site- packages/django/core/mail/backends/smtp.py", line 69, in open
self.connection.login(self.username, self.password)
File   "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 622, in login
raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn    more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials q8sm53748398wjj.7 - gsmtp')

Basically Gmail does not recognise the credentials, despite when I print them literally on the two lines before the one that causes the crash, they are correct.

Can someone help me?


Solution

  • Try to make an app password here https://security.google.com/settings/security/apppasswords and use it as your password in the STMP settings.