Gmail authentication error when sending email via django celery
<p><strong>TL;DR</strong>: 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.</p>

<p>Hi,</p>

<p>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:</p>

<pre><code>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',
                             'myrealemailaddress@gmail.com',
                             [student_email])
    msg.attach_alternative(html_email.render(context), "text/html")
    msg.send()
</code></pre>

<p>When I call the method normally:</p>

<pre><code>tasks.send_transaction_email(entry, stud_email)
</code></pre>

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

<pre><code>tasks.send_transaction_email.delay(entry, stud_email)
</code></pre>

<p>I get the following trace:</p>

<pre><code>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')
</code></pre>

<p>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.</p>

<p>Can someone help me?</p>

<p>Try to make an app password here <a href="https://security.google.com/settings/security/apppasswords" rel="nofollow">https://security.google.com/settings/security/apppasswords</a> and use it as your password in the STMP settings. </p>

python django smtp gmail celery