Jump to content

[SOLVED] Mail() continues to send html code


Kane250

Recommended Posts

Anyone familiar with mail() ?

 

I'm using it to build an email app. I'm using an ajax text editor that allows for test editing (color, bold, fnt, etc.) and it sends it all as html to mail(). However the e-mails that I receive all come with the actual html code written inside it. I added all the headers that it looked like I needed, but yet it still refuses. Anyone?

 

Oh also, if anyone has information about changing the envelope from name, I'm having a tough time there too. Keeps coming in as [email protected]...

 

Thanks!

 

<?php

$to      = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['msgpost'];

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

print "<pre>";

if ($_POST) {
mail($to, $subject, $message, $headers);
}

print "</pre>";
?>

Link to comment
https://forums.phpfreaks.com/topic/104308-solved-mail-continues-to-send-html-code/
Share on other sites

Hmm I wish I had a direct answer for you, however I do have a far fetched idea. When I was coding the mail function I had this...

	$to = $_POST["to"];
$from = $_POST["from"] . "\r\n";
$subject = $_POST["subject"];
$message = $_POST["message"];

$headers = "From: $from";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to,$subject,$message,$headers);

 

The only difference in our two scripts is I create the mime, and content type last, you pile stuff on after it. Try calling that last and see what you get, just a thought.

If the from name is a variable, it should be enclosed in double quotes or concatenated when used on the $headers variable. Place also an X-Mailer: PHP/" . phpversion() in the headers so clients identify it as an automated email sent from a php script.

 

As for the html issue, im throwing something nosense here, use htmlentities() on $_POST['message']

Thanks. I found a fix by changing the code to this:

 

<?php

$to      = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['msgpost'];
$from = '[email protected]';


print "<pre>";
if ($_POST) {
mail($to, $subject, $message, "From: $from\nContent-Type: text/html; charset=iso-8859-1");
}
print "</pre>";
?>

 

It seems to work, although not all the html formatting is coming through to the emails. Color, underline, etc. doesn't work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.