- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有人处理过收到的电子邮件中的附件吗?我在想,与其让用户上传图片,不如让他们将其作为附件发送,我可以使用它上传到数据存储区。
文档有 sending attachments但我找不到任何关于接收附件的文档。 This page says :
attachments
The file attachments for the message, as a list of two-value tuples, one tuple for each attachment.
Each tuple contains a filename as the first element, and the file contents as the second element.
An attachment file must be one of the allowed file types, and the filename must end with an extension that corresponds with the type. For a list of allowed types and filename extensions, see Overview: Attachments.
我认为这也是关于发送电子邮件。
我有这段代码可以将图像保存到数据存储区:
class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", emailAddress)
qTable = q.fetch(10)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 50, 50)
row.avatar = db.Blob(avatar)
db.put(qTable)
else:
self.response.out.write("user not found")
self.redirect('/')
直觉上,似乎message.attachment
而不是 "img"
会成功的。
avatar = images.resize(self.request.get(message.attachment), 50, 50)
你怎么看?谢谢。
Update2(新代码作为对 Nick Johnson 评论的回应)
class Register(InboundMailHandler):
def receive(self, message):
senderEmail = message.sender
emailTuple = parseaddr(senderEmail)
emailUserName = emailTuple[0]
emailAddress = emailTuple[1]
newAvatar = db.Blob(images.resize(goodDecode(message.attachments[0][1]), 50, 50))
newUser = User(userEmail=emailAddress,
userName=emailUserName,
avatar=newAvatar)
db.put(newUser)
Update1 问题已解决:
作为记录,对于任何有相同问题的人,请注意 attribute of message是attachments
不是attachment
:
message.attachment
给出 AttributeError
AttributeError: 'InboundEmailMessage' object has no attribute 'attachment'
和对象 message.attachment
看起来像这样:
[('portrait.png', <EncodedPayload payload=#8461006914571150170 encoding=base64>)]
所以拉<EncodedPayload payload=#8461006914571150170 encoding=base64>
的正确方法部分是
avatar = images.resize(goodDecode(message.attachments[0][1]), 50, 50)
我发布的原始代码有
avatar = images.resize(goodDecode(message.attachments[1]), 50, 50)
这显然行不通。
再次感谢 jesmith
和 Robert Kluin
寻求答案。
Update0(关于 jesmith
的回答)
就我而言,我正在拍摄图像 "img"
从用户上传的表单并将其写入数据存储,如下所示:
for row in qTable:
avatar = images.resize(self.request.get("img"), 50, 50)
row.avatar = db.Blob(avatar)
db.put(qTable)
self.redirect('/')
else:
logging.info("else user not found")
self.redirect('/user-not-found')
在您的代码中,这对应于本节,我相信:
try:
if hasattr(message, "attachment"):
for a in message.attachments:
msg.attachmentNames.append(a[0])
msg.attachmentContents.append(append(db.Blob(goodDecode(a[1])))
msg.put()
except:
logging.exception("exception decoding attachments in email from %s" % message.sender)
假设,就我而言,只有 1 个附件;如何获取附件的数据部分?
是message.attachment[1]
吗?
avatar = images.resize(message.attachment[1], 50, 50)
是message.attachment[1]
附件的数据部分?
谢谢!
最佳答案
这是我使用的传入邮件处理程序的片段:
bodies = message.bodies(content_type='text/html')
allBodies = u"";
for body in bodies:
allBodies = allBodies + u"\n" + unicode(goodDecode(body[1]), errors="ignore")
if not allBodies:
bodies = message.bodies(content_type='text/plain')
for body in bodies:
allBodies = allBodies + u"\n" + unicode(goodDecode(body[1]), errors="ignore")
msg = EmailMessageModel()
...fill in various stuff...
msg.sender = message.sender
msg.date = datetime.datetime.now()
msg.message = allBodies
# Calling put() before dealing with attachments because it seems like that could throw various exceptions
msg.put()
event.email = True
event.put()
event.project.email = True
event.project.put()
# attachments is a list of element pairs containing file names and contents.
try:
if hasattr(message, 'attachments'):
for a in message.attachments:
msg.attachmentNames.append(a[0])
msg.attachmentContents.append(db.Blob(goodDecode(a[1])))
msg.put()
except:
logging.exception("Exception decoding attachments in email from %s" % message.sender)
请注意,goodDecode 是我编写的一个函数,因为底层 GAE 解码中存在一个错误(它将所有内容小写,从而破坏了 base64 编码的文本):
def goodDecode(encodedPayload):
if not hasattr(encodedPayload, 'encoding'):
return encodedPayload
encoding = encodedPayload.encoding
payload = encodedPayload.payload
if encoding and encoding.lower() != '7bit':
payload = payload.decode(encoding)
return payload
这可能不再需要了,因为我很确定他们修复了那个错误。
在我的例子中,我将附件填充到数据库中:
class EmailMessageModel(db.Model):
....various stuff...
sender = db.StringProperty()
date = db.DateTimeProperty()
message = db.TextProperty()
attachmentNames = db.StringListProperty()
attachmentContents = db.ListProperty(db.Blob)
当我想显示这封电子邮件时,我正在使用:
<h2>{{ e.sender }} {{ e.date|date:"M j, Y f A " }} GMT</h2>
<p>From: {{ e.sender }}<br/>Date: {{ e.date|date:"M j, Y f A" }} GMT ({{ e.date|timesince }} ago)<br/>Subject: {{ e.subject }}</p>
{% if e.attachmentNames %}
<p>Attachments:
{% for a in e.attachmentNames %}
<a href="/admin/attachment?email={{ e.key }}&index={{ forloop.counter0 }}" target="_blank">{{ a }}</a>
{% endfor %}
</p>
{% endif %}
<div style='background-color: white'>{{ e.message }}</div>
附件处理程序是:
class AttachmentHandler(webapp.RequestHandler):
def get(self):
email = EmailMessageModel.get(self.request.get('email'))
index = self.request.get('index')
if index:
index = int(index)
filename = email.attachmentNames[index]
self.response.headers['Content-Type'] = str(mimetypes.guess_type(filename)[0]) or 'application/octet-stream'
self.response.out.write(email.attachmentContents[index])
(所以,基本上,我让浏览器弄清楚如何处理附件。)
希望这对您有所帮助!
关于python - 写入和图像附件作为头像 : Is it possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357022/
我正在尝试学习 Knockout 并尝试创建一个照片 uploader 。我已成功将一些图像存储在数组中。现在我想回帖。在我的 knockout 码(Javascript)中,我这样做: 我在 Jav
我正在使用 php 编写脚本。我的典型问题是如何在 mysql 中添加一个有很多替代文本和图像的问题。想象一下有机化学中具有苯结构的描述。 最有效的方法是什么?据我所知,如果我有一个图像,我可以在数据
我在两个图像之间有一个按钮,我想将按钮居中到图像高度。有人可以帮帮我吗? Entrar
下面的代码示例可以在这里查看 - http://dev.touch-akl.com/celebtrations/ 我一直在尝试做的是在 Canvas 上绘制 2 个图像(发光,然后耀斑。这些图像的链接
请检查此https://jsfiddle.net/rhbwpn19/4/ 图像预览对于第一篇帖子工作正常,但对于其他帖子则不然。 我应该在这里改变什么? function readURL(input)
我对 Canvas 有疑问。我可以用单个图像绘制 Canvas ,但我不能用单独的图像绘制每个 Canvas 。- 如果数据只有一个图像,它工作正常,但数据有多个图像,它不工作你能帮帮我吗? va
我的问题很简单。如何获取 UIImage 的扩展类型?我只能将图像作为 UIImage 而不是它的名称。图像可以是静态的,也可以从手机图库甚至文件路径中获取。如果有人可以为此提供一点帮助,将不胜感激。
我有一个包含 67 个独立路径的 SVG 图像。 是否有任何库/教程可以为每个路径创建单独的光栅图像(例如 PNG),并可能根据路径 ID 命名它们? 最佳答案 谢谢大家。我最终使用了两个答案的组合。
我想将鼠标悬停在一张图片(音乐专辑)上,然后播放一张唱片,所以我希望它向右移动并旋转一点,当它悬停时我希望它恢复正常动画片。它已经可以向右移动,但我无法让它随之旋转。我喜欢让它尽可能简单,因为我不是编
Retina iOS 设备不显示@2X 图像,它显示 1X 图像。 我正在使用 Xcode 4.2.1 Build 4D502,该应用程序的目标是 iOS 5。 我创建了一个测试应用(主/细节)并添加
我正在尝试从头开始以 Angular 实现图像 slider ,并尝试复制 w3school基于图像 slider 。 下面我尝试用 Angular 实现,谁能指导我如何使用 Angular 实现?
我正在尝试获取图像的图像数据,其中 w= 图像宽度,h = 图像高度 for (int i = x; i imageData[pos]>0) //Taking data (here is the pr
我的网页最初通过在 javascript 中动态创建图像填充了大约 1000 个缩略图。由于权限问题,我迁移到 suPHP。现在不用标准 标签本身 我正在通过这个 php 脚本进行检索 $file
我正在尝试将 python opencv 图像转换为 QPixmap。 我按照指示显示Page Link我的代码附在下面 img = cv2.imread('test.png')[:,:,::1]/2
我试图在这个 Repository 中找出语义分割数据集的 NYU-v2 . 我很难理解图像标签是如何存储的。 例如,给定以下图像: 对应的标签图片为: 现在,如果我在 OpenCV 中打开标签图像,
import java.util.Random; class svg{ public static void main(String[] args){ String f="\"
我有一张 8x8 的图片。 (位图 - 可以更改) 我想做的是能够绘制一个形状,给定一个 Path 和 Paint 对象到我的 SurfaceView 上。 目前我所能做的就是用纯色填充形状。我怎样才
要在页面上显示图像,你需要使用源属性(src)。src 指 source 。源属性的值是图像的 URL 地址。 定义图像的语法是: 在浏览器无法载入图像时,替换文本属性告诉读者她们失去的信息。此
**MMEditing是基于PyTorch的图像&视频编辑开源工具箱,支持图像和视频超分辨率(super-resolution)、图像修复(inpainting)、图像抠图(matting)、
我正在尝试通过资源文件将图像插入到我的程序中,如下所示: green.png other files 当我尝试使用 QImage 或 QPixm
我是一名优秀的程序员,十分优秀!