thoughtbot / griddler-sendgrid

SendGrid adapter for Griddler
MIT License
32 stars 35 forks source link

Getting "Invalid encoding for parameter" #37

Open denis-kos opened 1 year ago

denis-kos commented 1 year ago

Hi, I'm using griddler to accept incoming email. For some emails which has some non-English characters (like Bosnian language etc.) I'm getting following error. It looks like in most of the cases such emails are coming from Outlook clients (office365). Seems to be working fine with gmail. I was trying to use rack-utf8_sanitizer gem like this:

config.middleware.insert 0, Rack::UTF8Sanitizer, sanitizable_content_types: ['multipart/form-data', 'multipart/alternative', 'multipart/mixed'], only: ['rack.input']

It works, but if there is an attachment in the email, this breaks it. If I set Send Raw in the SendGrid configuration, the request is coming in fine, but then I don't have a body (as noted in this gem readme)

Any help would be greatly appreciated!

ActionController::BadRequest Invalid request parameters: Invalid encoding for parameter ``` Rack::QueryParser::InvalidParameterError: Invalid encoding for parameter: ...
� Moshe
...
denis-kos commented 1 year ago

I suspect this could be due to iso-8859-1 encoding of the body that is coming from outlook, but not sure how to deal with this.

"charsets": "{\"to\":\"UTF-8\",\"html\":\"iso-8859-1\",\"subject\":\"UTF-8\",\"from\":\"UTF-8\",\"text\":\"iso-8859-1\"}",
denis-kos commented 1 year ago

here is the param with invalid encoding:

> params
"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=koi8-u\">\n<style type=\"text/css\" style=\"display:none;\"> P {margin-top:0;margin-bottom:0;} </style>\n</head>\n<body dir=\"ltr\">\n<div style=\"font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);\" class=\"elementToProof\">\nƦȧ\xD7\xDAƧ\xA6\xD7<br>\n</div>\n</body>\n</html>\n"

> params.valid_encoding? # => false
> params.encoding # => #<Encoding:UTF-8>
GearoidDC commented 1 year ago

I'm having the same issue with emails from Outlook. I think it might be an issue with rails 6.1 and Griddler. This thread has the same type of error.

https://github.com/rails/rails/issues/41567

I think possibly skipping the encoding might work.


class Griddler::EmailsController < ActionController::Base
  skip_before_action :verify_authenticity_token, raise: false
  skip_parameter_encoding :create # Add this line
denis-kos commented 1 year ago

Thank you @GearoidDC. For now I have switched to griddler-mailgun but will try this skip_parameter_encoding option.

RomainMorlevat commented 2 weeks ago

Also got the issue with emails forwarded through Outlook.