在App Engine中如何给电子邮件添加附件?

4

如何在使用Google应用引擎(Python)时将位于Web URL上的文件附加到电子邮件中?

假设我有一个位于以下URL的文件:http://www.abc.com/files/file.pdf

我想将其附加到电子邮件并发送给应用引擎上的用户。我该怎么做?

1个回答

6

要发送附件,您需要在电子邮件消息的附件字段中填写一个包含文件名和文件内容的两个值元组列表。详情请见此处

from google.appengine.api import urlfetch
from google.appengine.api import mail  

url = "http://www.abc.com/files/file.pdf" 
result = urlfetch.fetch(url)

if result.status_code == 200: 
  document = result.content

mail.send_mail(sender="youremail@yourdomain.com",
               to="receiver@hisdomain.com",
               subject="The file you wanted",
               body="Here is the file you wanted",
               attachments=[("The file name.pdf", document)])

如果您已经将PDF文件上传到项目中,如何添加附件? - Angry_Yodeler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接