Austin Bailey
Austin Bailey

Reputation: 55

Error sending email using Django with Celery

I'm trying to send emails and below works perfectly fine if executed through the web server. However, when I try to send the task to Celery, I always get an Assertion Error returned telling me that "to" needs to be a list or tuple.

I don't want the emails to be sent via the web server as it will slow things down so if anyone can help me fix this, that would be greatly appreciated.

from celery import Celery
from django.core.mail import send_mail, EmailMessage

app = Celery('tasks', backend='amqp', broker='amqp://')

@app.task
def send_mail_link():
    subject = 'Thanks'
    message = 'body'
    recipients = ['[email protected]']
    email = EmailMessage(subject=subject, body=message, from_email='[email protected]', to=recipients)
    email.send()

Upvotes: 1

Views: 1309

Answers (1)

Austin Bailey
Austin Bailey

Reputation: 55

I'm not 100% sure why, but I made some changes and it now works with no errors.

I removed the import for send_mail and changed the name of the method from send_mail_link() to send_link(). I also restarted the Celery worker and now everything works as it should.

New code is:

from celery import Celery
from django.core.mail import EmailMessage

app = Celery('tasks', backend='amqp', broker='amqp://')

@app.task
def send_link():
    subject = 'Thanks'
    message = 'body'
    recipients = ['[email protected]']
    email = EmailMessage(subject=subject, body=message, from_email='[email protected]', to=recipients)
    email.send()

Hopefully somebody in the future may find this helpful.

Upvotes: 1

Related Questions