GithubHelp home page GithubHelp logo

tgext.mailer's Introduction

About tgext.mailer

https://travis-ci.org/amol-/tgext.mailer.png https://coveralls.io/repos/amol-/tgext.mailer/badge.png

tgext.mailer is a TurboGears2 extension for sending emails with transaction manager integration. Whenever the transaction manager provided with TurboGears commits the transaction all the emails are sent, when the transaction is abort the mails are discarded.

While there are other extensions available for sending emails on TurboGears, like TurboMail. Those are not bound to the transaction manager, have a more complex structure or are not actively maintained anymore. This lead to the birth of tgext.mailer.

tgext.mailer code is adapted from the pyramid_mailer project which uses repoze.sendmail to perform actual email delivery.

Installing

tgext.mailer can be installed from pypi:

pip install tgext.mailer

should just work for most of the users.

Enabling

To enable tgext.mailer put inside your application config/app_cfg.py the following:

import tgext.mailer
tgext.mailer.plugme(base_config)

or you can use tgext.pluggable when available:

from tgext.pluggable import plug
plug(base_config, 'tgext.mailer')

tgext.mailer will then load the options from your application configuration file, refer to the Configuration File Options section for a complete list of available options.

Sending Emails

Sending emails is performed through the mailer object, each request has its own mailer object, which is in charge of sending only the emails of that request. If you don't pass any request to the get_mailer call, the application wide mailer is returned (Do not use it inside a request):

from tgext.mailer import get_mailer
mailer = get_mailer(request)

To send a message, you must first create a tgext.mailer.message.Message instance:

from tgext.mailer import Message

message = Message(subject="hello world",
                  sender="[email protected]",
                  recipients=["[email protected]"],
                  body="Hi John!")

The Message is then passed to the Mailer instance. You can either send the message right away:

mailer.send(message)

or add it to your mail queue (a maildir on disk):

mailer.send_to_queue(message)

If you want to send the email without registering it on the transaction manager, to make sure it gets sent even in case of transaction failures, use:

mailer.send_immediately(message)

Configuration File Options

The available settings are listed below. Place them under [app:main] in your configuration *.ini file.

Setting Default Description
mail.debugmailer False Store emails on disk for debugging
mail.host localhost SMTP host
mail.port 25 SMTP port
mail.username None SMTP username
mail.password None SMTP password
mail.tls False Use TLS
mail.ssl False Use SSL
mail.keyfile None SSL key file
mail.certfile None SSL certificate file
mail.queue_path None Location of maildir
mail.default_sender None Default from address
mail.debug 0 SMTP debug level

In test units that have to check for sent email, make sure to set mail.debugmailer to "dummy" it will save outgoing emails in mailer.output instead of actually sending them.

Transactions

If you are using transaction management then tgext.mailer will only send the emails (or add them to the mail queue) when the transactions are committed.

For example:

import transaction

from tgext.mailer.mailer import Mailer
from tgext.mailer import Message

mailer = Mailer()
message = Message(subject="hello world",
                  sender="[email protected]",
                  recipients=["[email protected]"],
                  body="Hi John!")

mailer.send(message)
transaction.commit()

The email is not actually sent until the transaction is committed.

Usually TurboGears will automatically commit the transaction for your at the end of the request so you don't need to explicitly commit or abort within code that sends mail. Instead, if an exception is raised, the transaction will implicitly be aborted and mail will not be sent; otherwise it will be committed, and mail will be sent.

If you use the Application wide email manager it is usually best practice to only use send_immediately method, to avoid registering the same mail manager in multiple transactions.

Attachments

Attachments are added using the tgext.mailer.message.Attachment class:

from tgext.mailer import Attachment
from tgext.mailer import Message

message = Message()

photo_data = open("photo.jpg", "rb").read()
attachment = Attachment("photo.jpg", "image/jpg", photo_data)

message.attach(attachment)

You can pass the data either as a string or file object, so the above code could be rewritten:

from tgext.mailer import Attachment
from tgext.mailer import Message

message = Message()

attachment = Attachment("photo.jpg", "image/jpg",
                        open("photo.jpg", "rb"))

message.attach(attachment)

A transfer encoding can be specified via the transfer_encoding option. Supported options are currently base64 (the default) and quoted-printable.

You can also pass an attachment as the body and/or html arguments to specify Content-Transfer-Encoding or other Attachment attributes:

from tgext.mailer import Attachment
from tgext.mailer import Message

body = Attachment(data="hello, arthur",
                  transfer_encoding="quoted-printable")
html = Attachment(data="<p>hello, arthur</p>",
                  transfer_encoding="quoted-printable")
message = Message(body=body, html=html)

tgext.mailer's People

Contributors

amol- avatar barjomet avatar githk avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

githk barjomet

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.