Created
January 23, 2011 22:50
-
-
Save shazow/792538 to your computer and use it in GitHub Desktop.
My TurboMail integration with Pylons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""The application's Globals object""" | |
from beaker.cache import CacheManager | |
from beaker.util import parse_cache_config_options | |
from myproject.lib.email import TurboMailer | |
class Globals(object): | |
"""Globals acts as a container for objects available throughout the | |
life of the application | |
""" | |
def __init__(self, config): | |
"""One instance of Globals is created during application | |
initialization and is available during requests via the | |
'app_globals' variable | |
""" | |
self.cache = CacheManager(**parse_cache_config_options(config)) | |
# You can overwrite these things in your .ini | |
# For debug, use mail.transport = debug | |
self.mailer = TurboMailer({ | |
'mail.transport': config.get('mail.transport', 'smtp'), | |
'mail.smtp.server': config.get('mail.server', 'localhost'), | |
'mail.smtp.username': config.get('mail.username'), | |
'mail.smtp.password': config.get('mail.password'), | |
'mail.utf8qp.on': True, | |
'mail.smtp.tls': True, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from turbomail.control import interface | |
from turbomail import Message | |
import atexit | |
__all__ = ['TurboMailer'] | |
FROM_NAME = 'change this' | |
FROM_EMAIL = 'noreply@changethis' | |
class Mailer(object): | |
""" | |
This object is intended to be instantiated on a global-like context, | |
as it will spawn threads and we don't want to do that per-call. | |
""" | |
pass | |
class TurboMailer(Mailer): | |
""" | |
TurboMail-specific details here: | |
http://www.python-turbomail.org/docs/chapters/detailed.html | |
http://packages.python.org/TurboMail/ | |
""" | |
def __init__(self, config=None): | |
self.config = { | |
'mail.on': True, | |
'mail.transport': 'smtp', | |
'mail.smtp.server': 'localhost', | |
'mail.message.encoding': 'UTF-8', | |
} | |
self.config.update(config) | |
def shutdown(): | |
interface.stop() | |
atexit.register(shutdown) | |
interface.start(self.config) | |
def message(self, email, subject, plain, rich=None): | |
m = Message( | |
author=(FROM_NAME, FROM_EMAIL), | |
to=email, | |
subject=subject) | |
m.plain = unicode(plain) | |
if rich: | |
m.rich = rich | |
m.send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Blah blah blah | |
app_globals.mailer.message(email, "Password reset", "Go here to reset your password:\n\r{0}".format(reset_url)) | |
# For bonus awesomeness, use mako to make email templates and render them into the body. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment