0

I'm trying to schedule an asynchronous email job. I'm using django-rq as the queueing mechanism. I've tried numerous fixes such as changing the password, ensuring that it's correct etc. I can manually sending SMTP via REPL.

Update #1: the settings are being being picked up correctly by the worker as far as I can ascertain. The job correctly picks up the correct value for settings.DEFAULT_FROM_EMAIL and has the correct email address set as an arg for send_mail.

The following code works successfully (no django-rq):

send_mail(
    newClaim.linkedOffer.commsPromoHeadline,
    msg_plain,
    settings.DEFAULT_FROM_EMAIL,
    [newRecipient.email],
    html_message=msg_html,
)

whilst the following code generates an SMTP authentication error (traceback lower down):

django_rq.enqueue(
   send_mail,
   newClaim.linkedOffer.commsPromoHeadline,
   msg_plain,
   settings.DEFAULT_FROM_EMAIL,
   [newRecipient.email],
   html_message=msg_html,
   )

traceback:

Traceback (most recent call last):
  File "/home/user1/webapps/dev_django_platform/ENV/lib/python2.7/site-packages/rq/worker.py", line 568, in perform_job
    rv = job.perform()
  File "/home/user1/webapps/dev_django_platform/ENV/lib/python2.7/site-packages/rq/job.py", line 495, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/message.py", line 286, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/backends/smtp.py", line 92, in send_messages
    new_conn_created = self.open()
  File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/backends/smtp.py", line 59, in open
    self.connection.login(self.username, self.password)
  File "/usr/local/lib/python2.7/smtplib.py", line 622, in login
    raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')

Why is is throwing an exception and how can I fix it?

AndrewO
  • 1,105
  • 4
  • 16
  • 33

1 Answers1

0

My assumption is the enqueued task is run on a separate worker environment. Inside Django, the send_mail function knows where to look up the Auth details from. But inside the worker, it can not find them, since it's a separate process.

You need to write your custom send_mail function which does not depend on Django to provide it's settings.

masnun
  • 11,635
  • 4
  • 39
  • 50
  • thanks, but the job seems to be picking up the settings correctly. For example, the one of the args above is settings.DEFAULT_FROM_EMAIL, which within the job is set to the value within the django settings file. I'll update the question with this information – AndrewO Jan 06 '16 at 21:15
  • We're not passing any authentication info to `send_email` - Django provides them the details, possibly even sets up the connections. The settings are being read when enqueuing the task, which is still inside django. – masnun Jan 06 '16 at 21:20
  • I understand what you're saying now. I've logged an issue here https://github.com/ui/django-rq/issues/148 – AndrewO Jan 06 '16 at 21:38