TurboMail 3 与 Pylons 1.0 - MailNotEnabledException
我正在尝试将TurboMail 3与Pylons 1.0一起设置。
我按照这里的文档进行了操作。
我已经把这些内容添加到了development.ini文件中:
[DEFAULT]
...
mail.on = true
mail.manager = immediate
mail.transport = smtp
mail.smtp.server = localhost
而我的app_globals.py文件看起来是这样的:
"""The application's Globals object"""
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
class Globals(object):
def __init__(self, config):
self.cache = CacheManager(**parse_cache_config_options(config))
from turbomail.adapters import tm_pylons
tm_pylons.start_extension()
我的控制器里有这个方法:
def submit(self):
message = Message("from@example.com", "to@example.com", "Hello World")
message.plain = "TurboMail is really easy to use."
message.send()
问题是,当我调用message.send()时,出现了这个错误:
MailNotEnabledException: An attempt was made to use a facility of the TurboMail framework but outbound mail hasn't been enabled in the config file [via mail.on]
我不知道我缺少了什么? 根据文档来看,一切都没问题啊!
谢谢
1 个回答
3
Pylons 1.0对配置存储的方式和时机做了一些不兼容的改动。简单来说,以前在创建全局对象的时候会自动加载配置,但现在不是这样了。你需要把你的代码改成下面这样:
import atexit
from turbomail import interface
from turbomail.adapters import tm_pylons
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
class Globals(object):
def __init__(self, config):
self.cache = CacheManager(**parse_cache_config_options(config))
atexit.register(tm_pylons.shutdown_extension)
interface.start(tm_pylons.FakeConfigObj(config))
上面的代码(atexit和interface.start)正是start_extension()这个函数所做的事情。
我会发布一个更新版的TurboMail,让你可以把配置作为参数传递给start_extension(),这样做会更清晰明了。