Attaching Files to Email in App Engine?

2019-03-30 09:08发布

How do I attach a file located on Web URL to an email using google app engine (Python)?

I have the file located at say: http://www.abc.com/files/file.pdf

I want to attach this to an email and send it to a user on app engine. How do I do this?

1条回答
疯言疯语
2楼-- · 2019-03-30 09:19

To send attachment you have to fill the attachment field of an email message with a list of two-value tupless containing the file name and the file content. From here

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)])
查看更多
登录 后发表回答