无法使用pyramid_mailer和gmail发送电子邮件

3
我正在尝试使用我的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

我做错了什么?

谢谢!


不是答案,但你可以尝试使用另一个SMTP服务器吗?http://groups.google.com/group/comp.lang.python/browse_thread/thread/4791505038b2fca5 - Chris McDonough
2个回答

5
以下设置对我有用:

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

你的邮件发送代码似乎与我的相同,所以应该可以工作。
我没有尝试过SSL,但我假设那里可能存在各种问题。

有一件事,Gmail会更改发件人的头部信息,这真是令人沮丧。 - Matt Feifarek
Matt,你把这个放在development.ini配置文件里的[server:main]中,带服务器信息?还是类似[app:main]这样的位置?详见Pyramid文档:http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/project.html#development-ini - thesayhey

2

邮件在事务提交之前并不会实际发送。

您需要提交事务:

import transaction
transaction.commit()

或者使用 send_immediately:

mailer.send_immediately(message, fail_silently=False)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接