Tuesday, June 28, 2016

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

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_email@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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1dc8ce0-6454-474e-b747-15d832b4ac3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment