php-i18n Digest 8 Mar 2009 17:36:29 -0000 Issue 419

Topics (messages 1302 through 1306):

Re: mail-command and UTF-8
        1302 by: Jochem Maas
        1303 by: Darren Cook
        1304 by: Darren Cook
        1305 by: Dietrich Bollmann

Solved: mail-command and UTF-8
        1306 by: Roman Blöth

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Roman Blöth schreef:
> Dear list,
> 
> 
> I have just joined this mailing list hoping to find advice on my
> mail()-problem, since even after over 1.5 hours of googling I still
> don't know how to solve this (hope this is the correct list for my problem):
> 
> In order to overcome any codepage-problems I coded a website completely
> using UTF-8. On this website there is a contact form starting like this:
> 
> <form id="kontaktformular_intern" action="$_SERVER[PHP_SELF]"
> method="post" enctype="multipart/form-data" accept-charset="UTF-8">
> 
> So I believe the code will receive all UTF-8-text for sending the mail.
> 
> To send the mail I coded the following:
> 
> function sendEmail($to, $from, $subject, $message) {
>   $header = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" .
> 'X-Mailer: PHP/' . phpversion();
>   $header .= "\nContent-Type: text/plain; charset=UTF-8";
>   $header .= "\nContent-transfer-encoding: quoted-printable\n";
> 
>   $subject = utf8_decode($subject);
>   $subject = mb_encode_mimeheader($subject,
> mb_internal_encoding(),"B","\n");
>   return mail('[email protected]', $subject, $message, $header);
> }
> 
> This is what I had found some day in order to send UTF-8-encoded emails.
> Here with WindowsXP/Thunderbird everything works fine. But a friend of
> mine using some Windows and some Outlook will always receive srumbled
> german umlauts...
> 
> So I have tried to fumble with quoted-printable and base64-encoding, but
> it only gets worse...
> 
> Unfortunately I don't have the PEAR-library available on that server
> (unless I find out how to "install" it without ssh-access), so I will
> have to stick with using PHP's mail()-function.

assuming we're talking about some PEAR code and not a PECL extension then
you can just grab the relevant files from the PEAR site and upload
them to your site same as you do all your other code ... PEAR stuff
doesn't actually need to be installed ... it just comes with a cool management
tool that makes keeping stuff up2date and avoiding multiple copies, easier.

> 
> Can anyone give me any advice on how to accomplish this? The goal should
> be to send emails with german umlauts readable under linux, windows and
> os x.
> 
> 
> Thanks a load in advance and best regards,
> Roman.

--- End Message ---
--- Begin Message ---
> function sendEmail($to, $from, $subject, $message) {
>   $header = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" .
> 'X-Mailer: PHP/' . phpversion();
>   $header .= "\nContent-Type: text/plain; charset=UTF-8";
>   $header .= "\nContent-transfer-encoding: quoted-printable\n";
> 
>   $subject = utf8_decode($subject);
>   $subject = mb_encode_mimeheader($subject,
> mb_internal_encoding(),"B","\n");
>   return mail('[email protected]', $subject, $message, $header);
> }

Does dropping the "quoted-printable" header make any difference? As far
as I can see you are sending actual 8-bit UTF-8, not quoted-printable.

> This is what I had found some day in order to send UTF-8-encoded emails.
> Here with WindowsXP/Thunderbird everything works fine. But a friend of
> mine using some Windows and some Outlook will always receive srumbled
> german umlauts...

As you didn't say explicitly, have you confirmed he can receive an email
(e.g. sent by thunderbird) in UTF-8 encoding, with umlauts, and it
displays okay?

If so, send both versions to yourself (i.e. sent by thunderbird, and
sent by PHP mail()) and look at the source to see what is different.

Darren



-- 
Darren Cook, Software Researcher/Developer
http://dcook.org/mlsn/ (English-Japanese-German-Chinese-Arabic
                        open source dictionary/semantic network)
http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)

--- End Message ---
--- Begin Message ---
>   $subject = mb_encode_mimeheader($subject,
> mb_internal_encoding(),"B","\n");
>   return mail('[email protected]', $subject, $message, $header);

Sorry, I missed this earlier: if you have mb_encode_mimeheader() then
you also have mb_send_mail(), which does all the hard work for you.

http://php.net/mb_send_mail
http://jp.php.net/mb_language

So, this might replace almost (*) everything you wrote:
  mb_language('uni');
  mb_send_mail('[email protected]', $subject, $message);

Darren

*: Except the reply-to header. You can add that: mb_send_mail() takes
the same optional 4th parameter as mail().


