Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More granular control of how many emails to send. #156

Open
Hafnernuss opened this issue Nov 26, 2021 · 1 comment
Open

More granular control of how many emails to send. #156

Hafnernuss opened this issue Nov 26, 2021 · 1 comment

Comments

@Hafnernuss
Copy link

I just stumbled across this project and it seems to do everything I want, including some sort of rate limiting.
Currently I have to live with the fact that I can only send a few thousand emails per day.
Typically, most emails will have to be sent during business hours (different timezones are not an issue for now), and outside of those there wont be much to do, except an occasional password reset.

Using something like django-celery-beat I would setup a few periodic tasks that run every minute (one from midnight to 0600, one from 0600 till 1800 and one from 1800 till midnight) and adjust the admount of emails sent by each task. (e.g. the one from 1800 until midnight can only send one per minute, while another might send 5 per minute.

The three config parameters

MAILER_EMAIL_MAX_BATCH = None  # integer or None
MAILER_EMAIL_MAX_DEFERRED = None  # integer or None
MAILER_EMAIL_THROTTLE = 0  # passed to time.sleep()

seem quite helpful, but it would be great if I could pass a number of emails that should be sent to one of the management commands.

@Hafnernuss
Copy link
Author

Hafnernuss commented Nov 27, 2021

Regarding the usage of celery, maybe this would be a nice small addition to the docs:

In your apps task.py file, add the following:

from celery import shared_task

from mailer.management.commands.send_mail import send_all

@shared_task
def sendMails():
    send_all()

This seems to work without any issues, although I am not a 100% sure if it's a clean way of doing this.

Regarding the three config parameters for rate limiting, after closer inspection, I think I have an idea on how to achieve "dynamic" rate limiting based on time of day. Consider the following:

@shared_task
def sendMails():
  hour = timezone.now().hour  
  if hour >= 8 and hour <= 18:
    setattr(settings, "MAILER_EMAIL_MAX_BATCH", 20)
  else:
    setattr(settings, "MAILER_EMAIL_MAX_BATCH", 10)

  send_all()

However, this seems at least a bit hacky to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant