【问题标题】:Unable to parse Sendgrid parse api raw callback into Griddler email object无法将 Sendgrid 解析 api 原始回调解析为 Griddler 电子邮件对象
【发布时间】:2015-12-06 01:55:47
【问题描述】:

我正在努力完成以下工作:

让所有发送到我的域的电子邮件都解析其收件人、正文、主题行和...的内容,以便我从中构造一个传真对象并将其作为传真发送

到目前为止,我已经能够将我的 MX 记录设置为指向 Sendgrid,并使用 Ngrok 从 Sendgrid 的 Parse API 中获得回调,我的本地服务器中。下一步是将原始对象转换为我可以使用的对象。这让我找到了Thoughtbot的Griddler Gem

这就是我到目前为止设置我的应用程序的方式:

Sendgrid 设置: 网址:https://8c00fab0.ngrok.io/email_processor |垃圾邮件检查和发送原始邮件都被检查

如前所述,这部分有效,因为我确实收到了这个:

Parameters: {"dkim"=>"{@gmail.com : pass}", "email"=>"Received: by mx0034p1mdw1.sendgrid.net with SMTP id 253FfvZ40f Wed, 09 Sep 2015 22:31:48 +0000 (UTC)\nReceived: from mail-qg0-f54.google.com (mail-qg0-f54.google.com [209.85.192.54]) by mx0034p1mdw1.sendgrid.net (Postfix) with ESMTPS id 63848A83017 for <14085552422@faxbyemail.co>; Wed,  9 Sep 2015 22:31:48 +0000 (UTC)\nReceived: by qgx61 with SMTP id 61so21251135qgx.3 for <14085552422@faxbyemail.co>; Wed, 09 Sep 2015 15:31:48 -0700 (PDT)\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=Wo6p7CSlR30aGxf5SwjuZ2klt31URDqTZUHrKdmLz7s=; b=fWfFOQ4YOtijYvphYL9/1shyzK8CBTcHS37s89t9Qn4xK8OrXwVKtmGtNnwg7a1n48 JHkEeAdxVvRMpinxPllGiJoPNy0ivVN36uEYbsrLTcZVlx9RIIl8u5PjmrE1aqlnkjPy gOcbfJunVCFZLaLpv3RB/GiNYL1bodgW7nn8sMTmwN5EwoH7SncS7cle4ksPadgiGv9f DVoJniX2yuMqygSVLT4Qrk4JDzR6G2oj1Fz3QkcrQfNKxOS6iyBhpnh3cDuUi8GTSVYl Nqq2dtKlUNqI+U9ojtG1BjGVUKbP0v5apF3Wvmj72p8L0AB3FHqL8uvFnhi2dGTFVq+y 6nUQ==\nX-Received: by 10.140.233.137 with SMTP id e131mr48879201qhc.37.1441837907953; Wed, 09 Sep 2015 15:31:47 -0700 (PDT)\nMIME-Version: 1.0\nFrom: Sam Sedighian <samansb@gmail.com>\nDate: Wed, 09 Sep 2015 22:31:38 +0000\nMessage-ID: <CAEMkzYbhsLLB3NPLznG+-RibP5kzDG5yTT++mQL1ep-vH6_orA@mail.gmail.com>\nSubject: test 20\nTo: \"14085552422@faxbyemail.co\" <14085552422@faxbyemail.co>\nContent-Type: multipart/alternative; boundary=001a11354d8053d2d1051f580c22\n\n--001a11354d8053d2d1051f580c22\nContent-Type: text/plain; charset=UTF-8\n\nbody etxt\n\n--001a11354d8053d2d1051f580c22\nContent-Type: text/html; charset=UTF-8\n\n<div dir=\"ltr\">body etxt</div>\n\n--001a11354d8053d2d1051f580c22--\n", "to"=>"\"14085552422@faxbyemail.co\" <14085552422@faxbyemail.co>", "from"=>"Sam Sedighian <samansb@gmail.com>", "sender_ip"=>"209.85.192.54", "spam_report"=>"Spam detection software, running on the system \"mx0034p1mdw1.sendgrid.net\", has\nidentified this incoming email as possible spam.  The original message\nhas been attached to this so you can view it (if it isn't spam) or label\nsimilar future email.  If you have any questions, see\n@@CONTACT_ADDRESS@@ for details.\n\nContent preview:  body etxt body etxt [...] \n\nContent analysis details:   (0.0 points, 5.0 required)\n\n pts rule name              description\n---- ---------------------- --------------------------------------------------\n 0.0 FREEMAIL_FROM          Sender email is freemail (samansb[at]gmail.com)\n 0.0 HTML_MESSAGE           BODY: HTML included in message\n 0.0 T_MIME_NO_TEXT         No text body parts\n\n", "envelope"=>"{\"to\":[\"14085552422@faxbyemail.co\"],\"from\":\"samansb@gmail.com\"}", "subject"=>"test 20", "spam_score"=>"0.012", "charsets"=>"{\"to\":\"UTF-8\",\"subject\":\"UTF-8\",\"from\":\"UTF-8\"}", "SPF"=>"pass"}

