Google App Engine: mail quota limits and task queues

My application on Google App Engine recently hit a mail quota limit. Specifically, there is a quota limit on how many recipients the application can send emails to per minute. For the free version, this limit turned out to be quite low, namely eight.

The application in question has a scheduled task that once every day sends out an email which has a list of recipients. This list of recipients reached the aforementioned limit and the email sending failed. The log stated:

OverQuotaError: The API call mail.Send() required more quota than is available.

The solution to this was to change the email sending so that one email was generated per recipient, and the sending of the email was queued using the brilliant (however experimental) task queue functionality that GAE provides. The code for queuing looks like this:

queue = Queue('mail-queue')
for recipient in to:
    queue.add(Task(url='/task/mail', params= { 'to' : recipient, 'subject' : subject, 'body' : body }))

Here’s the actual task code that sends the email:

class MailSender(webapp.RequestHandler):
    def post(self):
        to = self.request.get('to')
        subject = self.request.get('subject')
        body = self.request.get('body')
        logging.info("Sending '%s' to %s" % (subject, to))
        mail.send_mail("not.the.real.sender@not.a.real.domain.com", to, subject, body)

Finally, I defined the ‘mail-queue’ in ‘queue.yaml’:

queue:
- name: mail-queue
  rate: 8/m

Works like a charm! For more information about task queuing, see http://code.google.com/intl/no/appengine/docs/python/taskqueue/overview.html