0
我有一個應用程序引擎應用程序,它接收電子郵件並將其元數據發送到腳本進行處理。它運作良好。代碼如下,並通過Github示例從Google提供。Google App Engine:接收電子郵件然後轉發它?
但問題是,如何將此腳本(或一般的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)
您是否正在尋找? https://cloud.google.com/appengine/docs/standard/python/mail/sending-mail-with-mail-api –
是我嗎?我想我是。 「轉發」是最終用戶在as處引用的內容。但我想後端正在接收消息,然後發送它。我對電子郵件瞭解不多,所以如果在協議層面沒有「轉發」這樣的事情,那麼就是這樣。 – Warren
我相信你已經回答了你自己的問題然後:)導致只有你可以根據你的命令「轉發/發送」電子郵件 –