Wednesday, September 5, 2012

A tryst with mail in PHP and the newbie

The problem with newbies is that they haven't the experience. Okay, sounds a but lame but the thing is they haven't seen as much crap as an experienced guy and for my 2 and half years I've seen some interesting crap.

Anyway, to the story the task was to send a mail from PHP. The guy was a newbie. The problem was the guy wasn't familiar with PHP. He didn't track the changes and effects that change brought. There is a way to solve any problem. If you know the root cause of any problem you hit that and then you solve it. But when you don't know the exact cause you got to research on the internet. And usually you end up with a number of things that comprise of a potential solution. And in these situations you have to keep track of every change you are doing to the system. Because unless you track everything and didn't check if every small change then you end up missing the solution.
I'm watching a lot of Dr House these days so I'll compare it to a diagnosis. You can't just use broad spectrum antibiotics for everything because they work on only a handful of cases.
So back to the problem, I let him wrestle with the problem for a week and by that time he ended up with a mess that would scare anybody.
First you find the cracks in the reality - I.e. The girl in red dress. Here it means you need to discover potential areas causing problems. They can be configs, switches to various programs involved, environment variables... In short anything. But you have to try and discover as much as possible. And once you have done that you have to make sure that they all are healthy. The configs are right and the logic super sane. You are doing what's possible to do and not trying to fly superman.
The guy had messed up so bad. He had modified php's mail settings. And sendmail settings. And apache settings. And god knows what else.
I was thinking all possible areas where the problem might be. So this evening I went by his cubicle and told him that we are either going to solve it now or we are going to leave it alone till deployment.
First thing I checked was php's ini file. The SMTP mail server settings are present there but they are needed on windows only. So I disabled them. I also disabled the SMTP port. Then I checked if send.ail was available for PHP. Since it was I left that setting alone otherwise we can specify sendmail binary path.
So here I ensured that PHP's side was okay. Next I checked sendmail settings. I used google with a targeted query to find resources which would explain send mail options and configurations. There I discovered the second problem we had. The send.ail settings sere a total mess. So I cleaned them up. This involved setting proper user name, proper hostname and disabling tls since the mail server connection is unencrypted. I can understand tls thing, but newbie guy messed up hostname and username. That's real shame.
Then I went through the mail sender PHP page's code. There also I found couple of problems, one was the variable names were all messed up. I cleared it up and one the web pages said about setting up an init parameter.
After doing this all I used php to send a mail and voila! The mail was sent.
Lesson: Being ignorant or inexperienced is not something you should be ashamed of but being stupid... God save you!

Sent from my Windows Phone

No comments:

Post a Comment