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
mail_content += link;
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