I have a blog post objects. After blog post created I have a signal to dispatch emails. And I want to send emails to multiple subscribers. I am using google mail as a mail server. I put in my settings file some attributes for using mail:
EMAIL_HOST = settings['EMAIL']['HOST']
EMAIL_PORT = settings['EMAIL']['PORT']
EMAIL_HOST_USER = settings['EMAIL']['USER']
EMAIL_HOST_PASSWORD = settings['EMAIL']['PASSWORD']
EMAIL_USE_TLS = True
And function to dispatch mails. Function is a celery task.
@shared_task
def email_dispatch(heading, text):
recipients = TheUser.objects.filter(subscription=True)
for recipient in recipients:
try:
html_content = render_to_string('mails/email_dispatch.html', {'text': text})
text_content = strip_tags(html_content)
subject = '{}'.format(heading)
email = EmailMultiAlternatives(subject, text_content, to=[recipient.id_user.email])
email.attach_alternative(html_content, 'text/html')
email.send()
logger.info('Successful processed "{}", sent to: "{}"'.format(count_processed, recipient.id_user.username))
except NoReverseMatch:
logger.info('Unexpected username: "{}"'.format(recipient.id_user.username))
logger.info('Email dispatching has been finished.')
At a first glance, all work okay. When celery task starts executing it dispatches correctly. But after approximately sent ~80 emails I receive
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Unfortunately, I didn't found any info about Google Mail restrictions in that case. Maybe I am setting something wrong from Django side or using an incorrect function for this case.
Thank you for any help.