我在Rails应用程序中使用SendGrid的
SMTP API发送电子邮件.但是,我使用RSpec测试电子邮件头(“X-SMTPAPI”)遇到麻烦.
邮件看起来像这样(从ActionMailer :: Base.deliveries检索):
#<Mail::Message:2189335760,Multipart: false,Headers: <Date: Tue,20 Dec 2011 16:14:25 +0800>,<From: "Acme Inc" <contact@acmeinc.com>>,<To: doesntmatter@nowhere.com>,<Message-ID: <4ef043e1b9490_4e4800eb1b095f1@Macbook.local.mail>>,<Subject: Your Acme order>,<Mime-Version: 1.0>,<Content-Type: text/plain>,<Content-Transfer-Encoding: 7bit>,<X-SMTPAPI: {"sub":{"|last_name|":[Foo],"|first_name|":[Bar]},"to":["foo@bar.com"]}>>@H_301_5@这是我的规范代码(失败):
ActionMailer::Base.deliveries.last.to.should include("foo@bar.com")@H_301_5@我也尝试过各种方法来检索标题(“X-SMTPAPI”),并且也不起作用:
mail = ActionMailer::Base.deliveries.last mail.headers("X-SMTPAPI") #NoMethodError: undefined method `each_pair' for "X-SMTPAPI":String@H_301_5@帮帮我?
更新(回答)
mail.header['X-SMTPAPI'].value@H_301_5@但是,返回的值为JSON格式.那么我所需要做的就是解码它:
sendgrid_header = ActiveSupport::JSON.decode(mail.header['X-SMTPAPI'].value)@H_301_5@它返回一个哈希,在那里我可以做到这一点:
sendgrid_header["to"]@H_301_5@检索电子邮件地址数组.