emailqueue

Non-blocking Email Queue for Tornado Web Framework

Download .zip Download .tar.gz View on GitHub

Tornado Email Queue

Tornado Email Queue is a non-blocking email queue for Python Tornado. This library uses MongoDB to store the queue data.

Installation

pip install emailqueue

Dependencies

Tornado Email Queue requires the environments officially supported by Tornado and MongoDB.

Requirements:

  • MongoDB
  • Python 2.7
  • tornado >= 4.2.1
  • motor >= 0.4.1
  • bson >= 0.4.0
  • futures >= 3.0.3

Examples

Here is a simple example of a Tornado web app with an email queue instance:

.. code-block:: python

from tornado import httpserver, ioloop, web
from tornado.options import options
from emailqueue.queues import MongoDbEmailQueue

smtp_user = 'sender@gmail.com'
smtp_password = ''
recipient_addrs = 'recipient@hotmail.com'

# Create the email queue.
email_queue = MongoDbEmailQueue(
    smtp_host='smtp.gmail.com',
    smtp_port=587,
    smtp_user=smtp_user,
    smtp_password=smtp_password,
    mongodb_uri='mongodb://localhost:27017',
    mongodb_collection='email_queue',
    mongodb_database='sample_db',
    queue_maxsize=0
)


class MainHandler(web.RequestHandler):

    def get(self):

        # Put new email in the queue for processing
        # by the mailer worker at the scheduled time.
        email_queue.sendmail(
            subject='Hello World Sample',
            from_addr=smtp_user,
            to_addrs=recipient_addrs,
            text='Greetings from Tornado Email Queue.',
            mime_type='plain',
            charset='utf-8',
            scheduled_hours_from_now=0
        )

        # Render a tornado html template and put it in the queue
        # for processing by the mailer worker at the scheduled time.
        email_queue.sendmail_from_template(
            subject='Tornado Templates Sample',
            from_addr=smtp_user,
            to_addrs=recipient_addrs,
            template_path='templates/email.html',
            params={'link': 'http://ricardoyorky.github.io/emailqueue/'},
            mime_type='html',
            charset='utf-8',
            scheduled_hours_from_now=0
        )

        self.write("Mail sent")


def main():
    options.parse_command_line()
    application = web.Application([
        (r"/", MainHandler),
    ])

    # Add email queue callback to IOLoop
    ioloop.IOLoop.current().add_callback(email_queue.worker)

    http_server = httpserver.HTTPServer(application)
    http_server.listen(8888)
    ioloop.IOLoop.current().start()


if __name__ == "__main__":
    main()

License

The MIT License (MIT)