PHP Tutorials

PHP Tips

PHP General Tips

PHP General Tips

In this tutorial you will learn some general tips on PHP programming - debugging common errors in PHP, sending HTML formatted message in PHP, sending and receiving HTML mail messages.

1. Common Errors in PHP

'Warning: Cannot modify header information - headers already sent by (output started at filename line no __) '

If you run a PHP document get the above error message, it means that an error has occured in your php document. This takes place if there are any executable statements like echo before the set cookie or session variable. Try to find the statement and remove it and then run the same program.

2. Sending HTML message using PHP mail function

If you want to send HTML text message in the mailbody without any instruction in the mail function it can be done as follows using the PHP mail function.

Example:

$strFrom="baskar@smartwebby.com";

$strTo="mail@smartwebby.com";

$strSubject="Sending HTML text in php mail";

$strMailbody="mailbody<br>Welcome to <a href="www.smartwebby.com">smart webby</a><br><br><b>Best Regards</b><br><b>SmartWebby";

Mail function for HTML message format is as shown below:

Mail Function:

mail($strTo,$strSub,$strMailbody,"From:$strFrom/r/nReply-to:$strFrom);

When sending mail using the above format, you'll receive a mail in plain text as shown below.

From:baskar@smartwebby.com To:mail@smartwebby.com
Subject:Sending HTML text in php mail

mailbody<br><a href="www.smartwebby.com">Welcome to smart webby</a><br><br><b>Best Regards</b><br><b>SmartWebby

So we send the HTML text in the mailbody using the following statement

mail($strTo,$strSub,$strMailbody,"From:$strFrom\r\nReply-to: $strFrom\r\nContent-type: text/html; charset=us-ascii");

Ouput for the above format is as shown below:

From:baskar@smartwebby.com To:mail@smartwebby.com
Subject:Sending HTML text in php mail

mailbody
Welcome to smart webby

Best Regards,
SmartWebby

Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks!