Question

I have somes problems with my php mailer, i receive only the source code not interprated.

Someone can check my headers?

$recipient = str_replace(Array("\r","\n"),"",$this->to);
$headers = 'From: "xxx.ch" <contact@xxx.ch> '."\r\n";
$headers .= 'Return-Path: <postmaster@xxx.ch>' . "\r\n";
if ( isset($this->replyTo) ){
    $headers .= 'Reply-To: contact@xxx.ch' . "\r\n";
}
$random_hash = md5(date('r', time()));
$headers .= "MIME-Version: 1.0 \r\n Content-Type: multipart/alternative; boundary=\"".$random_hash."\""; 
$body = '--'.$random_hash."\r".' 
         Content-Type: text/plain; charset="UTF-8"'."\r".'
         Content-Transfer-Encoding: 8bit'."\r".'

         Merci d\'utiliser un client mail supportant le format HTML'."\r".'

        --'.$random_hash."\r".'

        Content-Type: text/html; charset="UTF-8"'."\r".'
        Content-Transfer-Encoding: 8bit'."\r";
$body .= $this->HTMLBody ."\r".'--'.$random_hash.'--';

Thanks

Was it helpful?

Solution

While I would agree with other commenters that you should look into a 3rd-party library rather than doing this by hand, your current problem is probably to do with line-endings and whitespace, which MIME is very picky about.

You currently have lots of code like this:

$body = '--'.$random_hash."\r".' 
         Content-Type: text/plain; charset="UTF-8"'."\r".'
         Content-Transfer-Encoding: 8bit'."\r"; // (and so on)

You are carefully inserting a carriage return ("\r") but then embedding a newline and lots of whitespace into the next single-quoted string as well.

Instead, you should only include the carriage return, and make sure all other whitespace is outside the single quotes (you want the PHP to be readable, but for that not to affect the output):

$body = '--' . $random_hash . "\r"
         . 'Content-Type: text/plain; charset="UTF-8"'."\r"
         . 'Content-Transfer-Encoding: 8bit'."\r"; // (and so on)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top