2011-07-23 22 views
3

我想用我的gmail smtp和pyramid_mailer軟件包從我的金字塔網站發送電子郵件。首先,如果有人對電子郵件解決方案有其他建議,請告訴我!無法用pyramid_mailer和gmail發送電子郵件

添加以下到我的dev.ini:

mail.host = smtp.gmail.com 
mail.username = [email protected] 
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="[email protected]", 
         recipients=["[email protected]"], 
         body="hello!") 
mailer.send(message) 

不幸的是,我得到以下例外:

SMTPServerDisconnected: please run connect() first 

我在做什麼錯?

謝謝!

+0

不是一個答案,但你可以與其他SMTP服務器試試嗎? http://groups.google.com/group/comp.lang.python/browse_thread/thread/4791505038b2fca5 –

回答

4

以下設置爲我工作:

# pyramid_mailer 
mail.host = smtp.gmail.com 
mail.port = 587 
mail.username = [email protected] 
mail.password = mypassword 
mail.tls = True 

你的郵件發送代碼似乎是和我一樣,所以應該工作。

我還沒有嘗試SSL,但我假設各種bugaboos可能存在thataway。

+0

有一件事; Gmail更改了From:標頭,這是一個令人失望的標頭。 –

+0

Matt,你把它放在'server.main'下的'development.ini'配置文件中,是否有服務器信息?或者像'[app:main]'之類的東西?查看金字塔文檔: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)