Getting "SMTPServerDisconnected: Connection unexpectedly closed: [Errno 104] Connection reset by peer" errors when using django.core.mail.send_mail in the loop

756 views
Skip to first unread message

Haomin Zhang

unread,
Jun 28, 2016, 10:56:04 AM6/28/16
to Django users
Create a command like below, which send a mail to the user if the date meet the requirement:

class Command(BaseCommand):
    help= 'Send mail to user regularly if there have contacts needs reminder'
    
    def handle(self, *args, **options):
        now = timezone.now().date()
        backlogs = Backlog.objects.all()
        for backlog in backlogs.iterator():
            start_date = backlog.start_date
            end_date = backlog.end_date
            if start_date <= now and now < end_date:
                user = backlog.user
                user_email = user.email
                user_name = user.username
                if user_email is not None:
                    mail_title = u'Contracts need to be handling.'
                    mail_content = u"Dear:%s,you have messages" % user_name
                    link = u"http://xx.xxx.xx.xxx/contract-info/"
                    mail_content += link;
                    mail_from = 'from_...@xx.com'
                    mail_to = [user_email]
                    send_mail(mail_title,
                          mail_content,
                          mail_from,
                          mail_to)

This command can run well in local server. But when I launch it on the VPS and if call send_mail more than once, below mesasge appear:

"SMTPServerDisconnected: Connection unexpectedly closed: [Errno 104] Connection reset by peer"

But if only one backlog been filtered and only call once send_mail, it works well.

Does anybody met the issue before? thanks in advance!

-Haomin

Asif Khan

unread,
May 21, 2019, 9:21:38 AM5/21/19
to Django users
I am facing similar error while I am rendering html template to PDF by using xhtml2pdf. Same when my template have small number of rows it works perfect to render as pdf but when my result fetch mutiple table rows it start processing and after couple of duration give error [Errno 104] Connection reset by peer. 
Reply all
Reply to author
Forward
0 new messages