How to test email headers using RSpec - ruby-on-rails

I'm using SendGrid's SMTP API in my Rails application to send out emails. However, I'm running into troubles testing the email header ("X-SMTPAPI") using RSpec.
Here's what the email looks like (retrieving from 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"]}>>
Here's my spec code (which failed):
ActionMailer::Base.deliveries.last.to.should include("foo#bar.com")
I've also tried various method to retrieve the header("X-SMTPAPI") and didn't work either:
mail = ActionMailer::Base.deliveries.last
mail.headers("X-SMTPAPI") #NoMethodError: undefined method `each_pair' for "X-SMTPAPI":String
Help?
Update (answer)
Turns out, I can do this to retrieve the value of the email header:
mail.header['X-SMTPAPI'].value
However, the returned value is in JSON format. Then, all I need to do is to decode it:
sendgrid_header = ActiveSupport::JSON.decode(mail.header['X-SMTPAPI'].value)
which returns a hash, where I can do this:
sendgrid_header["to"]
to retrieve the array of email addresses.

The email_spec gem has a bunch of matchers that make this easier, you can do stuff like
mail.should have_header('X-SMTPAPI', some_value)
mail.should deliver_to('foo#bar.com')
And perusing the source to that gem should point you in the right direction if you don't want to use it e.g.
mail.to.addrs
returns you the email addresses (as opposed to stuff like 'Bob ')
and
mail.header['foo']
gets you the field for the foo header (depending on what you're checking you may want to call to_s on it to get the actual field value)

Repeating some of the other advice here, in more modern rspec syntax:
RSpec.describe ImportFile::Mailer do
describe '.file_error' do
let(:mail) { described_class.file_error('daily.csv', 'missing header') }
it { expect(mail.subject).to eq("Import error: missing header in daily.csv") }
it { expect(mail.header['X-source-file'].to_s).to eq ('daily.csv') }
end
end

Related

Stub Httparty call: Wrong number of arguments (given 2, expected 1)

I created a simple ruby file (not Rails) and I am trying to test (using Rspec) a method where I am calling an API. In the test I am trying to mock the call via WebMock but it keeps giving me this error:
Requests::FilesManager#display fetches the files from the API
Failure/Error: Requests::FilesManager.new.display
ArgumentError:
wrong number of arguments (given 2, expected 1)
The files are:
#run.rb
module Requests
require "httparty"
require 'json'
class FilesManager
include HTTParty
def initialize
end
def display
response = HTTParty.get('https://api.publicapis.org/entries', format: :json)
parsed_response = JSON.parse(response.body)
puts "The secret message was: #{parsed_response["message"]}"
end
end
end
and the spec file:
require 'spec_helper'
require_relative '../run'
RSpec.describe Requests::FilesManager do
describe "#display" do
it 'fetches the files from the API' do
stub_request(:get, "https://api.publicapis.org/entries").
to_return(status: 200, body: "", headers: {})
Requests::FilesManager.new.display
end
end
end
EDIT:
So the error seems to come from the line:
JSON.parse(response.body)
If I comment it out it disappears. The problem then is that the output of the call is not a json (even with the format: :json when calling the HTTParty). I tried other solutions but nothing seems to work in making the response json. It is just a string.
Change
response = HTTParty.get('https://api.publicapis.org/entries', format: :json)
to
response = HTTParty.get('https://api.publicapis.org/entries').
I think you don't need the format: :json more so when you explicitly format the response to JSON anyway.
You need to return a json object in the body parameter of the stubbed response:
E.g: For an empty response:
stub_request(:get, "https://api.publicapis.org/entries").
to_return(status: 200, body: "".to_json, headers: {})
OR For a valid response: (Note: You may have to require json to convert a hash to json)
require 'json'
...
stub_request(:get, "https://api.publicapis.org/entries").
to_return(status: 200, body: { entries: { '0': { message: "Hello World" } } }.to_json, headers: {})
Solved!
It seems there was an error because the json gem version that HTTParty uses is too old.
Moved on to RestClient gem for the RESTful API calls. It had another conflict in the mime gem versioning.
Finally moved to Faraday and that solved my problems:
JSON.parse(response.body, :quirks_mode => true)
tl;dr Had the same issue and ended up having to upgrade webmock.
Long form:
Webmock inserts middleware into your calls, so when HTTParty makes the calls they end up going through the Webmock interfaces first.
You can verify this by trying the call standalone (withouth all the rspec config):
bundle console
irb> require "httparty"
=> true
irb> httparty.get("https://google.com")
If that standalone call succeeds, the issue is somewhere within Webmock itself.
For me, somewhere along the line of calls through Webmock was an outdated interface that was incompatible and throwing the Wrong Number of Arguments error. And this was also crashing my debugger (RubyMine).
Upgrading Webmock solved this issue (because they had fixed it in newer versions).

Sendgrid v3 not working for me with rails my_mailer.rb

I want to send a transactional mail via Sendgrid when a user registers (I use devise for authentication). I had this working fine in my_mailer.rb using SMTP as follows:
def confirmation_instructions(record, token, opts={})
# SMTP header for Sendgrid - v2
# headers["X-SMTPAPI"]= {
# "sub": {
# "-CONFIRM_TOKEN-": [
# token
# ]
# },
# "filters": {
# "templates": {
# "settings": {
# "enable": 1,
# "template_id": "1111111-1111-1111-1111-111111111111"
# }
# }
# }
# }.to_json
However, prompted by Sendgrid to use v3 syntax to support newer mail templates, I changed code to the following (from the sendgrid help docs, as opposed to a real understanding):
def confirmation_instructions(record, token, opts={})
require 'sendgrid-ruby'
include SendGrid
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
data = JSON.parse('{
"substitutions": {
"-CONFIRM_TOKEN-": [
token
],
"template_id": "1111111-1111-1111-1111-111111111111"
}')
response = sg.client.mail._("send").post(request_body: data)
puts response.status_code
puts response.body
puts response.parsed_body
puts response.headers
Now I get the error message:
'NoMethodError (undefined method `include' for #<MyMailer:0x0000000003cfa398>):'
If I comment out the 'include' line I get:
'TypeError (no implicit conversion of nil into String):' on the line: "sg = SendGrid..."
I use the Gem: sendgrid-ruby (5.3.0)
Any ideas would be appreciated - I've been trying to hit on the correct syntax by trial-and-error for a while now and finally admit I am stuck.
UPDATE#1:
The first issue was I was using the wrong API_KEY env. variable (copied from 2 different help docs): "SENDGRID_API_KEY" (in code) vs. SENDGRID_APIKEY_GENERAL (set in Heroku). Fixed.
UPDATE #2:
With the "include" line commented out I now seem to be getting a JSON parse error:
JSON::ParserError (416: unexpected token at 'token
So my 2 current issues are now:
(1) I would like 'token' to be the confirmation token variable but it is not being passed
(2) Sending the below simple (1 line) content of 'data' does not throw up an error, but the appropriate template within Sendgrid is not selected:
data = JSON.parse('{
"template_id": "1111111-1111-1111-1111-111111111111"
}')
UPDATE #3:
Here's an update on the status of my issue and exactly where I am now stuck:
This code works fine (using Sendgrid v2 which I am trying to upgrade from):
def confirmation_instructions(record, token, opts={})
#
# SMTP header for Sendgrid - v2
# This works fine
#
headers["X-SMTPAPI"]= {
"sub": {
"-CONFIRM_TOKEN-": [
token
]
},
"filters": {
"templates": {
"settings": {
"enable": 1,
"template_id": "1111111-1111-1111-1111-111111111111"
}
}
}
}.to_json
This Sendgrid v3 code does not work (the email does get sent via Sendgrid but it does not select the template within Sendgrid - it just uses whatever code is in app/views/my_mailer/confirmation_instructions.html.erb):
#
# Sendgrid API v3
# This sends an email alright but it takes content from app/views/my_mailer/confirmation_instructions.html.erb
# It DOES NOT select the template within Sendgrid
#
data = JSON.parse('{
"template_id": "1111111-1111-1111-1111-111111111111",
"personalizations": [
{
"substitutions": {
"-CONFIRM_TOKEN-": "'+token+'"
}
}
]
}')
sg = SendGrid::API.new(api_key: ENV['SENDGRID_APIKEY_GENERAL2'])
response = sg.client.mail._("send").post(request_body: data)
puts response.status_code
puts response.body
puts response.parsed_body
puts response.headers
As always, any insight appreciated.
For anyone trying to get SendGrid v3 API working with Ruby/Devise/Heroku and use SendGrid's dynamic transactional emails these tips may help you. I spent a week getting this to work and these steps (& mistakes I made) were not apparent in the various documentation:
Generating the SendGrid API key (on SendGrid website): when generating the key, the API key only appears once allowing you to copy it, from then on it is invisible. As I could not see the key later I mistakenly used the "API Key ID" in my Heroku environment variables, rather than the true API Key.
Ensure the name you give the key in Heroku (for me: "SENDGRID_APIKEY_GENERAL") matches the code you use to reference it i.e. sg = SendGrid::API.new(api_key: ENV['SENDGRID_APIKEY_GENERAL'])
For sending variables to be substituted in the template use "dynamic_template_data" and not "substitutions". This should be within the "personalizations" section (see code example below).
I found it useful to refer to the Sendgrid dynamic template ID by using an environment variable in Heroku (for me: 'SENDGRID_TRANS_EMAIL_CONFIRM_TEMPLATE_ID') as opposed to hard-coding in Ruby (just allowed me to experiment with different templates rather than changing code).
The correct syntax for using a variable in the JSON string in Ruby is e.g. "CONFIRM_TOKEN": "'+token+'" (see code example below)
Do not use other characters in the name: i.e. "CONFIRM_TOKEN" worked but "-CONFIRM_TOKEN-" did not work
In the HTML of the transactional email template on SendGrid use this syntax for the substitution: {{CONFIRM_TOKEN}}
When creating a transactional template on SendGrid you can only have a 'design' view or a 'code' view not both. You must select at the start when creating the template and cannot switch after.
In the devise confirmations_instructions action refer to the user as a record (e.g. email) as record.email
Gemfile: gem 'rails', '5.2.2' ruby '2.6.1' gem 'devise', '4.6.1' gem 'sendgrid-ruby', '6.0.0'
Here is my successful ruby code that I have in my_mailer.rb:
def confirmation_instructions(record, token, opts={})
data = JSON.parse('{
"personalizations": [
{
"to": [
{
"email": "'+record.email+'"
}
],
"subject": "Some nice subject line content",
"dynamic_template_data": {
"CONFIRM_TOKEN": "'+token+'",
"TEST_DATA": "hello"
}
}
],
"from": {
"email": "aaaa#aaaa.com"
},
"content": [
{
"type": "text/plain",
"value": "and easy to do anywhere, even with Ruby"
}
],
"template_id": "'+ENV['SENDGRID_TRANS_EMAIL_CONFIRM_TEMPLATE_ID']+'"
}')
sg = SendGrid::API.new(api_key: ENV['SENDGRID_APIKEY_GENERAL'])
response = sg.client.mail._("send").post(request_body: data)
puts response.status_code
puts response.body
puts response.headers
You cannot include a module in a method. You have to include it in your class, so outside of the methode, like
class SomethingMailer
require 'sendgrid-ruby'
include SendGrid
def confirmation_instructions(record, token, opts={})
...
end
end
For your third update problem:
You are not sending a JSON but instead you are creating a JSON, then parsing it into a hash, then sending that hash, instead of the JSON.
JSON.parse #parses a JSON into a Hash
You should do the opposite and have a hash that you transform into a JSON
Something like
data = {
template_id: your_template_id # or pass a string
personalizations: [
...
]
}
Then you call
data_json = data.to_json
response = sg.client.mail._("send").post(request_body: data_json)
However this does not explain why your template in app/views/my_mailer/confirmation_instructions.html.erb gets sent. So I think you are either calling a different mailer_method at the same time, or you are not calling your actual confirmation_instructions method at all. Try to confirm that you SendGrid Methods actually is called and see what it returns. It should have returned some kind of error before, when you were sending a hash instead of a string.

How do I find the payload of a Sendgrid email sent by ActionMailer?

I'm working with SendGrid support to determine why categories stopped working on my multipart email campaigns (the text-only one is fine). If I intentionally set the content-type of an HTML email as "text/plain" the email displays the header data, text and raw html all on a single email, but will get its category. Otherwise the email looks correct, but there's no category.
SendGrid has asked me to send them a copy of the payload and I'm not sure what that is or how to find it. They said "If you are familiar with running a telnet test then that is what we are looking for." I'm not familiar with telnet tests. This is the info from the screenshot they provided as an example of what they're looking for:
220 Hi! This is Rob's hMailServer!
ehlo panoply-tech.com
250-SAGE013963
250-SIZE 20480000
250 AUTH LOGIN PLAIN
AUTH LOGIN
334 VXN1ea5bbVUG
YT3TQBHbhM9WBHKTDGUjeD65WQ20=
235 authenticated.
MAIL FROM: mayes#panoply-tech.com
250 OK
RCPT TO: cstickings#demosagecrm.com
250 OK
DATA
354 OK, send.
Subject: This is a test email
Hi Clemence,
Just sending you a test email.
.
250 Queued <25.927 seconds>
I went to .rvm/gems/ruby-2.3.3/gems/actionmailer-4.2.8/lib/action_mailer/base.rb and found a method called "set_payload_for_mail" but what that produces does seem to be like their example:
{"mailer":"B2c::B2cSendGridMailer",
"message_id":"5d0b979767c26_16f2c3fc04043f9c84968e#Domain-Person.local.mail",
"subject":"TEST: 26_txt","to":["person#domain.com"],
"from":["info#another.com"],"date":"2019-06-20T09:26:31.000-05:00",
"mail":"Date: Thu, 20 Jun 2019 09:26:31 -0500\r\nFrom: info#another.com\r\nTo: person#domain.com\r\nMessage-ID: \u003c5d0b979767c26_16f2c3fc04043f9c84968e#Domain-Person.local.mail\u003e\r\nSubject: TEST: 26_txt\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n
X-SMTPAPI: {\"category\":[\"html_false\"]}\r\n
X-SMTPAPI: {\"filters\": {\"ganalytics\": {\"settings\": {\"enable\":1}}}}
\r\n\r\nHi there, but text\r\n"}
I know in the Google inbox, you can click "Show Original" for an email and see the header info, etc. I've sent that to them but that didn't have what they needed.
def b2c_tester(html=false, content)
e_domain = 'careinhomes.com'
#mailer_path = "app/views/b2c/b2c_send_grid_mailer"
#from = "info#careinhomes.com"
#recipients = ['gina#pipelinesuccess.com']
#subject = html ? "#{DateTime.now.minute.to_s}_html" :
"#{DateTime.now.minute.to_s}_txt"
header_category = {"category": ["html_#{html}"]}
headers['X-SMTPAPI'] = header_category.to_json
if html
msg = tester_mail_with_opts({domain: e_domain}, content)
else
msg = tester_mail_plain_text_with_opts(
"b2c_tester",{domain: e_domain})
end
msg
end
#content ex: 'text/plain', 'text/html', 'multipart/alternative', etc
def tester_mail_with_opts(delivery_options={}, content=nil)
mail_opts = set_mail_opts(delivery_options)
unless content.nil?
mail_opts[:content_type] = content
end
mail mail_opts
end
def set_mail_opts(delivery_options={})
#subject = "TEST: #{#subject}" unless Rails.env.production?
# Required
mail_opts = {
to: #recipients,
from: #from,
subject: #subject,
}
mail_opts[:template_path] = #template_path if #template_path
mail_opts[:content_type] = #content_type if #content_type
# Do delivery options
mail_opts[:delivery_method_options] = DELIVERY_OPTIONS
mail_opts[:delivery_method_options] =
mail_opts[:delivery_method_options].merge(delivery_options)
unless delivery_options.blank?
mail_opts
end
In ActionMailer's base model is a method called deliver_mail that extracts the payload and you can capture it that way. It appears that, for my problem, the payload is an empty hash.
This is what a healthy payload should look like from ActionMailer:
{"mailer":"B2c::B2cSendGridMailer","message_id":"5d10dacb26dc2_17fb93ff52483b9c8952bf#Domain-Person.local.mail","subject":"TEST: 14_txt","to":["person#domain.com"],"from":["info#another.com"],"date":"2019-06-24T09:14:35.000-05:00","mail":"Date: Mon, 24 Jun 2019 09:14:35 -0500\r\nFrom: info#another.com\r\nTo: person#domain.com\r\nMessage-ID: \u003c5d10dacb26dc2_17fb93ff52483b9c8952bf#Domain-Person.local.mail\u003e\r\nSubject: TEST: 14_txt\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\nX-SMTPAPI: {\"category\":[\"html_false\"]}\r\nX-SMTPAPI: {\"filters\": {\"ganalytics\": {\"settings\": {\"enable\":1}}}}\r\n\r\nHi there, but text\r\n"}

ruby on rails action mailer not receiving emails to arrays from a yaml file

I have an issue where i am able to send email via the mailer for a hardcoded array.
But the email does not go through for an array picked up from config.yml
Here is my config.yml
company:
email:
- user1#company.com
- User1.Lastname#company.com
- User2.Lastname#company.com
This is my mailer class:
class ReportMailer < ActionMailer::Base
default :from => "donotreply#company.com"
def send_report(company, file)
mail(:to=> "#{CONFIG[company]['email']}", :subject => "Daily Report")
end
end
end
when run it in my rails console & view the logs, seems like everything was executed fine but I did not receive my email:
[DEBUG] 2016-04-21 18:21:29 :: Date: Thu, 21 Apr 2016 18:21:29 -0400
From: donotreply#merchantlink.com
to: ["user1#company.com", "User1.Lastname#company.com","User2.Lastname#company.com"]
...
...
[INFO] 2016-04-21 18:21:29 ::
Sent mail to ["user1#company.com", "User1.Lastname#company.com", "User2.Lastname#company.com"]
If i change my code & replace it with hardcoded array instead of reading from config.yml it works fine.
Am i reading the yaml array wrong?
As correctly pointed out in the comment by #Alfie, you are passing a stringified array to to:.
CONFIG[company]['email'] returns an array, then string interpolation calls to_s on it and you end up with
"['user1#company.com', 'User1.Lastname#company.com','User2.Lastname#company.com']"
Just pass in the array without inserting it into a string:
mail(:to=> CONFIG[company]['email'], :subject => "Daily Report")

Unable to parse Sendgrid parse api raw callback into Griddler email object

I am trying to accomplish the following:
Have all emails send to my domain parsed for the content of their to, body, subject line and ... in order for me to construct a fax object from it and send it as a fax
So far I have been able to setup my MX record to point to Sendgrid and from Sendgrid's Parse API using Ngrok I get the callback in my localhost server. The next step is to get the raw object into an object I could work with. This lead me to Thoughbot's Griddler Gem.
This is how I have setup my app so far:
Sendgrid Setup:
URL: https://8c00fab0.ngrok.io/email_processor | Spam Check and Send Raw are both checked
As mentioned before this part works since I do receive this:
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"}
Gemfile
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
I end up giving a Completed 500 Internal server error - Since I do not pass a 200 OK back to Sendgrid but the important part is that I get the following error in my log:
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'
I think the issue is that you're bypassing the work Griddler does on the controller side. You're pointing Sendgrid at a controller and action you created yourself. Instead I think you want to point to Griddler's controller and action. See the README under "Installation".
Change your routes.rb to be:
# your own route
# post '/email_processor' => 'fax#sendgrid'
# griddler's
post '/email_processor' => 'griddler/emails#create'
If you take a look at Griddler's controller you'll see that there are a few things it does for you to get things into your email processor class.
The way I do this in Rails is to gather the email params in the Controller.
For instance, in a sample app I created, people email selfies to our Parse API. In my selfie_controller.rb, I have a route '/inbound' and I have the following:
def inbound
if !params['attachment1'].nil? and params['attachment1'].content_type == "image/jpeg"
Selfy.create!({ pic: params['attachment1'], email: params['from'] })
end
end
The call to Selfy.create! uses the selfy.rb model, but I gather the params from the email to send to that from the controller.
Is this something you could try?

Resources