12

The code below is sending an email correctly but for the body. I need to show html in the body of the message and I cannot make it. The examples in the web won't send the email :(

How can I fix my code to send the email with the html in the body?

Thanks a ton!

<?php

$to = 'mymail@mail.com';

$subject = 'I need to show html'; 

$from ='example@example.com'; 

$body = '<p style=color:red;>This text should be red</p>';

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
  $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) {

  echo("<p>Sent</p>");
 } else {
  echo("<p>Error...</p>");
 }

?>
lleoun
  • 477
  • 3
  • 6
  • 15
  • +1 for question I had similarly question but this thread answers it ( from various angles ) – Sam Mar 30 '11 at 01:55

5 Answers5

20

use this header for the mail:

 $header  = "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html; charset: utf8\r\n";

and for the content/body:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...

it's important to use inline css commands and recommanded to use tables for the interface.

...

In your Mail-Body you than have to put HTML code with head and body

helle
  • 11,183
  • 9
  • 56
  • 83
  • Thanks helle, but error in the headers and no html in the body :( – lleoun Feb 04 '11 at 11:06
  • change your line to this as well. and close all tags you have opened. and use `` tag. `$headers .= "From: " . $from . "\r\nReply-To: " . $from . "";` – helle Feb 04 '11 at 11:09
  • Sorry, I'm not getting it: $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset: utf8\r\n"; sends the email with the text in read what is great. But in the from field is shown www-data .. how can I fix that? Once it is done it will be over. Thanks a lot – lleoun Feb 04 '11 at 11:21
  • @helle you mean its better to use tables than divs for layout? – Sam Mar 30 '11 at 01:57
  • 2
    @Sam in the case of mails: yes. – helle Mar 30 '11 at 16:52
4

Have you looked at the headers of the incoming mail? It says

Reply-To: example@example.comContent-type: text/html

Simply add another \r\n here:

Reply-To: " . $from . "\r\n";
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
2

I recommend rather than messing around with doing this yourself you use one of the many free classes available all over the web to do it.

I would recommend: PHPMailer

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
James
  • 2,609
  • 3
  • 20
  • 27
1

I found this works well!

Source

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$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: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

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

--PHP-alt-<?php echo $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-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$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";
?>
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
Paul Calabro
  • 1,748
  • 1
  • 16
  • 33
-3

Simple answer: Don't do it. HTML emails are evil and annoying. At least if there's no PROPER plaintext version included. Proper = same information like in the HTML version, not just a stupid comment about getting another email client or a link to the html version if it's available on the web.

If you really need it: http://pear.php.net/package/Mail_Mime

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 3
    They can be annoying if being misused. Though, if used carefully, they are quite good. In my opinion, best practice is to send both plain/text and plain/html bodies, so user can chose which one he wants to see. – binaryLV Feb 04 '11 at 11:18