【问题标题】:Parsing incoming mail with Google App Engine?使用 Google App Engine 解析收到的邮件?
【发布时间】:2019-10-08 19:52:59
【问题描述】:

我们使用 Google Apps 设置了邮件。我们希望能够对收到的邮件运行一些正则表达式并处理这些信息。

现在可以通过 Google App Engine 实现吗?谷歌是否提供某种可以做到这一点的基础设施?

【问题讨论】:

    标签: google-app-engine email


    【解决方案1】:

    来自谷歌文档here

    接收邮件

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

    string@appid.appspotmail.com
    

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

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

    /_ah/mail/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.
    

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      更新:现在是supported

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

      【讨论】:

      • 你有什么其他方法可以推荐我如何完成这项任务
      • 不,我想你可以从 html 邮件客户端屏幕抓取邮件,然后使用类似这样的东西:schedulerservice.appspot.com/about 最好使用其他平台或等待路线图实施。
      【解决方案4】:

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

      【讨论】:

      【解决方案5】:

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

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

      缺点是您必须通过轮询来获取信息,但这应该没问题,因为接受电子邮件会有些延迟。

      【讨论】:

      • 您不允许使用套接字库,这会阻止 imap 工作吗?
      • 我的意思是说,外部服务器(你自己托管的)没有在应用引擎上运行,所以它可以访问套接字库。我把答案编辑得更清楚了
      【解决方案6】:

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

      来自文档:

      接收邮件

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

      anything@appid.appspotmail.com
      

      here 中讨论的python 中处理传入邮件的配置相比,在应用程序的app.yaml 文件中启用传入邮件相当简单:

      inbound_services:
      - mail
      

      将您的应用文件命名为mail.go,然后将处理程序注册到/_ah/mail/ 路径并使用net/mail*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)
      }
      

      发送邮件

      关注此guideline 将您的发件人电子邮件注册为authorized senders

      使用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 上找到:

      GoogleCloudPlatform/golang-samples/docs/appengine/mail/mail.go

      要克隆示例代码,请转至您的Console。点击按钮打开Cloud Shell 然后与this quickstart类似,输入以下步骤:

      $ 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
      

      从这里您可以使用Web preview 访问端口8080 上的应用程序。
      要终止在Cloud Shell 中按Ctrl+C
      最后你可以部署你的应用了

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

      点击网址访问

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

      然后发邮件到anything@your-application-id.appspotmail.com看看是否有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-13
        • 2018-08-02
        • 2010-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-04
        • 2016-12-25
        相关资源
        最近更新 更多