You are here: mail() encoding problems « PHP Programming Language « IT news, forums, messages
mail() encoding problems

Posted by amygdala on 08/19/07 14:16

Hi all,

Another problem that has been bugging me for a while now, but which I swept
under the rug too long too now is a mail encoding problem at my (shared)
webhost.

The problem is that on different occassions when I send the exact same mail
(same e-mail address, same name, same body content) through my site's
contactform it produces the wellknown strange characters instead of the
intended diacritical characters. This problem also shows up at the
contactform for a client of mine, which site is hosted on the same webhost.
I've contacted my webhost a few times already, but they don't know what
could be the cause of the problem either.

This is the setup I'm using on my webhost:

The host runs fast-cgi PHP5 with customizable php.ini files per
domain/website on Slackware Linux.

All my php files are saved as 1252 ANSI. All my HTML output have

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

meta tags. The difference in these two encodings shouldn't be a problem is
my guess. But perhaps I'm wrong?

And my Mailer class uses the following code to send mail:

public function send()
{
if( is_null( $this->getErrors() ) )
{
$cc = implode( ', ', $this->cc );
$bcc = implode( ', ', $this->bcc );
$to = implode( ', ', $this->to );
$headers = 'From: ' . $this->from . LF;
if ( !empty( $cc ) ) $headers .= 'Cc: ' . $cc . LF;
if ( !empty( $bcc ) ) $headers .= 'Bcc: ' . $bcc . LF;
$headers .= 'MIME-Version: 1.0' . LF;
$headers .= 'X-Mailer: ' . $this->mailerName . LF;
$headers .= 'Content-Type: text/plain; charset=iso-8859-1' . LF;
$headers .= 'Content-Transfer-Encoding: 8bit' . LF;
$subject = $this->subject;
$body = $this->body;
if( mail( $to, $subject, $body, $headers ) )
{
return true;
}
$this->setError( 'general', 'error', basename( __FILE__, '.php' ),
__LINE__ );
return false;
}
else
{
return false;
}
}

Sometimes, when I run in to the problem I change the following lines in my
send() function to utf8 encoding

$headers .= 'Content-Type: text/plain; charset=utf-8' . LF;

$body = utf8_encode( $this->body );

Then, it seems to work for a while. But then all of a sudden it shows the
same problem again. Eventhough I send the same test mail.

The body content I send is:

test λθο

Which shows up as:

test ëèï

Does anybody have any idea what might be causing this weird problem?

Thanks

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация