2017-10-17 239 views
2

我已經使用--enable_sendmail=yes選項啓用了sendmail,如the GAE docs中所述,並且仍然沒有收到任何電子郵件(雖然電子郵件確實出現在日誌消息中)。簡化的代碼如下所示:Sendmail不適用於本地GAE本地開發服務器

from google.appengine.api import mail 

SENDER_EMAIL_ADDRESS = "[email protected]" 

msg = "Test message" 

subject = "Test subject" 

recipient = "{} <{}>".format('username', '[email protected]') 

mail.send_mail(sender=SENDER_EMAIL_ADDRESS, 
       to=recipient, 
       subject=subject, 
       body=msg) 

我在做什麼錯?

回答

2

如果指定發件人地址,它必須與本地計算機相對應。在郵件存根sendmail的處理功能,谷歌提供了其開發服務器看起來像:

... 
try: 
    child.stdin.write(mime_message.as_string()) 
    child.stdin.close() 
... 

的問題是,傳遞給send_mail方法的發件人地址爲mime_message頭。將以下行添加爲try塊中的第一行,從MIME消息中刪除該頭。這允許sendmail使用默認的發件人地址:

mime_message._headers = [x for x in mime_message._headers if x[0] != 'From'] 

使用此功能的文件可以在/path/to/google_cloud_sdk/platform/google_appengine/google/appengine/api/mail_stub.py

找到
相關問題