Thursday, September 24, 2009

Advanced PHP mail problem in Ubuntu

After a MTA like sendmail has been setup in Linux environment, PHP script for sending email seems not to work correctly. The symptom is so weird. Say, you sent out email successfully without any error message by using PHP. However, the email actually didn't reach the destination mailbox. The famous PHP manual did not show any light on what happened.

Assuming we are using SMTP server other than the sendmail agent to send out the email, sometimes we need to pass authentication details to make it work. However, PHP built-in mail() function is lack of authentication feature. This function works really well on Windows machine, supposing it is using SMTP server in local network.

For Linux, it is recommended to use PEAR project's Mail object as it has extra feature for passing authentication parameters to SMTP server. This is especially useful when the hosting machine is not in the local network of SMTP server. In this case, authentication is a must to send through emails successfully.

For the beginning of the PHP script, we'd better add this:



Then you may pupulate Mail::factory() object to send out the email content.

PHP4 or newer version has already integrated PEAR's Mail library in its package. If they don't, then you may add it yourself:



The beta 1.2.0b2 version of Mail works okay so I guess you may like it. Of course, you may install newer version as you like.

Then you may use Mail::factory() object for sending email using PHP script, surely with your SMTP username and password. This Mail object works even if you don't use authentication.

Nice to have a try;)

No comments:

Post a Comment