Skip to content

Instantly share code, notes, and snippets.

@archsh
Created June 21, 2014 01:28
Show Gist options
  • Save archsh/534d57f5a3cad6dd59de to your computer and use it in GitHub Desktop.
Save archsh/534d57f5a3cad6dd59de to your computer and use it in GitHub Desktop.
Hints forTurboGears
在TurboGears2中使用TurboMail发送邮件
1、安装TurboMail:
pip install turbomail
或者在项目的setup.py中添加依赖,然后运行setup.py develop
2、在项目lib中增加一个模块文件: tm_tg2.py:
# encoding: utf-8
"""TurboGears automatic startup/shutdown extension."""
from tg import config
from turbomail.control import interface
__all__ = ['start_extension', 'shutdown_extension']
def start_extension():
# interface.start will exit immediately if TurboMail is not enabled.
interface.start(config)
def shutdown_extension():
interface.stop()
3、更改项目中lib/app_globals.py, 在class Globals的__init__中增加:
from . import tm_tg2
tm_tg2.start_extension()
4、更改配置文件:development.ini:
在[app:main]段增加:
# SMTP Configuration
mail.on = true
mail.manager = immediate
mail.brand =
mail.transport = smtp
mail.smtp.server = (SMTP SERVER)
mail.smtp.username = (USERNAME)
mail.smtp.password = (PASSWORD)
mail.encoding = utf-8
mail.utf8qp.on = true
5、在你的代码中发送email:
from turbomail import Message
message = Message("from@example.com", "to@example.com", "Hello World")
message.plain = "TurboMail is really easy to use."
message.send()
6、注:这个发送email的操作是同步的操作,如果需要异步操作,需更改为一部操作,比如使用tgext.asyncjob。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment