Saturday, May 31, 2008

phpMailer and other coding woes.

Okay so this one is going to be a little more esoteric, but it might help someone out there. I spent a lot of time debugging a program I had been handed that had fewer comments than I might have liked. Specifically it was an application written in Flex that used php to do some things like generate reports and send off notification e-mails.

A couple of weeks back this application stopped sending out notification e-mails and I had no idea what was going on. The problem was compounded by the fact that any errors the PHP threw were being hidden by the Flex front end. So this first part of this is going to be a little bit of basic php that I employed to dump info to a text file.

Here is an quick example of file writing in php:


//Define the file
$fn = (dirname(__FILE__) . '/new.log');
//Open log file for writting
$fp = fopen($fn,'a');
//Write to the file
fwrite($fp,"Some text \n");
//Close the file
fclose($fp);

The first line of actual code is there just to assign the filename to a variable to make it easier to deal with. It should be noted that (dirname(__FILE__) is a good little tool to indicate the current directory. It is much better then full paths because when you move your the guts of the app around you don't have to adjust file names all throughout your program. Also remember that if you are doing this in a windows environment you will need to escape your back slashes in your file path. So C:\Windows\System32\LogFiles would become C:\\Windows\\System32\\Logfiles. Or as odd as it might look to longtime Microsofties you can use C:/Windows/System32/Logfiles (hey it surprised the heck out of me too!!).

The next line we use the fopen command to open the file ($fn) in 'a' or append mode and assign it to the $fp stream. You can look at the php docs for information about the fopen command.

Next we are going to write to the file. By writing "Some text \n" we are writing the phrase "some text" and then a new line to the file.

Finally we close the file.

Now one of the really important things to remember is if your text file that you are witting to is not available or the permissions are set wrong php will throw and error. So remember to do a chmod 777 on the file in Linux or to set the appropriate user rights in Windows.

Once I got the error messages into the text file I found this cryptic error message:
Language string failed to load: recipients_failed
So not to be deterred I decided I would dump all the e-mail addresses to the text file to see what it was complaining about. And there it was. After spending nearly two day exploring issues with DNS phpMail and all those other things it turned out it was.................a malformed email address. All I had to do was correct the address in AD (where the addresses were being pulled from) and life was once again good.

What I want you to take away from this is that if you are getting a Language string failed to load message in phpMailer check you message settings, TO, FROM, CC, BCC, Host, Mailer. Your problem is most likely with one of those items.

Hope this helps!!




No comments: