PHP & MySQL application using Mail() function sends some emails fine, then starts sending blank emails

Posted on 2011-09-23
Last Modified: 2012-08-14
I am using an application written by someone else using PHP and MySQL.  Part of the application sends out emails (in large quantities) to a list of email addresses that you populate in the software.  It sends them using the PHP Mail() function.  What is happening, is that sometimes the emails hang upon sending.  Many will send, but then the application appears to hang.  It's unclear if the issue is with the application itself, or the server(s) I have it running on.  My first thought was that the emails were being blocked by the smtp server (and institutional server) that they were being sent through -- possibly being mis-identified as spam.  I contacted the smtp server folks, they pulled the logs, and could identify that many successful emails went through, then a series of empty emails were attempting to be sent.  These are completely empty -- the from, the to, the body, etc... all empty, so they never get sent, of course (and never received).  Since many, many places use this software successfully, it's unlike something is wrong in the code (though always possible).  I think something is wrong on my servers -- either with PHP, MySQL, or with memory & cpu resources, etc.  I'm at a loss as to how to troubleshoot the issue.  Does anyone have any thoughts on what might cause this behavior?  How can I narrow down the cause?  Both servers are running Windows 2003 server (IIS & PHP on one, MySQL on the other).  Any help would be greatly appreciated.
Question by:sah18
  • 3
  • 3
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
ID: 36591329
The most likely place is empty address records in the database.  The second most likely is a flaw in the code where in your situation it doesn't recognize where it should stop.  Or maybe something stops it from filling out the form.  If there was something wrong with your server or the main softwares, it would most likely show up elsewhere also.

The first thing is to keep records of what happens and when.  As you make the records more detailed, you will most likely find a pattern to the problem.  Without records and keeping track, you're just guessing.

And even though "many, many places use this software successfully", the people who installed it on your servers could easily have modified it.  PHP files are just text files.

Author Comment

ID: 36592148
Thanks for your thoughts on this, Dave.  I actually did the software install (and do the updates), and I haven't made any modifications to the code at all.

I did check the backend database, and all the email addresses are populated, and they look correctly formatted (no empties).

I actually have very specific records of what is happening (dates and times).  That is how we were able to get the institution to pull the precise smtp logs to see if they were being blocked on their end.  I don't see any clues in the log files, but I don't know that whatever this problem is is being written to a log file (or throwing an error).  I wondered about this being a timeout issue -- with possibly any of the pieces (PHP, IIS, or MySQL), but have no idea how to determine this.  I'd prefer to try to isolate what the specific problem is before starting to make guesses & changes in the php.ini and my.ini files.

LVL 109

Accepted Solution

Ray Paseur earned 300 total points
ID: 36592657
There are so many ways that email can go wrong that it can be a frustrating thing to try to get right.  As a practical matter, you might consider using a service to send the emails instead of trying to do it yourself.  I use and recommend because they always get it right and they are almost embarrassingly inexpensive.

If you want to debug this, you might try these things.

1. Add a one-second sleep() between mail() functions
2. Test the return value from the mail() function
3. Echo the contents of the mail() arguments to the browser and look for blank areas

My guess is that a rapid succession of mail() statements may be exhausting a queue somewhere, but that's just a guess.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 83

Expert Comment

by:Dave Baldwin
ID: 36593173
"changes in the php.ini and my.ini files."  I never change those files to find a problem, that just confuses the issue.

How many emails are you sending in a batch?  I take it that the SMTP server belongs to someone else?  Your ISP?

Author Comment

ID: 36594058
Thanks again to both of you.  I can't use another method to send the emails -- they are integrated into the software we are using.  I really don't think it has to do with the software or the php coding (possible, but unlikely, as others aren't having issues with the same software).

Users send the emails -- the amount can vary.  When testing, I tried sending smaller amounts and larger amounts.  Smaller amounts always send fine.  Larger amounts (say >100 at a time) may or may not go.  I tried sending 1000 in a batch, just to see how far it would get... it made it through 219, but not further.
The software pauses to send the emails in batches to prevent timeout issues, or spam blocking software from keeping them from going out -- It appears to send in groups of 20 at a time, then pauses.  It may also pause between sends, but I'm not sure about this.

The smtp server belongs to my institution -- IT dept runs it.  The aren't blocking anything on their end, but they do see my server attempting (after a while) to send completely empty emails in the logs.

Ray -- I like your suggestions & think they are something I should try.  Can you help with some simple syntax for carrying them out (reminder, I'm really not a php programmer, but know some php).
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36594106
Have you checked the website for the software?  It's next to impossible to write 'perfect' software and sometimes special conditions bring out an error that wasn't seen before.

Here's the page for sleep() and there is a link for time_nanosleep() near the bottom for smaller increments.  You could put sleep(1); at the end of the loop that sends the emails.

Author Comment

ID: 36787275
I appreciate all the suggestions.  The issue turned out to be something with the PHP version I was running.  I upgraded to the latest version, and the emails are sending out fine now.  I'll split points - thanks again for helping me think through the issues.

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Migrating files on WS2003SP2 to Azure File Share 7 46
Moving from Mcrypt to OpenSSL 18 48
php help 34 58
SQL querys that gives me from one table into another. 2 26
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question