...

View Full Version : Issues sending simple HTML mail



Arnaud
01-20-2010, 05:23 PM
Hi Folks!

I am trying to send a simple HTML e-mail but it fails. Here is the code:



<?
$to = 'arnaud.xxx@xxx.org';
$subject = 'Test email';
$message = '
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Here some text!</p>
<table>
<tr>
<td>Here some more text!</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
?>


The mail goes out correctly but when I receive it in my e-mail client (whatever I use, Outlook, OWA, iPhone) the formatting is not done. I basically receive the HTML code as text...

This is how my e-mail client shows it (message body):


-------------------------------------

Content-type: text/html; charset=iso-8859-1

Message-Id: <20100120165728.9BC6C2E20E@helpdesk.xxxx.local>
Date: Wed, 20 Jan 2010 17:57:28 +0100 (CET)
From: arnaud.xxx@xxx.org (WWW daemon apache)

<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Here some text!</p>
<table>
<tr>
<td>Here some other text</td>
</tr>
</table>
</body>
</html>

-------------------------------------

Any idea what I am doing wrong? Or am I missing something on the PHP server? Thanks in advance. This is not the first time I use PHP generated HTML e-mail but never got to something similar...

angst
01-20-2010, 05:26 PM
your code seems correct according to the manual,

this is a function that I wrote a few years ago that should solve this issue for you:


// Format and Sends HTML & TXT Email at the same time
function SendHTMLTxTEmail($From, $FromName, $To, $ToName, $Subject, $Text, $Html) {
$OB="----=_OuterBoundary_000";
$IB="----=_InnerBoundery_001";
$Html=$Html?str_replace(array("\r","\n","\t"), "", $Html):preg_replace("/\n/","<br>",$Text)
or die("neither text nor html part present.");
$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
$From or die("sender address missing");
$To or die("recipient address missing");

$headers ="MIME-Version: 1.0\n";
$headers.="From: ".$FromName." <".$From.">\n";
// $headers.="To: ".$ToName." <".$To.">\n";
$headers.="Reply-To: ".$FromName." <".$From.">\n";
// $headers.="X-Priority: 1\n";
// $headers.="X-MSMail-Priority: High\n";
// $headers.="X-Mailer: My PHP Mailer\n";
$headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";

//Messages start with text/html alternatives in OB
$Msg ="This is a multi-part message in MIME format.\n";
$Msg.="\n--".$OB."\n";
$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

//plaintext section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/plain;\n\tcharset=\"Windows-1252\"\n";
$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$Msg.=$Text."\n\n";

// html section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/html;\n\tcharset=\"Windows-1252\"\n";
$Msg.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$Msg.=chunk_split(base64_encode($Html))."\n\n";

// end of IB
$Msg.="\n--".$IB."--\n";

//message ends
$Msg.="\n--".$OB."--\n";

//if(USE_SMTP){
// $this->mymail($To,$Subject,$Msg,$headers);
//} else {
mail($To,$Subject,$Msg,$headers);
//}
}

Arnaud
01-20-2010, 05:37 PM
Wow, man, that was a quick response, quick implementation, and quick resolution!

Works fine, thanks a lot. But any idea why I need your function and why basic code does not work?

Thanks again!
Arnaud

angst
01-20-2010, 05:38 PM
no problem,
as I said I'm not exactly sure, I would need to do some testing, as your code does look correct and it right out of the php manual. are you by chance running this on a windows server?

Arnaud
01-20-2010, 05:42 PM
Ok. Don't spend too much time on this. I will try to figure out myself.

But no, this is not running on a Windows machine. PHP 5 on a standard linux apache 2 server.

angst
01-20-2010, 05:43 PM
hmm ok, well have a look at my function, that may give you some ideas.

Arnaud
01-20-2010, 05:44 PM
Sure! thanks for your contribution!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum