Does Redmine have trouble reading incoming mails encoded with base64?

Added by Fredrik Frodlund over 11 years ago

We've got a proprietary CMS that can send emails from a support form for our customers. I noticed that the built-in feature is having problem accepting these emails into the correct project (or at all, for that matter). Whenever I tried to send from my own client (Mail.app on Leopard), the email gets accepted without any issues. When I was troubleshooting, I started out with looking at the email in raw text format and here's what it looks like (domain names and IP's removed):

X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on
  developx.domain.se
X-Spam-Level: 
X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00 autolearn=ham
  version=3.2.4
X-TFF-CGPSA-Version: 1.5
X-TFF-CGPSA-Filter: Scanned
Return-Path: <supportform@domain.se>
Received: from support@domain.se
by domain.se (CommuniGate Pro GROUP 5.2.0)
with GROUP id 116267; Tue, 01 Jul 2008 19:25:18 +0200
X-Autogenerated: group
Reply-To: support@domain.se
Received: from [21x.xxx.11x.xx] (HELO servername)
by domain.se (CommuniGate Pro SMTP 5.2.0)
with ESMTP id 116272 for support@domain.se; Tue, 01 Jul 2008 19:25:18 +0200
MIME-Version: 1.0
From: supportform@domain.se
To: support@domain.se
Date: 1 Jul 2008 19:25:16 +0200
Subject: Test 17
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
Message-ID: <auto-000000116272@domain.se>

RXR0IHN1cHBvcnTDpHJlbmRlIGZyw6VuIEt1bmQgS3VuZHNzb24gKGt1bmQpIHDDpSBGw7Zy
ZXRhZ2V0IEFCIGhhciBrb21taXQgaW4uIEVuIGtvcGlhIGF2IGRldHRhIG1haWwgaGFyIHNw
YXJhdHMgaSBkYXRhYmFzZW4uDQoNCitUZXN0IDE3Kw0KUHJvamVjdDogdGVzdDUwMDANClBy
aW9yaXRldDogS3JpdGlza3QNCkFkcmVzczogaHR0cDovL25ldHJlbGF0aW9uczIwMDcuZGV2
LnNob3V0LnNlL3N1cHBvcnQvYXJlbmRlbi9mb3JldGFnZXQtYWIvMjAwOC0wNy0wMS10ZXN0
LTE3Lw0KDQoNCsOEcmVuZGViZXNrcml2bmluZzoNCkphaGFwcA0K

It's the Content-Transfer-Encoding: base64 that I suspect is being the culprit here. Can anyone confirm this?

Replies (5)

RE: Does Redmine have trouble reading incoming mails encoded with base64? - Added by Fredrik Frodlund over 11 years ago

I've been RTFM:ing for a bit and while my Ruby skills are very limited at the moment, it seems that implementing Base64.decode64() in some way could resolve this.

I've noticed that some clients and webmail services encode their mails with Base64 (for example Gmail), so it might be a good idea to implement decoding capabilities to Redmine.

RE: Does Redmine have trouble reading incoming mails encoded with base64? - Added by Fredrik Frodlund over 10 years ago

After performing an update a few months back, Redmine was able to read email that were base64 encoded. However, after updating to 0.8.1, no more mail are coming in once again. When I look at the IMAP account, I can tell that the mail have been read but no new issue has been created from it.

I would really appreciate some help with this.

RE: Does Redmine have trouble reading incoming mails encoded with base64? - Added by Alex Rojo almost 9 years ago

I'm using redmine 1.0.3 and I'mhaving the same problem, anybody has a solution?

RE: Does Redmine have trouble reading incoming mails encoded with base64? - Added by Waleed Jaffar over 6 years ago

Hi Alex,

You can fix it this way.
In redmine_root/app/models/mail_handler.rb, modify code as below.

def plain_text_body
  return @plain_text_body unless @plain_text_body.nil?
  parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
  if parts.empty?
    parts << @email
  end
  plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
  if plain_text_part.nil?
    # no text/plain part found, assuming html-only email
    # strip html tags and remove doctype directive   
-   @plain_text_body = strip_tags(@email.body.to_s)
+   @plain_text_body = strip_tags(decode_body_if_base64_encoded(plain_text_part.body).to_s)
    @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
  else
-   @plain_text_body = plain_text_part.body.to_s
+   @plain_text_body = decode_body_if_base64_encoded(plain_text_part.body).to_s
  end
  @plain_text_body.strip!
  @plain_text_body
end

def decode_body_if_base64_encoded(mail_body)
  (@email.header_string('Content-Transfer-Encoding') == "base64") ? Base64.decode64(mail_body) : mail_body
end

Due to lack of time, I haven't tested above code change.
But for sure Base64.decode64(mail_body) will save your day.

Refer to below example in Rails Console:

1.9.3p194 :227 > mail_body
 => "KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq\nKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq\nKgpUSElTIElTIEFOIEFVVE9NQVRFRCBFTUFJTCAtIFBMRUFTRSBETyBOT1Qg\nUkVQTFkuCioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq\nKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq\nKioqKioqKioKCgpEZWFyIE5ldyBNb25leWJvb2tlcnMgQ3VzdG9tZXIsCgpZ\nb3UgaGF2ZSBjaG9zZW4gbWJfdGVzdF9jb21wYW55QHRhcnluZWFzdC5jb20g\nYXMgdGhlIGVtYWlsIGFkZHJlc3MgZm9yIHlvdXIgbmV3IE1vbmV5Ym9va2Vy\ncyBhY2NvdW50LiAKClBsZWFzZSB2ZXJpZnkgdGhpcyBlbWFpbCBhZGRyZXNz" 
1.9.3p194 :228 > Base64.decode64(mail_body)
 => "*******************************************************************************************\nTHIS IS AN AUTOMATED EMAIL - PLEASE DO NOT REPLY.\n*******************************************************************************************\n\n\nDear New Moneybookers Customer,\n\nYou have chosen mb_test_company@taryneast.com as the email address for your new Moneybookers account. \n\nPlease verify this email address" 
1.9.3p194 :229 >

(1-5/5)