2011-09-16 86 views
0

我嘗試從表單發送電子郵件會收到錯誤消息。我應該如何使用POST請求處理文件?我不需要將它寫入blobstore,只需發送電子郵件。如何使用GAE通過電子郵件發送附件?

模板:

<form method="POST" action="{{form_url}}" name="upload" enctype="multipart/form-data"> 
<table border="0"><tr><td colspan="2">  
<div class="labelform"> 
    </div> 
    <div><input type="hidden" id="lng" name="lng" size="35" maxlength="50" value="" /></div> 
<div class="labelform">  
    </div> 
    <div><input type="hidden" id="lat" name="lat" size="35" maxlength="50" value="" /> 
<input type="hidden" id="place" name="place" size="55" maxlength="55" value="" /> 
</div>   
    </td><td rowspan="9">  
    </td></tr>  
{% for field in form %}  
<tr><td> 
     <div class="labelform" style="display: block;"> 
     <label>{% trans "Type of problem" %}:</label> 
     </div> 
     </td><td> 
</label> <select name="subject" id="subject"> 
      <option value="{% trans "Problems with ads" %}" >{% trans "Problems with ads" %}</option> 
      <option value="{% trans "Advertising" %}" >{% trans "Advertising" %}</option> 
      <option value="{% trans "Images" %}" >{% trans "Images" %}</option> 
      <option value="{% trans "Our rules when advertising" %}" >{% trans "Our rules when advertising" %}</option> 
      <option value="{% trans "Technical problems" %}" >{% trans "Technical problems" %}</option> 
      <option value="{% trans "Other" %}" >{% trans "Other" %}</option>   
      </select> 
       </div> 
    </td></tr> 
<tr><td> 
    <div class="fieldWrapper"> 
     {{ form.name.errors }} 
     <label for="id_subject">{% filter capfirst %}{% trans "name" %}{% endfilter %}</label></td><td> 
     <div> 
    <input type="text" id="name" name="name" value="{{ user.nickname }}{% if not user %}{{ current_user.name|escape }}{% endif %}{% if not user and not current_user %}{% ifequal twittername None %}{% else %}{{ twittername }}{% endifequal %}{% endif %}" size="35" maxlength="50" /> 
    <div id="err_name" style="display: none;"> 
     <span class="warning" id="err_msg_name"></span> 
    </div> 
    </div></td></tr> 
    </div><tr><td> 
    <div class="fieldWrapper"> 
     {{ form.email.errors }} 
     <label for="id_sender">{% trans "E-mail address" %}</label></td><td> 
     {{ form.email }}</td></tr> 

    </div><tr><td valign="top"> 
    <div class="fieldWrapper"> 
     {{ form.text.errors }} 
     <label for="id_message">{% trans "Text" %}</label></td><td> 
     {{ form.text }}</td></tr> 
    </div> 
    </div> 
    <tr><td> </td><td> <input type="file" name="file" /><br>{% trans "If there is a problem with the images - upload them here" %}<br/> 
<input type="submit" value="Submit" /> </td></tr></table> 

代碼:

class ContactFileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):#to do:attachment 
    def post(self): 
     message = mail.EmailMessage(sender='[email protected]', subject=self.request.POST.get('subject'))#, attachments=[('test', self.request.POST.get('file').file.read()) ]) 
     message.body = ('%s \n%s \n%s \nhttp://...com/') % (self.request.POST.get('name'), self.request.POST.get('email'), self.request.POST.get('text')) 
     message.to='[email protected]' 
     message.send() 
     self.redirect('/customer_service.htm') 
+0

「獲取錯誤消息」不是很有幫助。請包括堆棧跟蹤。 –

回答

1

在GAE上載文件,你必須爲它寫Blob存儲區,所以你需要用它的工作這樣的代名詞。

from google.appengine.ext import blobstore 

upload_files = self.get_uploads('file') 
blob_info = upload_files[0] 
blob_reader = blobstore.BlobReader(blob_info.key()) 

message.attachments = [blob_info.filename,blob_reader.read()] 

一旦你完成,你總是可以刪除它。

blob = blobstore.BlobInfo.get(blob_info.key()) 
blob.delete() 
+0

它的工作原理。非常感謝 –

+1

不是問題:) –

+0

如果沒有必要存儲文件,是否有另一種方法來做到這一點,以便在附件之後自動刪除文件?刪除不必是即時的,我只是想找出一種不必明確刪除它的方法。另外,我應該使用HTML5文件上傳(很多基於jQuery的選擇來實現這一點),還是堅持傳統的文件上傳? –

相關問題