使用谷歌应用引擎解析收到的邮件?

时间:2009-03-05 22:13:18

标签: google-app-engine email

我们使用谷歌应用程序设置邮件。我们希望能够在传入邮件上运行一些正则表达式并处理此信息。

今天是否可以使用Google App Engine?谷歌提供某种可以做到这一点的基础设施吗?

6 个答案:

答案 0 :(得分:11)

来自Google文档here

接收邮件

您的应用可以通过以下表单的地址接收电子邮件:

string@appid.appspotmail.com

请注意,即使您的应用部署在自定义域中,您的应用也无法接收发送到该域上地址的电子邮件。 电子邮件将作为HTTP请求发送到您的应用。这些请求由App Engine生成并发布到您的应用程序。在应用程序的配置中,指定将被调用以处理这些HTTP请求的处理程序。在处理程序中,您会收到电子邮件的MIME数据,然后您可以将其分析到各个字段中。

使用以下网址将电子邮件作为HTTP POST请求发送到您的应用:

/_ah/mail/address

其中address是完整的电子邮件地址,包括域名。 默认情况下,禁用在您的应用中接收邮件的功能。要使您的应用程序能够接收邮件,您必须在app.yaml文件中指定您希望启用此服务,方法是:

inbound_services:
- mail

Python SDK定义了InboundMailHandler,这是一个用于处理传入电子邮件的webapp类。要使用InboundMailHandler,您可以将其子类化并覆盖receive()方法。使用类InboundEmailMessage的参数调用receive()方法,这是Python SDK定义的另一个类。

InboundMailHandler位于google.appengine.ext.webapp.mail_handlers包中。您可以像这样创建一个InboundEmailMessage实例:

import logging, email
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        logging.info("Received a message from: " + mail_message.sender)

InboundEmailMessage对象包含用于访问其他消息字段的属性:

subject contains the message subject.
sender is the sender's email address.
to is a list of the message's primary recipients.
cc contains a list of the cc recipients.
date returns the message date.
attachments is a list of file attachments, possibly empty. Each value in the list is a tuple of two elements: the filename and the file contents.
original is the complete message, including data not exposed by the other fields such as email headers, as a Python email.message.Message.

答案 1 :(得分:4)

答案 2 :(得分:3)

更新:现在是supported

尚不支持处理传入的电子邮件。但是,它们的路线图是:http://code.google.com/appengine/docs/roadmap.html

答案 3 :(得分:2)

Google目前不支持在App Engine中处理电子邮件,尽管它位于路线图中。与此同时,smtp2web等服务将为您处理(免责声明:我写过smtp2web)。

答案 4 :(得分:0)

您可以设置电子邮件帐户并让外部服务器(您在AE之外创建和托管的服务器)通过IMAP访问Gmail帐户。然后,您的“邮件服务器”会在AE上读取消息并访问您应用的/ email API。

Python有一个电子邮件模块,因此您可以在那里发布整个邮件,或者如果这不起作用(由于任何限制),您可以在邮件服务器上预处理它并将简化版本发布到您的应用程序。 / p>

缺点是你不得不求助于民意调查信息,但这应该没问题,因为接受电子邮件会有一些延迟。

答案 5 :(得分:0)

作为Gabriel的answer的另一个选项,我建议在go上使用App EngineSending and Receiving Mail with the Mail API环境。

来自文档:

接收邮件

您的应用可以通过以下表单的地址接收电子邮件:

anything@appid.appspotmail.com

python中处理传入邮件的配置进行比较[{3}},在您应用的here文件中启用传入邮件非常简单:

inbound_services:
- mail

将您的应用文件命名为mail.go,然后将处理程序注册到/_ah/mail/路径,并使用app.yaml*http.Requestlike读取电子邮件的数据,如下所示:

func incomingMail(w http.ResponseWriter, r *http.Request) {
        ctx := appengine.NewContext(r)
        defer r.Body.Close()
        var b bytes.Buffer
        if _, err := b.ReadFrom(r.Body); err != nil {
                log.Errorf(ctx, "Error reading body: %v", err)
                return
        }
        log.Infof(ctx, "Received mail: %v", b)
}

发送邮件

按照此 net/mail 将发件人电子邮件注册为 guideline

使用mail.Message类型设置邮件的发件人,收件人,主题和正文 使用mail.Send功能发送电子邮件。

func confirm(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    addr := r.FormValue("email")
    url := createConfirmationURL(r)
    msg := &mail.Message{
        Sender:  "Example.com Support <support@example.com>",
        To:      []string{addr},
        Subject: "Confirm your registration",
        Body:    fmt.Sprintf(confirmMessage, url),
    }
    if err := mail.Send(ctx, msg); err != nil {
        log.Errorf(ctx, "Couldn't send email: %v", err)
    }
}

部署

GitHub上提供了接收和发送的完整示例代码:

  

authorized senders

要克隆示例代码,请转到GoogleCloudPlatform/golang-samples/docs/appengine/mail/mail.go。点击按钮打开ConsoleCloud Shell 然后与enter image description here类似,请输入以下步骤:

$ SOURCEDIR=https://github.com/GoogleCloudPlatform/golang-samples.git
$ TUTORIALDIR=~/src/your-application-id/go_gae_samples
$ git clone $SOURCEDIR $TUTORIALDIR
$ cd $TUTORIALDIR
$ git checkout master
$ cat docs/appengine/mail/app.yaml
$ cat docs/appengine/mail/mail.go
$ goapp serve docs/appengine/mail/app.yaml

从此处,您可以使用this quickstart在端口8080上访问该应用 要终止Ctrl+C中的Cloud Shell 最后,您可以部署您的应用

goapp deploy -application your-application-id -version 0

点击访问该网址的网址

http://your-application-id.appspot.com/

然后发送电子邮件至anything@your-application-id.appspotmail.com,看看它是否有效。