1

I have written this PHP script to send a HTML email. However the email output is appearing as raw text and not being interpreted as html.

<?php 
 if(isset($_POST['submit']))  {
    $to = $_POST['email'];
    $from = "[email protected]";
    $subject = "subject";
    $message = "<div style=\"background:red;height:100px; " .
      "width:100px;display:block;\">dfsdf</div>";

    $headers = "MIME-Version: 1.0rn";
    $headers .= "Content-type: text/html; charset=iso-8859-1rn";
    $headers  .= "From: $from\r\n";
    mail($to, $subject, $message, $headers);
    echo "Message has been sent....!";  
 }else{
    echo "Add an email address"; 
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="/send" method="post">
  <input name="email" type="text" />
  <input name="submit" type="submit" value="Submit">
</form>
</body>
</html>

The email I get is:

<div style="background:red;height:100px;width:100px;display:block;">dfsdf</div>

I expect to be getting a red box. What am I doing wrong?

1
  • The way the e-mail appears is entirely up to your e-mail reader. It should have nothing to do with this code. Commented Dec 13, 2012 at 12:26

4 Answers 4

2

Try changing the email headers: content-type and charset like this:

$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";

to

$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
Sign up to request clarification or add additional context in comments.

Comments

1
    $mail_to=$tomail;

    $mail_from=$frommail;

    $mail_sub="Subject";

    $mail_mesg="Body of your Message";

    $headers = "MIME-Version: 1.0 \r\n";

    $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";

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

    if(mail($mail_to,$mail_sub,$mail_mesg,$headers))
        $result = "send successfully";
    else
        $result = "failed to send mail";

Comments

0

Here's what works for me:

$mailheaders = "From: $email\n";
$mailheaders .= "Reply-to: $email\n";
$mailheaders .= "Content-Type: text/html; charset=iso-8859-1\n";

mail('[email protected]', 'Test', 'Foo bar', $mailheaders);

Comments

0

You have to use $headers .= "Content-type: text/html; charset=iso-8859-1". "\r\n"; and $headers = "MIME-Version: 1.0\r\n"; instead of

$headers .= "Content-type: text/html; charset=iso-8859-1rn"; $headers = "MIME-Version: 1.0rn";

So try this

 <?php 
     if(isset($_POST['submit']))  {
        $to = $_POST['email'];
        $from = "[email protected]";
        $subject = "subject";
        $message = "<div style=\"background:red;height:100px;width:100px;display:block;\">dfsdf</div>";
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1". "\r\n";
        $headers  .= "From: $from\r\n";
        mail($to, $subject, $message, $headers);
        echo "Message has been sent....!";  
     }else{
        echo "Add an email address"; 
     }?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="/send" method="post">
      <input name="email" type="text" />
      <input name="submit" type="submit" value="Submit">
    </form>
    </body>
    </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.