How to limit GAE Emails to stay under email quotas

57 Views Asked by At

I'm working on an GAEJ application which will email out on both an event basis and on a regular basis.
My current problem is that I'm having difficulty thinking of the best way to keep a track of where I am with each quota. I had hoped to try and access appstats in my code to check the total number of mail.send calls in the last minute & that day but the documentation of that code is rather lacking.

Any advice would be appreciate, cheers.

2

There are 2 best solutions below

0
Ian Marshall On

One way could be to keep a count in the memory cache for each quota day. Each of these counts would need to be backed by a datastore sharded counter for when/if the memory cache counts are evicted.

1
David On

As Ian suggests, you could keep track of the number of mails sent through a counter. If there are numerous mails, consider using sharded counters.

Another option is to put the messages in a task queue, and to limit the output of the task queue to 100 messages per day (if you want to stay under the free quota). That way if a message cannot be sent today, it will be sent tomorrow.

To enforce another quota such as the 8 messages/minutes or the 8 attachements/minute, you can simply chain the task queues : a first one with a rate of 100/day, then another one with a rate of 32/min. That way you're in complete control of you quotas.

Caveats :

  • To match the way Google computes the quotas, you must create one task per recipient, so for one mail sent to 4 recipients, you will create 4 tasks
  • If you send too many mails, the task queues can become clogged (they empty more slowly than they are filled). So you have to watch the numbers of tasks.