Jump to content

[SOLVED] HTML mail() problem


sKunKbad

Recommended Posts

I've looked at a few tutorials on sending HTML email. All the tags show up in the email, and none of the HTML formatting is being applied.

 

$email = "[email protected]";
$content = "<p style='color:blue'This text should be blue</p>";
$header = "From: My Store <[email protected]>\r\n";
$header .= "Reply-To: [email protected]\r\n";
$header .= "Return-Path: [email protected]\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail ("$email","Look at my HTML email","$content","$header");

 

What am I doing wrong here?

Link to comment
https://forums.phpfreaks.com/topic/78654-solved-html-mail-problem/
Share on other sites

Are you getting anything at all displayed?

 

If not it could be that you haven't closed you p tag correctly:

$content = "<p style='color:blue'This text should be blue</p>";

should be:

$content = "<p style=\"color:blue\">This text should be blue</p>";

Try:

 

$email = "[email protected]";
$content = "<html><head></head><body><p style='color:blue'>This text should be blue</p></body></html>";
$header = "From: My Store <[email protected]>\r\n";
$header .= "Reply-To: [email protected]\r\n";
$header .= "Return-Path: [email protected]\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail ("$email","Look at my HTML email","$content","$header");

 

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

You can also space it like so:

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

 

$email = "[email protected]";

$content = "
<html>
<head></head>
<body>
  <p style='color:blue'>This text should be blue</p>
</body>
</html>
";

$header = "From: My Store <[email protected]>\r\n";
$header .= "Reply-To: [email protected]\r\n";
$header .= "Return-Path: [email protected]\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail ("$email","Look at my HTML email","$content","$header");

This is what I am currently using, and it works:

 

<?php
$to = '[email protected]';
$subject = 'Test HTML email';
//create a boundary string. It must be unique - so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: Brian Evaslim <[email protected]>\r\nReply-To: [email protected]\r\nReturn-Path: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
$message = "
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-$random_hash
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-$random_hash--
";
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

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.