6

How to encode mail subject in perl ?

Now I finally found something but it's still not working :

use MIME::Words qw/encode_mimewords/;
$recipientsubject = encode_mimewords('Votre fichier a bien été envoyé');

But the (bugged) result is :

Subject: Votre fichier a bien =?ISO-8859-1?Q?=E9t=E9?= =?ISO-8859-1?Q?envoy=E9?=

Which displays :

Votre fichier a bien étéenvoyé

(It eats some spaces)

matthias krull
  • 4,389
  • 3
  • 34
  • 54
Denis BUCHER
  • 310
  • 4
  • 16
  • 1
    Update you version MIME::Words. This is [bug #5462 in MIME:Tools](https://rt.cpan.org/Public/Bug/Display.html?id=5462), which has been fixed in MIME::Tools 5.504 in early 2013. – Palec Aug 31 '14 at 14:19

2 Answers2

17

Use Encode, it is a core module.

perl -Mutf8 -MEncode -E 'say encode("MIME-Header", "Votre fichier a bien été envoyé")'

… will output either one of:

=?UTF-8?Q?Votre=20fichier=20a=20bien=20?= =?UTF-8?Q?=C3=A9t=C3=A9=20envoy=C3=A9?=
=?UTF-8?B?Vm90cmUgZmljaGllciBhIGJpZW4gw6l0w6kgZW52b3nDqQ==?=

And decode with:

perl -C -MEncode -E 'say decode("MIME-Header", "=?UTF-8?Q?Votre=20fichier=20a=20bien=20?= =?UTF-8?Q?=C3=A9t=C3=A9=20envoy=C3=A9?=")'
perl -C -MEncode -E 'say decode("MIME-Header", "=?UTF-8?B?Vm90cmUgZmljaGllciBhIGJpZW4gw6l0w6kgZW52b3nDqQ==?=")'

Which will print:

Votre fichier a bien été envoyé

If you still have the same results, you should give more information on your Perl environment. The version is a good starter.

daxim
  • 39,270
  • 4
  • 65
  • 132
matthias krull
  • 4,389
  • 3
  • 34
  • 54
  • 4
    Pedantic note: With `decode` unless there is a specific reason to the contrary it's best to use `MIME-Header`, as it can handle both the MIME Q and B encodings. [RFC 2047](http://www.ietf.org/rfc/rfc2047.txt) states "a mail reader which claims to recognize 'encoded-word's MUST be able to accept either encoding for any character set which it supports." – Ven'Tatsu Aug 15 '12 at 14:13
  • 1
    Great, thanks a lot ! I did use Encode and used encode("MIME-q", "Votre fichier a bien été envoyé") – Denis BUCHER Aug 15 '12 at 17:04
  • Actually, Encode::MIME::Header is buggy and from what I saw in the bugtracker, the author is incompetent. He repeatedly closed correct bug reports, even when presented with valid references to the RFCs. Actually, because Encode::MIME::Header is a core module, many other modules rely on it and Perl script all over the internet send broken email. – Palec Jun 30 '15 at 05:41
  • Could you provide a link to those reports/examples and also maybe an alternative that works better for you? – matthias krull Jul 03 '15 at 12:54
2

Another module that handles MIME encoding of non-ASCII strings is Email::MIME::RFC2047. For example

use strict;
use warnings;
use utf8;

use Email::MIME::RFC2047::Encoder;
use Email::MIME::RFC2047::Decoder;

binmode(STDOUT, ':utf8');

my $encoder = Email::MIME::RFC2047::Encoder->new;
my $encoded = $encoder->encode_text('Votre fichier a bien été envoyé');
print "$encoded\n";

my $decoder = Email::MIME::RFC2047::Decoder->new;
my $decoded = $decoder->decode_text($encoded);
print "$decoded\n";

prints

Votre fichier a bien =?utf-8?Q?=c3=a9t=c3=a9_envoy=c3=a9?=
Votre fichier a bien été envoyé

Some benefits of Email::MIME::RFC2047 over Encode:

  • It tries hard to use MIME encoding for as few words as possible, also by using quoted strings in phrases.
  • It supports correct decoding of MIME phrases used in To, From, or Cc headers (impossible with Encode).
  • It supports other character sets than UTF-8.
  • It encodes space as underscore in MIME-Q encoded words.
  • It has fewer bugs than Encode (none that I know of).

Disclosure: I am the author of the module.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
  • In the module’s description you write about RFC 822. It’s the oldest one, obsoleted by RFC 2822, which is in turn obsoleted by RFC 5322. The most recent one (from 2008) is not supported well, AFAIK, but RFC 2822 (from 2001) is supported and fixes at least line folding, which is broken in RFC 822. Do you have a reason to prefer 822 over 2822? – Palec Jun 30 '15 at 05:32
  • @Palec I must admit that I'm pretty clueless about the differences between RFCs. But I think my module conforms to RFC 2822 and possibly RFC 5322 as well. The core classes for encoding and decoding don't fold lines, and only work with `phrase` and `text` productions. – nwellnhof Jun 30 '15 at 14:02
  • I’ll look into it more sometime. You have to perform line folding somewhere, because the RFCs impose a strong limit on how long a line containing an encoded word can be. If you don’t fold, you cannot encode arbitrarily long text. – Palec Jun 30 '15 at 17:22
  • 1
    @Palec I only make sure that MIME encoded words aren't longer than the maximum length (75 characters or so). Line folding is then done by a module that manages the headers like `Email::Simple`. – nwellnhof Jul 01 '15 at 18:23