Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:26:36+00:00 2026-06-07T12:26:36+00:00

Rails 3.0.11 ruby 1.9.3dev (2011-09-23 revision 33323) [x86_64-linux] mail gem v. 2.2.19 postfix v.

  • 0

Rails 3.0.11
ruby 1.9.3dev (2011-09-23 revision 33323) [x86_64-linux]
mail gem v. 2.2.19
postfix v. 2.7.0

I pipe received emails to an actionmailer. This usually works well unless the received email is not correctly encoded.

I have tried multiple approaches but always end up with an invalid byte sequence in UTF-8 error.

This is how I attempt to properly encode / force a proper encoding in the actionmailer:

email.text_part.decoded.force_encoding('UTF-8').encode('UTF-8', undef: :replace, invalid: :replace, replace: "")

This is how I forward mails to my app (content of the /etc/aliases file as suggested by the ruby on rails guide)

theaddress: "|/usr/local/rvm/bin/ruby-1.9.3-rc1 /var/www/myapp/script/rails runner -e mailer 'Mymailer.receive(STDIN.read)'"

I have also tried piping the mail through iconv first

theaddress: "|iconv -t UTF-8 -c |/usr/local/rvm/bin/ruby-1.9.3-rc1 /var/www/myapp/script/rails runner -e mailer 'Mymailer.receive(STDIN.read)'"

I also tried using Encding::Converter

Encoding::Converter.new("UTF-8//IGNORE", "UTF-8")

Thanks for any insight and help!

The Solution

I had to add following to my environment file:

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

Also I removed any force_encoding and encoding methods. So following now works:

email.text_part.decoded
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:26:41+00:00Added an answer on June 7, 2026 at 12:26 pm

    Are you sure the encoding of the incoming email is UTF-8? My solution was to grab the charset from the header.raw_source

    /charset=(?<charset>[^\s]+)\s?/ =~ part.header.raw_source
    

    and if that is not null, gsub out any quotes and try to encode that

    charset.gsub!('"', '')
    raw = part.body.to_s
    raw.force_encoding(charset)
    decoded = raw.encode("UTF-8")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for the ruby/rails gem that deals with the Authentication API for
I cannot figure out why rails/ruby cannot see this gem, despite each telling me
I'm having trouble understanding the difference between the jquery-rails ruby gem & jRails .
Using Rails 3.2.2 and ruby 1.9.3dev and mysql I am new to ruby and
Is there a Rails/Ruby idiom for checking if an enumerable is both present and
I'm writing a non-Rails ruby application (gasp!) and would like to be able to
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
I am using Sunspot (Ruby/Rails) on top of SOLR in my webapp. Now I
What are the best Ruby / Rails book available right now ? Note :
This is not a ruby/rails project deploy. I have the following situation and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.