--- End Message ---
--- Begin Message ---
On Sun, 2009-03-08 at 14:17 +0900, Darren Cook wrote:
> > function sendEmail($to, $from, $subject, $message) {
> >   $header = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" .
> > 'X-Mailer: PHP/' . phpversion();
> >   $header .= "\nContent-Type: text/plain; charset=UTF-8";
> >   $header .= "\nContent-transfer-encoding: quoted-printable\n";
> > 
> >   $subject = utf8_decode($subject);
> >   $subject = mb_encode_mimeheader($subject,
> > mb_internal_encoding(),"B","\n");
> >   return mail('[email protected]', $subject, $message, $header);
> > }
> 
> Does dropping the "quoted-printable" header make any difference? As far
> as I can see you are sending actual 8-bit UTF-8, not quoted-printable.

Hi Darren :)

Or, the other way around, is the $message really encoded with
"quoted-printable"?  (example: $message =
quoted_printable_encode($message);) 

I doubt, as the function 'quoted_printable_encode()' is available only
from PHP version 5.3.0 and most PHP installations still are 5.2.

You could use base64 encoding at the place of quoted printable:

When sending the string "Umlaute: äüöÄÜÖ" as subject and body using my
"evolution" mail program it gets encoded in the following way:

 [...]
 Subject: Umlaute: =?ISO-8859-1?Q?=E4=FC=F6=C4=DC=D6?=
 [...]
 Content-Type: text/plain; charset=UTF-8
 [...]
 Content-Transfer-Encoding: base64
 [...]

 VW1sYXV0ZTogw6TDvMO2w4TDnMOWDQoNCg==

So you could try "Q" for "Quoted-Printable" at the place of "B" for
"Base64" when encoding the subject, and "base64" as transfer encoding.
Of course you first have to encode the "$message" with something like
'$message = base64_encode($message);' (which should be available even on
older PHP installations).

Nowadays probably most of the internet can deal with 8-bit encodings
like UTF-8.  But with base64 you can be sure that the content doesn't
get messed up during the mail transfer at least.

Dietrich


> > This is what I had found some day in order to send UTF-8-encoded emails.
> > Here with WindowsXP/Thunderbird everything works fine. But a friend of
> > mine using some Windows and some Outlook will always receive srumbled
> > german umlauts...
> 
> As you didn't say explicitly, have you confirmed he can receive an email
> (e.g. sent by thunderbird) in UTF-8 encoding, with umlauts, and it
> displays okay?
> 
> If so, send both versions to yourself (i.e. sent by thunderbird, and
> sent by PHP mail()) and look at the source to see what is different.
> 
> Darren
> 
> 
> 
> -- 
> Darren Cook, Software Researcher/Developer
> http://dcook.org/mlsn/ (English-Japanese-German-Chinese-Arabic
>                         open source dictionary/semantic network)
> http://dcook.org/work/ (About me and my work)
> http://dcook.org/blogs.html (My blogs and articles)
> 



--- End Message ---
--- Begin Message ---
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear Darren, dear List,

Darren Cook schrieb:
> As you didn't say explicitly, have you confirmed he can receive an email
> (e.g. sent by thunderbird) in UTF-8 encoding, with umlauts, and it
> displays okay?
> 
> If so, send both versions to yourself (i.e. sent by thunderbird, and
> sent by PHP mail()) and look at the source to see what is different.
This was an obvious method which didn't come to my mind. Thank you very
much!
And thanks a lot to all of you who have answered to my request.

Again for all of you: My problem was "how to send UTF-8 - encoded emails
using the simple mail()-command, that display german umlauts well even
in outlook?"

The solution is very simple:

function sendEmail($to, $from, $subject, $message) {
  // For security reasons: Make sure to parse the parameters
  // before passing them to this function, or extend the code
  // accordingly:

  $charset = 'UTF-8';
  $encoded_subject = "=?$charset?B?".base64_encode($subject)."?=\r\n";
  $headers = "From: " . $from . "\r\n"
    . "Content-Type: text/plain; charset=$charset; format=flowed\r\n"
    . "MIME-Version: 1.0\r\n"
    . "Content-Transfer-Encoding: 8bit\r\n"
    . "X-Mailer: PHP\r\n";

  return mail($to, $encoded_subject, $message, $headers);
}

As I found very few helpful information on this matter on the web and
probably more people have to cope with this problem than there are
experts out there, I hope this solution will be of use for more coders.
There are loads of possibilities to send emails, and there are loads of
header informations you can stuff inside, but finally and hopefully most
of you will only want to use a _working_ UTF-8-solution. We're in 2009
after all...

This solution might not be the best, and it doesn't solve the problem of
emails going right into the recipients (or the recipients mail
provider's) spam folder (for this you should send your emails using SMTP
with login-name and -password, e.g. using PEAR's "Mail.php"). But it
solved my problem.


Best regards,
Roman Blöth.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJtAH4CShhkGXCcigRAvaQAJ97uMZMsdLpVYJ0PDx2rgVzuSHyJwCgj2GH
hQODwinJh4I/x38KsC67XRY=
=tlGG
-----END PGP SIGNATURE-----

--- End Message ---

Reply via email to