Solved

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

Posted on 2011-09-23
7
239 Views
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.
0
Comment
Question by:sah18
  • 3
  • 3
7 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
Comment Utility
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.
0
 
LVL 9

Author Comment

by:sah18
Comment Utility
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.

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 300 total points
Comment Utility
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 ConstantContact.com 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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
"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?
0
 
LVL 9

Author Comment

by:sah18
Comment Utility
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).
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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() http://us3.php.net/manual/en/function.sleep.php 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.
0
 
LVL 9

Author Comment

by:sah18
Comment Utility
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now