Sending HTML Emails using PHP

software development

#1

Hello: Need some help here.

A Flash-based SWF file is collecting information via fields and submitting it to

The SWF file:

http://www.tomhanser.com/contact.html

The PHP code is here:

http://www.tomhanser.com/form.php

Code is:

<?php // To send the HTML mail we need to set the Content-type header. $headers = "MIME-Version: 1.0rn"; $headers .= "Content-type: text/html; charset=iso-8859-1rn"; $headers .= "From: $from\r\n"; $to = "tom@emailaddress.com"; $msg = "$name\n\n"; $msg .= "$message\n\n"; mail($to, $subject, $msg, "Message From: Online client\nReply-To: $email\n"); ?>

Problem is, the resulting email in my inbox is obviously in Plain Text format - the HTML tags appear.

I understand that the code snippet to send as HTML email is as follows:

// To send the HTML mail we need to set the Content-type header.
$headers = “MIME-Version: 1.0rn”;
$headers .= “Content-type: text/html; charset=iso-8859-1rn”;
$headers .= “From: $from\r\n”;

But this is not working - I still get a Plain Text email.

What am I doing wrong? What have I over looked?

Thanks,

Tom Hanser


#2

Tom, here’s the PHP documentation for the mail() function -
http://us.php.net/manual/en/function.mail.php

And here is how you are calling it:

mail($to, $subject, $msg, “Message From: Online client\nReply-To: $email\n”)

Checklist time!

  1. $to parameter = OK
  2. $subject parameter = OK
  3. $message parameter = OK
  4. optional $additional_headers parameter = ERROR
  5. optional $additional_parameters parameter = NONE

Wait a minute, if we have a $headers variable to use for the $additional_headers parameter, why are we passing a different string that has malformed headers to boot?

Customer since 2000 :cool: openvein.org


#3

Thanks for your reply, but as I’m not a PHP developer, I’m only cutting and pasting from existing code I’ve found online.

I wish I could run a bug check on this code somehow!

I’m still not able to receive HTML emails, and my code is now:

<?php // To send the HTML mail we need to set the Content-type header. $headers = "MIME-Version: 1.0rn"; $headers .= "Content-type: text/html; charset=iso-8859-1rn"; $to = "tom@tomhanser.com"; $msg = "$name\n\n"; $msg .= "$message\n\n"; mail($to, $subject, $msg, "Message From: Online client\nReply-To: $email\n"); ?>

What am I doing wrong?

Thanks.


#4

[quote]Thanks for your reply, but as I’m not a PHP developer, I’m only cutting and pasting from existing code I’ve found online.

I wish I could run a bug check on this code somehow!

I’m still not able to receive HTML emails, and my code is now:[/quote]
Copy and pasting code when you don’t know what it does will lead to trouble a lot of times. In your case though you don’t have to be a developer you just have to apply some problem solving skills.

You already know that in sending a message, you need a set of recipients, a subject, and a message, right? So $to, $subject and $message should be slam dunks…

But you know you need to add some headers that indicate the message is HTML. You already know what those headers are. But how do we use those headers when sending the message? Let’s see if we can do that with the mail() function. The documentation says:

[code]bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

$additional_headers (optional)

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). [/code]Sweet, we can specify additional headers! OK so we got our research done. Let’s apply what we’ve learned:

We need a $to
We need a $subject
We need a $message
We need a $additional_headers
And then we call
mail($to, $subject, $message, $additional_headers);

So, what do we have for our parameters so far?

$headers = “MIME-Version: 1.0rn”;
$headers .= “Content-type: text/html; charset=iso-8859-1rn”;
$to = "tom@tomhanser.com";
$msg = “$name\n\n”;
$msg .= “$message\n\n”;

Damn. We’re missing $subject. Let’s try again, renaming the variables though so I understand what the documentation says they are for… and duh I want to specify a reply-to address, almost forgot. And yeah, I don’t want someone using this to send spam from my site, either. [code]# We need to sanitize $email before using it in a message header
$replyto = preg_replace(’/[^\x20-\x7E]/’, ‘’, $email);
$to = "tom@tomhanser.com";
$subject = “Message From: Online client”
$message = “$name\n\n”;
$message .= “$message\n\n”;
$additional_headers = “MIME-Version: 1.0\n”;
$additional_headers .= “Content-type: text/html; charset=iso-8859-1\n”;
$additional_headers .= “Reply-to: $replyto\n”;

OK now that I finally got the parameters ready and look at that they are in order I need for

the call to mail() too!

mail($to, $subject, $message, $additional_headers);[/code]Sigh, if only all of “PHP developing” was as easy as reading a little and filling in the blanks :smiley:

Customer since 2000 :cool: openvein.org