PDA

View Full Version : Configuring HTML emails with PHP


minus273
07-01-2006, 09:27 AM
Hi,

I am using a PHP script to send emails via a contact form on a web page. The only problem is that when viewing the emails in Outlook 2003, they arrive like this in a very unformatted way:

Content-type: text/html; charset=iso-8859-1 From:
X-NAS-Language: English
X-NAS-Bayes: #0: 6.17484E-006; #1: 0.999994
X-NAS-Classification: 0
X-NAS-MessageID: 141
X-NAS-Validation: {DFF16927-88E6-4EAA-A097-460B7E65289B}


<html> <head> <title>Contact Form</title> </head> <body> <p><strong>Reason:</strong> </p> <p><strong>Name:</strong> Paul</p> <p><strong>E-mail:</strong> sdvs@ddos.com</p> <p><strong>Comment / Question:</strong></p> <p>sdvsd</p> <p><strong>Mailing List:</strong> </p> <p><strong>Sent on:</strong> Wed, Jun 14th 2006 / 9:00pm</p> <p><strong>Logged IP Address:</strong> 86.138.68.196</p> </body></html>

Now, all of the other html emails that i recieve from other sources look as they should. The PHP code that i'm using to send these looks like so:

$time = time();
$datetime = date("D, M jS Y / g:ia", $time);
$ip = $_SERVER["REMOTE_ADDR"];
$message = '
<html>
<head>
<title>'.$subject.'</title>
</head>

<body>
<p><strong>Reason:</strong> '.$Reason.'</p>
<p><strong>Name:</strong> '.$Name.'</p>
<p><strong>E-mail:</strong> '.$Email.'</p>

<p><strong>Comment / Question:</strong></p>
<p>'.SafeHTML($Comment).'</p>

<p><strong>Mailing List:</strong> '.$Mailing.'</p>

<p><strong>Sent on:</strong> '.$datetime.'</p>
<p><strong>Logged IP Address:</strong> '.$ip.'</p>
</body></html>';

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $name\r\n";
mail($to, $subject, $message, $headers);

Any ideas anyone?

Many thanks.

Joe
07-01-2006, 11:10 AM
Are your settings in Outlook configured to view HTML emails and not just plain text?

minus273
07-01-2006, 11:26 AM
Yes, it is configured to view HTML; all emails form Amazon and other commercial organizations look fine and display correctly.

Incidentally Joe, this script was from you (an awesome script), I have just adapted it a little. Maybe I have done something wrong in translation!

Joe
07-01-2006, 01:57 PM
I thought it looked familiar. You haven't done anything wrong with your script as far as I can see, looks fine.

Just for testing, try using these headers and see if they make a difference, I use them and they work fine for me.

$headers = "From: Contact Form\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);

See what happens with that.

minus273
07-01-2006, 04:32 PM
You're a legend, that seems to have done the trick!

Just one more thing, in the viewing pane in Outlook, the 'From' displays the website IP address; how can i configure it so that bit shows the senders name?

That original cutting of script I posted left the 'From' blank.

Cheers mate.

Joe
07-01-2006, 04:58 PM
Glad it worked.

Change;

$headers = "From: Contact Form\r\n";

To

$headers = "From: " $Name;

And that should display the senders name in the From field then. Originally I think it was this header that was causing you the problem ... as it was the only bit that I actually changed.

minus273
07-01-2006, 05:18 PM
That seemed to crash the script completely, but not to worry, i'll have a play around and see what I can come up with.

Cheers for your help, much appreciated.

Joe
07-02-2006, 05:43 AM
Did it? Try this instead then;

$headers .= "From: $Name\r\n";

This should work now.

minus273
07-02-2006, 10:59 AM
Thats done it. Cheers mate.