【问题标题】:pyramid_mailer not sending emails?pyramid_mailer 不发送电子邮件?
【发布时间】:2013-02-13 15:12:04
【问题描述】:

这里是 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

在我的项目/__init__.py:

config.include('pyramid_mailer')

在我的项目/views.py中

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

...

    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)

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

【问题讨论】:

    标签: python email pyramid


    【解决方案1】:

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

    .send() 是一个惰性发送,它将消息添加到事务管理器。如果您没有使用pyramid_tm,则在请求结束时不会发送邮件。事务性电子邮件很好,因为如果在调用 send() 后代码中出现异常,则不会发送邮件。

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

    【讨论】:

    • 哦,谢谢!但是,它似乎仍然不起作用):任何其他提示,因为似乎没有任何错误消息会导致我认为它不起作用。如果有帮助的话,我正在尝试给自己发送一封电子邮件
    • 当您说“似乎不起作用”时,您尝试使用什么方法?
    【解决方案2】:

    您可能需要检查并使用:

    mail.tls = True
    

    Can't send emails with pyramid_mailer and gmail

    你也可以使用.send_immediately(message, fail_silently=False)

    你会有类似的东西:

    mail.host = smtp.gmail.com
    mail.username = EMAIL@gmail.com
    mail.password = PASSWORD
    mail.port = 587
    mail.tls = True
    

    在您的设置中:

    config.include('pyramid_mailer')
    

    然后

    mailer = get_mailer(request)
    message = Message(subject="It works!",
                      sender="EMAIL@gmail.cm",
                      recipients=["EMAIL@gmail.com"],
                      body="Hey there!")
    
    mailer.send_immediately(message, fail_silently=False)
    

    如果仍然无法正常工作,您可以使用

    启用调试

    mail.debug = True

    它应该在标准输出中转储 smtp 会话。如果某事没有奏效。你会确切地知道为什么。如果一切正常。

    【讨论】:

      猜你喜欢
      • 2011-10-11
      • 2011-09-16
      • 1970-01-01
      • 2015-03-09
      • 2014-08-17
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多