Skip to content

Commit

Permalink
Merge pull request tpitale#147 from tjaartvdwalt/master
Browse files Browse the repository at this point in the history
UTF-8 encode messages for Que deliverer
  • Loading branch information
stanhu committed Jan 13, 2023
2 parents 6beac47 + 602a198 commit 50bc2fb
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/mail_room/delivery/que.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pg'
require 'json'
require 'charlock_holmes'

module MailRoom
module Delivery
Expand Down Expand Up @@ -34,7 +35,7 @@ def initialize(options)
# deliver the message by pushing it onto the configured Sidekiq queue
# @param message [String] the email message as a string, RFC822 format
def deliver(message)
queue_job(message)
queue_job(utf8_encode_message(message))
@options.logger.info({ delivery_method: 'Que', action: 'message pushed' })
end

Expand All @@ -58,6 +59,19 @@ def queue_job(*args)

connection.exec(sql, [options.priority, options.job_class, options.queue, JSON.dump(args)])
end

def utf8_encode_message(message)
message = message.dup

message.force_encoding("UTF-8")
return message if message.valid_encoding?

detection = CharlockHolmes::EncodingDetector.detect(message)
return message unless detection && detection[:encoding]

# Convert non-UTF-8 body UTF-8 so it can be dumped as JSON.
CharlockHolmes::Converter.convert(message, detection[:encoding], 'UTF-8')
end
end
end
end

0 comments on commit 50bc2fb

Please sign in to comment.