如何使用GAE通过电子邮件发送表单中的附件?

时间:2011-09-16 13:21:33

标签: python forms http google-app-engine post

我尝试从表单发送电子邮件会收到错误消息。我应该如何使用POST请求处理文件?我不需要将它写入blobstore,只需通过电子邮件发送即可。

模板:

  <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='admin@appspotapp-id.com', 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='info@....com'
        message.send()
        self.redirect('/customer_service.htm')

1 个答案:

答案 0 :(得分:1)

在GAE中上传文件是将其写入blobstore的同义词,因此您需要使用它。

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()