emencia / emencia-django-newsletter

An app for sending newsletter by email to a contact list.
189 stars 72 forks source link

Server hard limit does not work for multiple newsletters #31

Closed qiemem closed 13 years ago

qiemem commented 13 years ago

When the server defaults to MAILER_HARD_LIMIT, it doesn't look at the emails it's already sent when returning credits(). Thus, if multiple newsletters are sent within an hour that collectively send more than MAILER_HARD_LIMIT, the limit will be exceeded even though none of the newsletters send more than MAILER_HARD_LIMIT individually. I think the SMTPServer.credits() function should read:

def credits(self):
    """Return how many mails the server can send"""
    if not self.mails_hour:
        allowed_mails = MAILER_HARD_LIMIT
    else:
        allowed_mails = self.mails_hour

    last_hour = datetime.now() - timedelta(hours=1)
    sent_last_hour = ContactMailingStatus.objects.filter(
        models.Q(status=ContactMailingStatus.SENT) |
        models.Q(status=ContactMailingStatus.SENT_TEST),
        newsletter__server=self,
        creation_date__gte=last_hour).count()
    return allowed_mails - sent_last_hour
Fantomas42 commented 13 years ago

Hi qiemem,

good point but the code is good.

The MAILER_HARD_LIMIT value is returned only if the self.mails_hour is set to 0. If the mails_hours is set to 0 this means that the server has no limit for sending emails, but using 0 will not allow you to send emails, that's why MAILER_HARD_LIMIT is set to a high value.

So the remaining credits are computed only if you set a positive value.

Regards.