Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail html format not working

Tags:

php

email

Using php, and with this code I receive the email as a plain text, did I miss something? as I need to send formatted email which could contain links for example.

$to = "[email protected]";
$subject = "Password Recovery";

$body = '
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Birthday Reminders for August</title>
    </head>
    <body>
        <p>Here are the birthdays upcoming in August!</p>
        <table>
            <tr>
                <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
            </tr>
            <tr>
                <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
            </tr>
            <tr>
                <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
            </tr>
        </table>
    </body>
</html>
';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: [email protected]\r\n"."X-Mailer: php";
if (mail($to, $subject, $body, $headers)) 
echo "Password recovery instructions been sent to your email<br>";
like image 500
user1483799 Avatar asked Aug 16 '12 19:08

user1483799


People also ask

What is PHP mail () function?

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

How can I put a HTML link inside an email body in PHP?

You need to specify a Content Type of HTML in your function. // 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' .

What is correct syntax for sending email in PHP?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );

Why is my mail PHP not working?

check with your host, many have disabled the mail() function for anti-spam purposes you might need to use smtp instead. The script looks ok. Also the sucess message suggest is a configuration problem... check your configuration... also check the configuration on the receiving server.


3 Answers

You've re-set your headers:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: [email protected]\r\n"."X-Mailer: php";

You're missing a dot in that last line, which is over-writing the previous two:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: [email protected]\r\n"."X-Mailer: php";
like image 158
andrewsi Avatar answered Oct 14 '22 23:10

andrewsi


Look at this example, this is sufficient to send mail in php:

<?php 
    //change this to your email. 
    $to = "[email protected]";
    $from = "[email protected]";
    $subject = "Hello! This is HTML email";

    //begin of HTML message 
    $message ="
<html> 
  <body> 
    <p style=\"text-align:center;height:100px;background-color:#abc;border:1px solid #456;border-radius:3px;padding:10px;\">
        <b>I am receiving HTML email</b>
        <br/><br/><br/><a style=\"text-decoration:none;color:#246;\" href=\"www.example.com\">example</a>
    </p>
    <br/><br/>Now you Can send HTML Email
  </body>
</html>";
   //end of message 
    $headers  = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n";

    //options to send to cc+bcc 
    //$headers .= "Cc: [email][email protected][/email]"; 
    //$headers .= "Bcc: [email][email protected][/email]"; 

    // now lets send the email. 
    mail($to, $subject, $message, $headers); 

    echo "Message has been sent....!"; 
?>
like image 21
Patt Mehta Avatar answered Oct 15 '22 00:10

Patt Mehta


I found the same problem with a specific mail server, in my case the solution was to set "\n" instead of "\r\n" as the end of line for the headers.

like image 22
MazarD Avatar answered Oct 15 '22 00:10

MazarD