我有一个应用引擎应用,可以接收电子邮件并将其元数据发送到脚本进行处理。它运作良好。代码如下,并通过Github示例从Google提供。
但问题是,如何将此脚本(或一般的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)
答案 0 :(得分:0)
如上所述,您可以使用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(
sender= sender_address,
subject= original_message.subject,
to = forward_address,
sender = original_message.sender,
body = html_body,
attachments = original_attachments
)
message.send()
# [END forward_mail]
然后在HandleEmail类中调用此函数。
希望有所帮助