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

发布于 2024-12-05 03:08:46 字数 4123 浏览 0 评论 0原文

我尝试从表单发送电子邮件时收到错误消息。我该如何处理带有 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='[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')

My attempt at emailing from a form gets an error message. How should I process the file with the POST request? I don't need to write it to the blobstore, just email it.

Template:

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

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-12-12 03:08:46

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

In GAE uploading a file as you have is synonymous with writing it to the blobstore so you need to work with it as such.

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

Once you are done you can always delete it.

blob = blobstore.BlobInfo.get(blob_info.key())
blob.delete()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文