简体   繁体   中英

how to send an HTML content via php mail()

I am trying to send an HTML e-mail using this code but all i am getting is FALSE from the mail() function.

The error_log is empty.

Can someone tell me why mail() is not working?

$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>SDFSDF</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>VXCVSDF</td></tr>";
$message .= "</table>";
$message .= "</body></html>";

$to = 'my_mail@gmail.com';

$subject = 'Website Change Reqest';

$headers = "From: USER NAME"."\r\n";
$headers .= "Reply-To: USER EMAIL"."\r\n";
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";

if (mail($to, $subject, $message, $headers)) {
    echo 'Your message has been sent.';
} else {
    echo 'There was a problem sending the email.';
}

It's hard to debug PHP mail() function.

After checking your script, I can confirm that your code is working fine. It's something with your server or/and PHP configuration.

Start with this little snippet to see what is happening:

error_reporting(E_ALL);
ini_set('display_errors', -1);
echo '<br>I am : ' . `whoami`.'<br>';
$result = mail('myaddress@mydomain.com','This is the test','This is a test.');
echo '<hr>Result was: ' . ( $result === FALSE ? 'FALSE' : 'TRUE') . ' ('. $result. ')';
echo '<hr>';
echo phpinfo();

After output, check your sendmail_path , in most case sendmail_path uses sendmail MTA:

/usr/sbin/sendmail -t -i

Edit your php.ini file, set the following and don't forget to restart httpd server:

sendmail_path = /usr/sbin/sendmail -t -i

Check log files at /var/log/maillog , it could really help you to solve the problem.

If you still have a problem, just take a good look at PHPMailer , SwiftMailer , PEAR's Mail or Zend Framework's Zend_Mail an excellent, comprehensive, modern PHP mailing library. It will be easy to debug your problem after all.

You can using phpmailer for this way!

Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM