2011-04-01 39 views
0

我試圖從Google App Engine內發送電子郵件。從GAE發送郵件的困難

每當我嘗試發送時,都會收到「缺少主題錯誤」。

當我在開發服務器上運行代碼時,它似乎工作正常 - 控制檯輸出看起來正確,我被帶到我期待的頁面。但是,當我把它上傳並運行它,我得到:

Traceback (most recent call last): 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 636, in __call__ 
    handler.post(*groups) 
    File "/base/data/home/apps/spam-tool/1.349401522260793315/spam-tool.py", line 34, in post 
    body=cgi.escape(self.request.get('content'))) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py", line 297, in send_mail 
    message.send(make_sync_call) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py", line 799, in send 
    raise ERROR_MAP[e.application_error](e.error_detail) 
BadRequestError: Missing subject 

但絕對是個問題。

我正在使用的代碼是:

> message = mail.EmailMessage(sender="[email protected]>", 
      subject="test") 
     message.subject=self.request.get('content') 
     message.to = addr_to_send_to 
     message.body = self.request.get('content') 
     message.send() 

(是的,對象被設置兩次......我已經試過只在一個地方或其它設置它,既不工作。)

在此先感謝。

回答

0

我不知道它是什麼是扔你了...但發送者應該是這樣的:

sender = '<[email protected]>', 

你缺少了「<」發件人。有時它會在實際錯誤所在的位置下游引發錯誤。

此外,請確保[email protected]是已添加爲該應用程序的開發人員的Google帳戶。

+0

好的建議,但那是我已經有的 - 我應該編輯它來使它更清晰,以便在將它張貼在此處之前更改了這段代碼。 – user686830 2011-04-01 13:01:30

1

試試這個:

def email_member(request): 
    to_addr = request.POST.get('email_to') 

    if not mail.is_email_valid(to_addr): 
    # Return an error message... 
    pass 
    else: 
    # note we can also send html email, e.g. html='<html><head></head><body>Hello world</body><html>' 
    mail.send_mail(
     sender='[email protected]', 
     to=to_addr, 
     subject=request.POST.get('email_title'), 
     body=request.POST.get('email_body')) 

我建議你也將任務隊列中發送電子郵件,所以你可以設置這樣的事情,你可以通過在特有的參數,這樣你就不太可能被黑客攻破。

def mail_member_information(email_to, email_title, email_body): 
    taskqueue.add(
    url = 'email_message', 
    params = 
    { 
     'email_to': email_to, 
     'email_title': email_title, 
     'email_body': email_body, 
     'valid_guid': 'ae9e34ca-a2c5-476e-b8be-72d807e3dc6b' 
    } 
) 

如果你只是想發送給管理員,然後使用此:

mail.send_mail_to_admins(sender='[email protected]', subject=request.POST.get('email_title'), body=request.POST.get('email_body'), html=request.POST.get('email_body'))

0

我認爲你可以這樣發,確保發送者是應用程序的管理

from google.appengine.api import mail 
mail.send_mail(sender="[email protected]", 

       to=email_valid.email, 
       subject="Please validate your email", 
       body=body_text, 
       html=htmlbody_text) 
+0

你可以 - 我試過這種方式,但它不起作用,所以我嘗試了更多的面向對象的方式,我發佈它以防修正事情。它沒有。 – user686830 2011-04-01 13:03:46

+0

@svaha你能告訴我什麼是你嘗試上面的代碼時的錯誤 – 2011-04-06 04:26:54