宝石文件

gem 'griddler'
gem 'griddler-sendgrid'

Gemfile.lock

griddler (1.2.1)
  htmlentities
  rails (>= 3.2.0)
griddler-sendgrid (0.0.1)
  griddler

routes.rb

post '/email_processor' => 'fax#sendgrid'
# this corresponds to the following route when I rake route:
# email_processor POST   /email_processor(.:format) fax#sendgrid

config/initializers/griddler.rb

Griddler.configure do |config|
  config.processor_class = EmailProcessor
  config.processor_method = :process
  config.reply_delimiter = '-- REPLY ABOVE THIS LINE --'
  config.email_service = :sendgrid
end

app/controllers/fax_controller.rb

def sendgrid
  EmailProcessor.new(params).process
end

app/models/email_processor.rb

class EmailProcessor

  def initialize(email)
    @email = email
    @from = email.from
    @body = email.body
  end

  def self.process(email)
    Fax.create({params go here})
  end

end

我最终给出了 Completed 500 Internal server error - 因为我没有将 200 OK 回传给 Sendgrid,但重要的是我在日志中收到以下错误:

NoMethodError (undefined method `from' for #<ActionController::Parameters:0x007fc742a33070>):
  app/models/email_processor.rb:5:in `initialize'
  app/controllers/fax_controller.rb:31:in `new'
  app/controllers/fax_controller.rb:31:in `sendgrid'

【问题讨论】:

    标签: ruby-on-rails sendgrid griddler


    【解决方案1】:

    我认为问题在于您绕过了 Griddler 在控制器端所做的工作。您将 Sendgrid 指向您自己创建的控制器和操作。相反,我认为您想指向 Griddler 的控制器和操作。见the README under "Installation"

    将您的 routes.rb 更改为:

    # your own route
    # post '/email_processor' => 'fax#sendgrid'
    
    # griddler's
    post '/email_processor' => 'griddler/emails#create'
    

    如果您take a look at Griddler's controller,您会发现有一些事情可以帮助您将内容放入您的电子邮件处理器类中。

    【讨论】:

      【解决方案2】:

      我在 Rails 中这样做的方式是在 Controller 中收集电子邮件参数。

      例如,在我创建的示例应用程序中,人们通过电子邮件将自拍照发送到我们的 Parse API。在我的 selfie_controller.rb 中,我有一个路线“/入站”,我有以下内容:

      def inbound
      
          if !params['attachment1'].nil? and params['attachment1'].content_type == "image/jpeg"
      
              Selfy.create!({ pic: params['attachment1'], email: params['from'] })
      
          end
      
      end
      

      对 Selfy.create 的调用!使用 selfy.rb 模型,但我从电子邮件中收集参数以从控制器发送到该参数。

      你可以试试这个吗?

      【讨论】:

      • 抱歉,刚刚意识到 - 这不是使用 Griddler,只是手动处理。 (对此,Griddler 有点不必要)
      • 从外观上看,我可能只需要手动执行此操作。我试图避免它,因为我不想处理极端情况。但是,您的答案对于手动操作绝对有用。谢谢
      猜你喜欢
      • 2020-05-18
      • 2012-09-14
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 2015-05-03
      相关资源
      最近更新 更多