根据文档,我们应该看一个名为" message"的参数,它不会进来。进来的是一个名为" mandrill_events&#34的参数;
require 'mail'
class InboxController < ApplicationController
skip_before_filter :verify_authenticity_token, only: :create
def create
mail = Mail.new(params[:message])
....
没有骰子,但是当我使用以下语法时,我确实在控制台日志中看到了这一点(注意,它是如此多的JSON,因此无法通过格式化查看它):
puts JSON.parse(params[:mandrill_events])
{"mandrill_events"=>"[{\"event\":\"inbound\",\"ts\":1426188360,\"msg\":{\"raw_msg\":\"Received: from mail-ob0-f181.google.com (unknown [209.85.214.181])\\n\\tby ip-10-39-136-77 (Postfix) with ESMTPS id 25B0E2C0509\\n\\tfor <test@inbound.diabetesdelivery.com>; Thu, 12 Mar 2015 19:26:00 +0000 (UTC)\\nReceived: by obcvb8 with SMTP id vb8so16123307obc.10\\n for <test@inbound.diabetesdelivery.com>; Thu, 12 Mar 2015 12:25:59 -0700 (PDT)\\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed\\/relaxed;\\n d=gmail.com; s=20120113;\\n h=mime-version:date:message-id:subject:from:to:content-type;\\n bh=jyd6a9aMNBrnuDpbsOEokXnACKw\\/lLnh72zkG7LCs3c=;\\n b=wzt6xiplBtL6dq7uGR8RV6tRR9Lcsmj5zmIktZ7KXFcxbwezN+uEhK\\/XFw6ZjIfhp6\\n LVoEH5ljLjOwK4X4nrtHaPoNzHZizwQUzxk0qXGabTXHW6tqOLZUs3FeDwtt2ekeLvlb\\n bDuU4VcBnNZ5VkXDpA5hQeysCfGECBilAtMy\\/EBjdOngcgk4Fsp8u11\\/eeB2xqBPUcrR\\n JYeu\\/99XossdoeJW3avfxFDBw7ngc98oRS2ZPpyL7MY36XoCZCM25U9ue9kBcJ82n92i\\n ctbNdl+9ikVPdcmUokI0WIvBIBGl5yAcrvOQffxmaTz0c31fkq8+iyprDMP\\/GexVfQzN\\n SAKA==\\nMIME-Version: 1.0\\nX-Received: by 10.202.89.135 with SMTP id n129mr34093639oib.60.1426188359432;\\n Thu, 12 Mar 2015 12:25:59 -0700 (PDT)\\nReceived: by 10.60.119.68 with HTTP; Thu, 12 Mar 2015 12:25:59 -0700 (PDT)\\nDate: Thu, 12 Mar 2015 12:25:59 -0700\\nMessage-ID: <CAMf1_47jvgW6QMwDx8R9JU6C5YiUMaPjvFz3muxhM5v4HJyoSg@mail.gmail.com>\\nSubject: Subject FTW?\\nFrom: Dee diabetic <Deediabetic@gmail.com>\\nTo: test@inbound.diabetesdelivery.com\\nContent-Type: multipart\\/alternative; boundary=001a113d39288bfe8505111c5a88\\n\\n--001a113d39288bfe8505111c5a88\\nContent-Type: text\\/plain; charset=UTF-8\\n\\nBody here\\n\\n--001a113d39288bfe8505111c5a88\\nContent-Type: text\\/html; charset=UTF-8\\n\\n<div dir=\\\"ltr\\\">Body here<\\/div>\\n\\n--001a113d39288bfe8505111c5a88--\",\"headers\":{\"Received\":[\"from mail-ob0-f181.google.com (unknown [209.85.214.181]) by ip-10-39-136-77 (Postfix) with ESMTPS id 25B0E2C0509 for <test@inbound.diabetesdelivery.com>; Thu, 12 Mar 2015 19:26:00 +0000 (UTC)\",\"by obcvb8 with SMTP id vb8so16123307obc.10 for <test@inbound.diabetesdelivery.com>; Thu, 12 Mar 2015 12:25:59 -0700 (PDT)\",\"by 10.60.119.68 with HTTP; Thu, 12 Mar 2015 12:25:59 -0700 (PDT)\"],\"Dkim-Signature\":\"v=1; a=rsa-sha256; c=relaxed\\/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=jyd6a9aMNBrnuDpbsOEokXnACKw\\/lLnh72zkG7LCs3c=; b=wzt6xiplBtL6dq7uGR8RV6tRR9Lcsmj5zmIktZ7KXFcxbwezN+uEhK\\/XFw6ZjIfhp6 LVoEH5ljLjOwK4X4nrtHaPoNzHZizwQUzxk0qXGabTXHW6tqOLZUs3FeDwtt2ekeLvlb bDuU4VcBnNZ5VkXDpA5hQeysCfGECBilAtMy\\/EBjdOngcgk4Fsp8u11\\/eeB2xqBPUcrR JYeu\\/99XossdoeJW3avfxFDBw7ngc98oRS2ZPpyL7MY36XoCZCM25U9ue9kBcJ82n92i ctbNdl+9ikVPdcmUokI0WIvBIBGl5yAcrvOQffxmaTz0c31fkq8+iyprDMP\\/GexVfQzN SAKA==\",\"Mime-Version\":\"1.0\",\"X-Received\":\"by 10.202.89.135 with SMTP id n129mr34093639oib.60.1426188359432; Thu, 12 Mar 2015 12:25:59 -0700 (PDT)\",\"Date\":\"Thu, 12 Mar 2015 12:25:59 -0700\",\"Message-Id\":\"<CAMf1_47jvgW6QMwDx8R9JU6C5YiUMaPjvFz3muxhM5v4HJyoSg@mail.gmail.com>\",\"Subject\":\"Subject FTW?\",\"From\":\"Dee diabetic <Deediabetic@gmail.com>\",\"To\":\"test@inbound.diabetesdelivery.com\",\"Content-Type\":\"multipart\\/alternative; boundary=001a113d39288bfe8505111c5a88\"},\"text\":\"Body here\\n\\n\",\"text_flowed\":false,\"html\":\"<div dir=\\\"ltr\\\">Body here<\\/div>\\n\\n\",\"from_email\":\"Deediabetic@gmail.com\",\"from_name\":\"Dee diabetic\",\"to\":[[\"test@inbound.diabetesdelivery.com\",null]],\"subject\":\"Subject FTW?\",\"spf\":{\"result\":\"pass\",\"detail\":\"sender SPF authorized\"},\"spam_report\":{\"score\":0.5,\"matched_rules\":[{\"name\":\"RCVD_IN_DNSWL_LOW\",\"score\":-0.7,\"description\":\"RBL: Sender listed at http:\\/\\/www.dnswl.org\\/, low\"},{\"name\":null,\"score\":0,\"description\":null},{\"name\":\"listed\",\"score\":0,\"description\":\"in list.dnswl.org]\"},{\"name\":\"FREEMAIL_FROM\",\"score\":0,\"description\":\"Sender email is commonly abused enduser mail provider\"},{\"name\":\"HTML_MESSAGE\",\"score\":0,\"description\":\"BODY: HTML included in message\"},{\"name\":\"DKIM_VALID_AU\",\"score\":-0.1,\"description\":\"Message has a valid DKIM or DK signature from author's\"},{\"name\":\"DKIM_SIGNED\",\"score\":0.1,\"description\":\"Message has a DKIM or DK signature, not necessarily valid\"},{\"name\":\"DKIM_VALID\",\"score\":-0.1,\"description\":\"Message has at least one valid DKIM or DK signature\"},{\"name\":\"RDNS_NONE\",\"score\":1.3,\"description\":\"Delivered to internal network by a host with no rDNS\"}]},\"dkim\":{\"signed\":true,\"valid\":true},\"email\":\"test@inbound.diabetesdelivery.com\",\"tags\":[],\"sender\":null,\"template\":null}}]"}
Mar 12 12:26:046666 app / web.1:{&#34; event&#34; =&gt;&#34; inbound&#34;,&#34; ts&#34; =&gt; 1426188360, &#34; msg&#34; =&gt; {&#34; raw_msg&#34; =&gt;&#34;收到:来自mail-ob0-f181.google.com(未知[209.85.214.181])\ n \ tby ip-10-39-136-77(Postfix),ESMTPS id 25B0E2C0509 \ n \ t;星期四,2015年3月12日19:26:00 +0000(UTC)\ n接收:通过obcvb8与SMTP id vb8so16123307obc.10 \ n for;星期四,2015年3月12日12:25:59 -0700(PDT)\ nDKIM-Signature:v = 1; A = RSA-SHA256; c =放松/放松; \ n d = gmail.com; S = 20120113; \ NH = MIME版本:日期:消息ID:受试者:由:内容类型; \ n BH = jyd6a9aMNBrnuDpbsOEokXnACKw / lLnh72zkG7LCs3c =; \ NB = wzt6xiplBtL6dq7uGR8RV6tRR9Lcsmj5zmIktZ7KXFcxbwezN + uEhK / XFw6ZjIfhp6 \ n LVoEH5ljLjOwK4X4nrtHaPoNzHZizwQUzxk0qXGabTXHW6tqOLZUs3FeDwtt2ekeLvlb \ n bDuU4VcBnNZ5VkXDpA5hQeysCfGECBilAtMy / EBjdOngcgk4Fsp8u11 / eeB2xqBPUcrR \ n倢羽企业/ 99XossdoeJW3avfxFDBw7ngc98oRS2ZPpyL7MY36XoCZCM25U9ue9kBcJ82n92i \ n ctbNdl + 9ikVPdcmUokI0WIvBIBGl5yAcrvOQffxmaTz0c31fkq8 + iyprDMP / GexVfQzN \ n SAKA == \ nMIME-版本:1.0 \ NX-接收:由10.202.89.135与SMTP ID n129mr34093639oib.60.1426188359432; \ n星期四,12 2015年3月12:25:59 -0700(太平洋夏令时)\ n接收:用10.60.119.68与HTTP;星期四,2015年3月12日12:25:59 -0700(PDT)\ n日期:星期四,2015年3月12日12:25:59 -0700 \ n消息ID:\ n主题:主题FTW?\ n来自:糖尿病患者\ nTo:test @ inbound.diabetesdelivery.com \ nContent-Type:multipart / alternative; boundary = 001a113d39288bfe8505111c5a88 \ n \ n - 001a113d39288bfe8505111c5a88 \ nContent-Type:text / plain; charset = UTF-8 \ n \ nBody here \ n \ n - 001a113d39288bfe8505111c5a88 \ nContent-Type:text / html; charset = UTF-8 \ n \ n在这里\ n \ n - 001a113d39288bfe8505111c5a88 - &#34;,&#34;标题&#34; =&gt; {&#34;收到&#34; =&gt; [&# 34;来自mail-ob0-f181.google.com(未知[209.85.214.181])ip-10-39-136-77(Postfix),ESMTPS id 25B0E2C0509 for;星期四,2015年3月12日19:26:00 +0000(UTC)&#34;,&#34; by obcvb8 with SMTP id vb8so16123307obc.10 for;星期四,2015年3月12日12:25:59 -0700(PDT)&#34;,&#34;由10.60.119.68与HTTP;星期四,2015年3月12日12:25:59 -0700(PDT)&#34;],&#34; Dkim-Signature&#34; =&gt;&#34; v = 1; A = RSA-SHA256; C =轻松/放松; d = gmail.com; S = 20120113; H = MIME版本:日期:消息ID:受试者:由:内容类型; BH = jyd6a9aMNBrnuDpbsOEokXnACKw / lLnh72zkG7LCs3c =; B = wzt6xiplBtL6dq7uGR8RV6tRR9Lcsmj5zmIktZ7KXFcxbwezN + uEhK / XFw6ZjIfhp6 LVoEH5ljLjOwK4X4nrtHaPoNzHZizwQUzxk0qXGabTXHW6tqOLZUs3FeDwtt2ekeLvlb bDuU4VcBnNZ5VkXDpA5hQeysCfGECBilAtMy / EBjdOngcgk4Fsp8u11 / eeB2xqBPUcrR倢羽企业/ 99XossdoeJW3avfxFDBw7ngc98oRS2ZPpyL7MY36XoCZCM25U9ue9kBcJ82n92i ctbNdl + 9ikVPdcmUokI0WIvBIBGl5yAcrvOQffxmaTz0c31fkq8 + iyprDMP / GexVfQzN SAKA ==&#34;,&#34; MIME的版本&#34; =&GT;&#34; 1.0& #34;,&#34; X-Received&#34; =&gt;&#34;由10.202.89.135与SMTP id n129mr34093639oib.60.1426188359432;星期四,2015年3月12日12:25:59 -0700(PDT)&#34;,&#34;日期&#34; =&gt;&#34;星期四,2015年3月12日12:25:59 -0700&#34; ,&#34; Message-Id&#34; =&gt;&#34;&#34;,&#34;主题&#34; =&gt;&#34;主题FTW?&#34;,&#34;来自&#34; =&gt;&#34; Dee diabetic&#34;,&#34; To&#34; =&gt;&#34; test@inbound.diabetesdelivery.com",&#34; Content-类型&#34; = GT;&#34;多部分/替代; boundary = 001a113d39288bfe8505111c5a88&#34;},&#34; text&#34; =&gt;&#34; Body在这里\ n \ n&#34;,&#34; text_flowed&#34; =&gt; false,&#34; html&#34; =&gt;&#34; Body在这里\ n \ n&#34;,&#34; from_email&#34; =&gt;&#34; Deediabetic@gmail.com",&#34; from_name& #34; =&gt;&#34; Dee diabetic&#34;,&#34; to&#34; =&gt; [[&#34; test@inbound.diabetesdelivery.com",nil]],&# 34;主题&#34; =&gt;&#34;主题FTW?&#34;,&#34; spf&#34; =&gt; {&#34;结果&#34; =&gt;&#34;传递&# 34;,&#34;详细信息&#34; =&gt;&#34;发件人SPF授权&#34;},&#34; spam_report&#34; =&gt; {&#34;得分&#34; =&gt; 0.5 ,&#34; matched_rules&#34; =&gt; [{&#34;名称&#34; =&gt;&#34; RCVD_IN_DNSWL_LOW&#34;,&#34;得分&#34; =&gt; -0.7,&# 34;描述&#34; =&gt;&#34; RBL:发件人列在http://www.dnswl.org/,低&#34;},{&#34;名称&#34; =&gt; nil,&#34;得分&# 34; =&gt; 0,&#34;说明&#34; =&gt; nil},{&#34;名称&#34; =&gt;&#34;列出&#34;,&#34;得分&#34; =&gt; 0,&#34;说明&#34; =&gt;&#34;在list.dnswl.org中]&#34;},{&#34;名称&#34; =&gt;&#34; FREEMAIL_FROM& #34;,&#34;得分&#34; =&gt; 0,&#34; de scription&#34; =&gt;&#34;发件人电子邮件通常是滥用的最终用户邮件提供商&#34;},{&#34;名称&#34; =&gt;&#34; HTML_MESSAGE&#34;,&#34;得分& #34; =&gt; 0,&#34;说明&#34; =&gt;&#34; BODY:消息中包含的HTML&#34;},{&#34; name&#34; =&gt;&#34; DKIM_VALID_AU&#34;,&#34;得分&#34; =&gt; -0.1,&#34;说明&#34; =&gt;&#34;讯息具有来自作者的有效DKIM或DK签名&#34 ;},{&#34;名称&#34; =&gt;&#34; DKIM_SIGNED&#34;,&#34;得分&#34; =&gt; 0.1,&#34;说明&#34; =&gt;&# 34;消息具有DKIM或DK签名,不一定有效&#34;},{&#34;名称&#34; =&gt;&#34; DKIM_VALID&#34;,&#34;得分&#34; =&gt; -0.1,&#34;说明&#34; =&gt;&#34;消息至少有一个有效的DKIM或DK签名&#34;},{&#34;名称&#34; =&gt;&#34; RDNS_NONE& #34;,&#34;得分&#34; =&gt; 1.3,&#34;说明&#34; =&gt;&#34;由没有rDNS&#34;}]}的主机传送到内部网络,& #34; dkim&#34; =&gt; {&#34;签名&#34; =&gt; true,&#34;有效&#34; =&gt; true},&#34;电子邮件&#34; =&gt;& #34; test@inbound.diabetesdelivery.c om&#34;,&#34;标签&#34; =&gt; [],&#34;发件人&#34; =&gt; nil,&#34;模板&#34; =&gt; nil}}
所以我得到一个JSON字符串,但是获取TO,FROM和SUBJECT的最佳方法是什么?
更新:我刚刚尝试了以下内容:
tester = JSON.parse(params[:mandrill_events])
puts tester[:msg][:subject]
#and even this
puts tester["msg"]["subject"]
没用。出错:** TypeError(没有将String隐式转换为Integer):**
答案 0 :(得分:0)
经过一天的敲打,我想出了解决方案。顺便说一下,直接访问这些值并不起作用。我不得不循环进去。
incomingMail = params[:mandrill_events]
mail_from = "Cannot read From"
mail_originally_to = "Cannot read To"
mail_subject = "No subject"
mail_date = ""
mail_body = "No email body"
if incomingMail != nil
incomingMail = JSON.parse(incomingMail)
incomingMail.each do |item|
mail_from = item["msg"]["from_email"]
mail_originally_to = item["msg"]["headers"]["To"]
mail_subject = item["msg"]["subject"]
mail_date = item["msg"]["headers"]["Date"]
mail_body = item["msg"]["text"]
mail_body_html = item["msg"]["html"]
end
#Confirm
else
puts "Email not sent. Parameter invalid."
end