gpt4 book ai didi

python - 谷歌应用引擎: receive email then/and forward it?

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:32 26 4
gpt4 key购买 nike

我有一个应用程序引擎应用程序,它接收电子邮件并将其元数据发送到脚本进行处理。效果很好。代码如下,由 Google 通过 Github 示例提供。

但是,问题是,如何将这个脚本(或一般的 App Engine)收到的电子邮件转发到另一个电子邮件地址?

特别是,对于使用 Gmail 向我转发电子邮件的客户,我希望收到他们的转发请求,以便我作为人工管理员可以批准这些请求。

import webapp2
import logging
import urllib
import json

from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.api import urlfetch


class HandleEmail(InboundMailHandler):
def receive(self, message):

# parse out fields
to = message.to
sender = message.sender
cc = getattr(message, 'cc', '')
date = message.date
subject = message.subject

# Original message, as a python email.message.Message
original = str(message.original)

html_body = ''
for _, body in message.bodies('text/html'):
html_body = body.decode()

plain_body = ''
for _, plain in message.bodies('text/plain'):
plain_body = plain.decode()

# Attachements are EncodedPayload objects, see
# https://code.google.com/p/googleappengine/source/browse/trunk/
# python/google/appengine/api/mail.py#536
attachments = [{
'filename': attachment[0],
'encoding': attachment[1].encoding,
'payload': attachment[1].payload
}
for attachment
in getattr(message, 'attachments', [])]

# logging, remove what you find to be excessive
logging.info('sender: %s', sender)
logging.info('to: %s', to)
logging.info('cc: %s', cc)
logging.info('date: %s', date)
logging.info('subject: %s', subject)
logging.info('html_body: %s', html_body)
logging.info('plain_body: %s', plain_body)
logging.info('attachments: %s', [a['filename'] for a in attachments])

# POST (change to your endpoint, httpbin is cool for testing though)
# url = 'http://httpbin.org/post'
url = 'https://myapp.appspot.com/receiver.php'

form_fields = urllib.urlencode({
'sender': sender.encode('utf8'),
'to': to.encode('utf8'),
'cc': cc.encode('utf8'),
'date': date.encode('utf8'),
'subject': subject.encode('utf8'),
'html_body': html_body.encode('utf8'),
'plain_body': plain_body.encode('utf8'),
'original': original.encode('utf8'),
'attachments': json.dumps(attachments)
})

headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}

result = urlfetch.fetch(url=url,
method=urlfetch.POST,
payload=form_fields,
headers=headers)

# log more
logging.info('POST to %s returned: %s', url, result.status_code)
logging.info('Returned content: %s', result.content)

application = webapp2.WSGIApplication([HandleEmail.mapping()], debug=True)

最佳答案

正如所讨论的,您可以使用 mail.send_mail() 函数在当前类中实现此目的。

from google.appengine.api import app_identity #[ If you want current user as the sender ]
from google.appengine.api import mail

def forward_mail(sender_address,forward_address,original_message,html_body,original_attachments):
# [START forward_mail]
message = mail.EmailMessage(
subject= original_message.subject,
to = forward_address,
sender = original_message.sender,
body = html_body,
attachments = original_attachments
)

message.send()
# [END forward_mail]

然后在 HandleEmail 类中调用此函数。

希望对你有帮助

关于python - 谷歌应用引擎: receive email then/and forward it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45407777/

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