【问题标题】:Can't send emails with pyramid_mailer and gmail无法使用 pyramid_mailer 和 gmail 发送电子邮件
【发布时间】:2011-10-11 13:34:12
【问题描述】:

我正在尝试使用我的 gmail smtp 和 pyramid_mailer 包从我的金字塔网站发送电子邮件。首先,如果有人对电子邮件解决方案有其他建议,请告诉我!

我在 dev.ini 中添加了以下内容:

mail.host = smtp.gmail.com
mail.username = user@gmail.com
mail.password = password
mail.port = 465
mail.ssl = True

然后我像这样发送消息:

config.registry['mailer'] = Mailer.from_settings(settings)

后来……

mailer = request.registry['mailer']
message = Message(subject="hello world",
                      sender="admin@mysite.com",
                      recipients=["someaddress@gmail.com"],
                      body="hello!")
mailer.send(message)

不幸的是,我得到了以下异常:

SMTPServerDisconnected: please run connect() first

我做错了什么?

谢谢!

【问题讨论】:

标签: python smtp gmail pyramid


【解决方案1】:

以下设置对我有用:

# pyramid_mailer
mail.host = smtp.gmail.com
mail.port = 587
mail.username = my.login@gmail.com
mail.password = mypassword
mail.tls = True

您的邮件发送代码似乎与我的相同,所以应该可以。

我没有尝试过 SSL,但我假设各种各样的 bugaboos 都可能存在。

【讨论】:

【解决方案2】:

在事务提交之前不会真正发送电子邮件。

您应该提交交易:

import transaction
transaction.commit()

或使用 send_immediately:

mailer.send_immediately(message, fail_silently=False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 2017-01-16
    • 1970-01-01
    • 2018-08-12
    • 2021-07-06
    • 2011-10-16
    • 2015-10-24
    • 2018-11-08
    相关资源
    最近更新 更多