My favorites | 中文(简体) | Sign in

附件

电子邮件可以包含文件附件。电子邮件的 [attachments] 字段接受二值元组列表(每个附件对应一个元组)。元组的第一个元素是用于邮件中的附件的文件名,第二个元素是数据(采用字节字符串形式)。

from google.appengine.api import mail
from google.appengine.ext import db

class DocFile(db.Model):
  doc_name = StringProperty()
  doc_file = BlobProperty()

q = db.GqlQuery("SELECT * FROM DocFile WHERE doc_name = :1", myname)
results = q.fetch(1)
if results:
  doc = results[0]
  mail.send_mail(sender="support@example.com",
                to="Albert Johnson <Albert.Johnson@example.com>",
                subject="The doc you requested",
                body="""
Attached is the document file you requested.

The example.com Team
""",
                attachments=[(doc.doc_name, doc.doc_file)])

只允许某些类型的文件作为附件。附件的文件名则说明该文件的类型。

允许的附件类型

为了安全起见,附加到电子邮件的文件必须是以下允许的文件类型,并且文件名必须以与文件类型对应的扩展名结尾。附件以文件扩展名决定的 MIME 类型发送。

以下是允许作为电子邮件文件附件的 MIME 类型及其对应的文件扩展名列表。

MIME 类型文件扩展名
图像/x-ms-bmpbmp
文本/csscss
文本/逗号分隔值csv
图像/gifgif
文本/htmlhtm、html
图像/jpegjpeg、jpg、jpe
应用程序/pdfpdf
图像/pngpng
应用程序/rss+xmlrss
文本/plaintext、txt、asc、diff、pot
图像/tifftiff、tif
图像/vnd.wap.wbmpwbmp