pyramid_mailer 不发送电子邮件?

zhu*_*yxn 2 python email pyramid

Pyramid 新手,并尝试设置 pyramid_mailer 向自己发送电子邮件:

我在 development.ini 中有:

mail.host = smtp.gmail.com
mail.username = EMAIL@gmail.com
mail.password = PASSWORD
mail.port = 587
mail.ssl = True
[handlers]
keys = console
Run Code Online (Sandbox Code Playgroud)

在我的项目/__init__.py 中:

config.include('pyramid_mailer')
Run Code Online (Sandbox Code Playgroud)

在我的项目/views.py

from pyramid_mailer.mailer import Mailer
from pyramid_mailer import get_mailer
from pyramid_mailer.message import Message
Run Code Online (Sandbox Code Playgroud)
@view_config(renderer="templates/site_view.pt")
def site_view(self):
Run Code Online (Sandbox Code Playgroud)

...

    config.registry['mailer'] = Mailer.from_settings(settings)
    mailer = request.registry['mailer']
    message = Message(subject="It works!",
                      sender="EMAIL@gmail.cm",
                      recipients=["EMAIL@gmail.com"],
                      body="Hey there!")

    mailer.send(message)
Run Code Online (Sandbox Code Playgroud)

我在这里错过了一些非常基本的东西吗?

Mic*_*kel 5

事实上,你缺少一些基本的东西!:-)

.send()是将消息添加到事务管理器的延迟发送。如果您不使用,pyramid_tm则不会在请求结束时发送邮件。事务性电子邮件很好,因为如果在调用 之后在您的代码中引发异常send(),则不会发送邮件。

无论如何,让您的代码发送的方法是通过.send_immediately().