如何使用 GAE 通过电子邮件发送表单附件?
我尝试从表单发送电子邮件时收到错误消息。我该如何处理带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 GAE 中,上传文件相当于将其写入 blobstore,因此您需要按原样使用它。
完成后您可以随时将其删除。
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.
Once you are done you can always delete it.