How to send a html by php mail function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanjay123456
    New Member
    • Sep 2006
    • 125

    How to send a html by php mail function

    Dear friends,

    [PHP]

    <?php
    //define the receiver of the email
    $to = 'sanjay.mit@gma il.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@examp le.com\r\nReply-To: webmaster@examp le.com";
    //add boundary string and mime type specification
    $headers .= "\r\nConten t-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash. "\"";
    //define the body of the message.
    ob_start(); //Turn on output buffering
    ?><?php echo $random_hash; ?>


    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <h2>Hello World!</h2>
    <p>This is something with <b>HTML</b> formatting.</p>
    </body>


    <?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";
    echo "sending a mail in html in php";
    ?>
    [/PHP]



    I would like to following output

    Hello World!
    This is something with HTML formatting.


    but its give the following output
    [HTML]
    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    </head>

    <body>

    <h2>Hello World!</h2>

    <p>This is something with <b>HTML</b> formatting.</p>

    </body>
    [/HTML]

    so anyone tell where i am doing wrong in this code ??????

    thanks in advance !!


    sanjay
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You need to send this with your headers

    [php]
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    [/php]

    Comment

    • sanjay123456
      New Member
      • Sep 2006
      • 125

      #3
      Dear guru ,


      Its also not working here .

      so what can i do for this problem ????

      Comment

      • stepterr
        New Member
        • Nov 2007
        • 157

        #4
        Originally posted by sanjay123456
        Dear guru ,


        Its also not working here .

        so what can i do for this problem ????

        Take a look at this thread. I posted an example of a very basic way to send html within an email using the PHP mail function. Maybe that will help you as well

        Comment

        Working...