dstegelman/django-mail-queue — pull request #12 by rspeed (2013-02-19 21:55:07+00:00)
Title: Django management command for sending queued messages

## Purpose

I like this project, but not the idea of running scheduled tasks through the web server. So I added a management command to bypass it.

## Changes:

1. Added a management command called send_queued_messages which (can you guess?) sends queued messages.
2. Replaced a hardcoded value with a setting called MAILQUEUE_LIMIT, which limits the number of queued emails to send at once.
3. Fixed a small bug that raised a warning when time zone support is enabled.

## Details

This command can help make cronjobs more obvious and less prone to random errors caused by networking glitches. Plus, if logging is ever added it should make problems easier to trace. To avoid duplication I created a custom model manager containing a method called send_queued, and then called that from both the view and management command.

Running the command is straightforward:

``` bash
python manage.py send_queued_messages
```

It also includes an option to override the default limit for the number of messages to send.

``` bash
python manage.py send_queued_messages --limit=10
````

That brings me to item 2, which is that users can now add a MAILQUEUE_LIMIT setting. If it isn't set it defaults to 30, which is the current hardcoded value. Using the limit flag overrides this setting.

And finally, I fixed a small bug that raised a timezone warning when the last_attempt property was set. DateTimeField expects values to have a timezone when timezone support is enabled. When one isn't present it just assumes the local time zone and stores it as UTC (which is correct), but raises a warning due to that assumption. From the web site it's invisible, but it gets output on the command line, which can mess with cronjobs. The fix is to generate a datetime object using utcnow() instead of now(), then explicitly its timezone as UTC.

## Note

I haven't tested any of this in a production environment yet, nor did I add any unit tests.