【问题标题】:How to get email reply on rails app from email server如何从电子邮件服务器获取 Rails 应用程序上的电子邮件回复
【发布时间】:2014-01-27 18:53:02
【问题描述】:

我需要实现消息传递功能。当我从我的 rails 应用程序向任何用户发送消息时,它也会发送到用户电子邮件。但是,如果收到电子邮件的用户从 gmail、yahoo..etc 进行回复,那么我需要实施的回复也应该进入 rails 应用程序。任何人都可以指导我..所以我可以在谷歌上搜索它。

例如:

如果我从这封电子邮件“mc-6bckm434ls@reply.xyz.com”向用户发送电子邮件,并且用户在 gspq5diedss@reply.xyz.com 上回复,我在标题中设置了 Reply-To。然后我需要用户在我的 rails 应用程序中的回复,以便我可以将此用户的回复添加到我的消息传递线程中。 通过这个我想要实现的功能,用户不需要登录我的应用程序来发送消息,用户也可以通过电子邮件回复在当前对话中发送消息。

【问题讨论】:

  • 我很想知道这是否可能
  • 只需创建一个 gmail 帐户并让 rails 轮询该帐户以获取传入的电子邮件。这是最简单的解决方案。谷歌“ruby gmail”可以找到一些你可以使用的宝石。

标签: ruby-on-rails email sendgrid


【解决方案1】:

有可能!

例如,您可以使用mailman

您所要做的就是在您的电子邮件中设置一个Reply-To 标头以使其独一无二,这样当您获取邮件时您就知道它对应的是什么。

例如,假设您拥有电子邮件地址 foo@bar.com

您可以发送带有回复标题“foo+content_id@bar.com”的电子邮件,这样您就知道用户正在回复什么。

然后,mailman 可以从邮箱中获取邮件并解析其中的内容 id。

一些服务也这样做,处理所有电子邮件部分并向您发送收到电子邮件的通知,例如,postmark

【讨论】:

    【解决方案2】:

    https://github.com/titanous/mailman/blob/master/USER_GUIDE.md

    有一个关于使用 gem 的 railscast,但它只收费:-/ http://railscasts.com/episodes/313-receiving-email-with-mailman

    但是,有来自 railscast 的 github 存储库,它可以向您展示一个使用 mailman 的示例应用程序(带有 before 和 after,以便您可以跟踪更改)-https://github.com/railscasts/313-receiving-email-with-mailman

    【讨论】:

      【解决方案3】:

      由于您标记了 SendGrid 问题,我假设您正在使用它。您可以使用 SendGrid 的Inbound Parse Webhook 来处理传入消息的解析。

      我们还有一个最近的教程,介绍了在 rails 应用程序中使用 webhook:http://sendgrid.com/blog/two-hacking-santas-present-rails-the-inbound-parse-webhook/

      【讨论】:

        【解决方案4】:

        Ruby on Rails 在 6.0 版中引入了Action Mailbox

        使用 ActionMailbox,您可以轻松配置将传入电子邮件路由到何处的规则(文档中的示例):

        # app/mailboxes/application_mailbox.rb
        class ApplicationMailbox < ActionMailbox::Base
          routing /^save@/i     => :forwards
          routing /@replies\./i => :replies
        end
        

        以及如何处理邮箱中的特定消息:

        # app/mailboxes/forwards_mailbox.rb
        class ForwardsMailbox < ApplicationMailbox
          # Callbacks specify prerequisites to processing
          before_processing :require_forward
        
          def process
            if forwarder.buckets.one?
              record_forward
            else
              stage_forward_and_request_more_details
            end
          end
        
          private
            def require_forward
              unless message.forward?
                # Use Action Mailers to bounce incoming emails back to sender – this halts processing
                bounce_with Forwards::BounceMailer.missing_forward(
                  inbound_email, forwarder: forwarder
                )
              end
            end
        
            def forwarder
              @forwarder ||= Person.where(email_address: mail.from)
            end
        
            def record_forward
              forwarder.buckets.first.record \
                Forward.new forwarder: forwarder, subject: message.subject, content: mail.content
            end
        
            def stage_forward_and_request_more_details
              Forwards::RoutingMailer.choose_project(mail).deliver_now
            end
        end
        

        Rails Guides 中查找有关如何配置操作邮箱的文档和一些示例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-21
          • 2011-04-06
          • 2012-01-12
          相关资源
          最近更新 更多