2012-06-20 15 views
1

新來這裏的金字塔,並試圖建立pyramid_mailer發送電子郵件給自己:pyramid_mailer不發送電子郵件?

我在development.ini:

mail.host = smtp.gmail.com 
mail.username = [email protected] 
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 protected]", 
         recipients=["[email protected]"], 
         body="Hey there!") 

    mailer.send(message) 

我失去了一些非常基本的嗎?

回答

3

實際上你錯過了一些基本的東西! :-)

.send()是一個延遲發送,它將消息添加到事務管理器。如果您未使用pyramid_tm,則在請求結束時不會發送郵件。交易電子郵件是很好的,因爲如果調用send()後代碼中出現異常,郵件將不會被髮送。

無論如何,讓代碼發送的方式是通過.send_immediately()

+0

哦,謝謝!但是,它似乎仍然不起作用):任何其他提示,因爲似乎沒有任何錯誤信息會導致我認爲它不起作用。我試圖發送一封電子郵件給我自己,如果這可以幫助 – zhuyxn

+0

當你說「似乎不工作」你試圖使用什麼方法? –

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 protected] 
mail.password = PASSWORD 
mail.port = 587 
mail.tls = True 

在你的設置:

config.include('pyramid_mailer') 

然後

mailer = get_mailer(request) 
message = Message(subject="It works!", 
        sender="[email protected]", 
        recipients=["[email protected]"], 
        body="Hey there!") 

mailer.send_immediately(message, fail_silently=False) 

如果沒有仍能正常工作,您可以使用

mail.debug啓用調試=真

應該轉儲標準輸出SMTP會話。如果有什麼不起作用。你會確切地知道爲什麼。如果一切都很好。

相關問題