gpt4 book ai didi

python - 如何从 Google App Engine 中的 InboundeMailHandler 读取电子邮件(自定义) header 和附件

转载 作者:行者123 更新时间:2023-12-01 05:54:26 24 4
gpt4 key购买 nike

如何在 Google App Engine 中读取电子邮件标题。我正在尝试在谷歌中搜索,但找不到任何好的且有效的示例。

最佳答案

答案在这里。
文件:app.yaml
(您将向 mymail@myapp.appspotmail.com 发送电子邮件)

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /_ah/mail/mymail@myapp\.appspotmail\.com
script: mail.app
login: admin

- url: .*
script: main.app

libraries:
- name: webapp2
version: "2.5.1"

inbound_services:
- mail

假设您的应用收到一封电子邮件,并且这是电子邮件 header 的一部分:

...
MIME-Version: 1.0
Received: by 10.220.68.18 with HTTP; Tue, 30 Oct 2012 21:18:18 -0700 (PDT)
Date: Wed, 31 Oct 2012 12:18:18 +0800
Message-ID: <CA+aGk47FkztZR3jUfrG=XU-ikLNnpEVE3KOcoXHgX1ntoBs1+A@mail.gmail.com>
Subject: Butter cake receipe
From: Someone somebody <someone@somebody.com>
To: Danny Hong <danny@otherone.com>
Content-Type: multipart/alternative; boundary=bcaxec5y19610f36df7204cd5331cd
X-Gm-Message-State: ALoCoQmzPBg3OYwtkdO5pUbNI5Zk+v2Qa42P5E6lWRzCiVUPBNsHbmSOJoBir2UfsUdq7vZLYnUOsHg
...

文件:mail.py

import logging
import webapp2
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class MailHandle(InboundMailHandler):
def receive(self, mail_message):

#Read headers
buf = mail_message.original.get('MIME-Version')
logging.info("I get: %s" % buf)
# output: i get: 2.0

buf = mail_message.original.get('Message-ID')
logging.info("I get: %s" % buf)
# output: i get: <CA+aGk47FkztZR3jUfrG=XU-ikLNnpEVE3KOcoXHgX1ntoBs1+A@mail.gmail.com>

buf = mail_message.original.get('X-Gm-Message-State')
logging.info("I get: %s" % buf)
# output: i get: ALoCoQmzPBg3OYwtkdO5pUbNI5Zk+v2Qa42P5E6lWRzCiVUPBNsHbmSOJoBir2UfsUdq7vZLYnUOsHg

buf = mail_message.original.get('Subject', 'no subject')
logging.info("I get: %s" % buf)
# output: i get: Butter cake receipe

buf = mail_message.original.get('something-else','no such header')
logging.info("I get: %s" % buf)
# output: i get: no such header

# Looping and getting attachments...
if hasattr(mail_message,'attachments'):
for fn, att in mail_message.attachments:
logging.info("Attachment filename is %s" % fn)
# output: Attachment file is <filename.xxx>
logging.info("Attactment data in blob %s" % att.decode())
# output: Attachment data in blob <binary>

关于python - 如何从 Google App Engine 中的 InboundeMailHandler 读取电子邮件(自定义) header 和附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13152995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com