/    Sign up×
Community /Pin to ProfileBookmark

mail() problem

I have a problem with a new script that processes incoming emails. I’m using some std. code that works in several other scripts just fine. This new script is giving me problems with the mail call though.

I have done extensive debugging to ensure that mail is the culprit. It has come down to my script issuing a call to error_log to post an entry prior to the mail() call and then one following the mail() call with the result of the call. The error log shows that the mail was sent successfully (if returns true) YET the email never arrives.

What conclusion would you make from this? That perhaps the headers were wrong? Well, I copy and pasted the headers from a script that worked just fine a minute ago and tried the problem script again and the email still does not show up. (The source for the headers did in fact send an email that I received)

So, the headers work in one place – they should work in the other, no? That means only that my post office (at my host) is rejecting the email?

Any ideas?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmauthorSep 04.2014 — Follow up.

I used the error_log function to get a step by step view of what my script is doing. It works as expected, and it definitely reports that my reading of the mail was good, my sql update of the data gleaned from it was good and then it says that the email that I sent returned a 'true' response. Yet - no email arrives.

OTOH - if my script has any syntax error I GET THE EXPECTED EMAIL along with a mail delivery failure message caused by php's echoing of the error messages which is not allowed from a script that was piped from an email (which is totally to be expected). It seems that my emails only get forced to delivery when they are being followed by another email, the bounceback message.

Does this help any geniuses out there?
Copy linkTweet thisAlerts:
@NogDogSep 04.2014 — These things can be a PITA to debug, sometimes, as the actual "problem" may not show up until after PHP is done and the local mail server has accepted the request. Then if something is failing during that actual transmission, you may have to find/analyze the mail server's error log. Then, if that's not the issue, you could run into problems on the receiving side, typically due to spam-blocking stuff (which could be triggered by any combination of content, header, and source domain/IP criteria).

As far as local mail server issues, I've had better success using PHPMailer, using its built-in SMTP class, which seems to make more debug info available than PHP's built-in mail() function does, in case you want to try that line of approach.

PS: The most common issue I see with mail() is that many hosts require that the "From:" header be an valid email address [I]on that host[/I] -- though I [i]think[/i] that usually results in a detectable error from the mail() call, though I may be misrembering. Therefore if you are putting a dynamic value into the From header, you may need to instead hard code a valid email address then put that dynamic value into a Reply-To header.
Copy linkTweet thisAlerts:
@NogDogSep 04.2014 — PPS: My reply was being written before your reply, so from that, I'm guessing the From/Reply-To header issues isn't the, uh, issue. ?
Copy linkTweet thisAlerts:
@ginerjmauthorSep 04.2014 — The thing is that this same setup is being used by 3-4 other piped scripts of mine. Same email box for the email to be sent to; same headers; the only difference is the from address but I have double-checked that I didn't introduce an error in header (it's included via a php var anyway).

To add the fire: I have found that when my script runs perfectly fine I do not get the resulting email from the script. BUT if I introduce a syntax error in the script the email gets delivered, followed immediately by a 'deliver failure' message. Fix the error and the email never shows up.
Copy linkTweet thisAlerts:
@NogDogSep 04.2014 — Is this being run via the CLI (e.g. a cron job, or shell script, etc.)? If so, it might be interesting to check the processes it generates, and see if there's something weird going on, such as the process not going away when the script runs "normally" (and doesn't send the email). I'm wondering if something odd is going on where the call to mail() is not really completing, and hangs around in a zombie process, whereas when something goes "wrong", the process is terminated and forces the call to the system mail function to be terminated as well?

Yeah, I'm grasping at straws here, but can't really think of anything else. ?
Copy linkTweet thisAlerts:
@ginerjmauthorSep 04.2014 — You may very well be right, but I have no answers for you. This is a hosted appl and it is a script that is triggered from an email being piped to it. Your scenario could explain the whole thing but it is beyond me to debug. And of course my host says it's got to be a problem with my script.

The script does a read of the incoming email from stdin. It looks for certain text items and save them. Once the file is done and closed (yes) I then check my array of values to be sure they are all found and, if so, I continue to do a MySQL update and then send an email which apparently goes into thin air now. Again - this script is from a model that I wrote and debugged last year of so and still works perfectly for some other processes that I still have in place. I have re-written this script twice to try and solve it but to no avail.
Copy linkTweet thisAlerts:
@NogDogSep 05.2014 — Hmm...wish I had a revelation to give you. If I were in your shoes -- pending someone wise in the ways of mail chiming in -- I'd probably go with my previous idea of using PHPMailer with its SMTP option, just to see if bypassing PHP's mail() function solves it. I hate work-arounds instead of fixes, but sometimes you just have to bite the bullet. ?
Copy linkTweet thisAlerts:
@ginerjmauthorSep 05.2014 — For Those Interested:

I have managed to close every loophole I could think of in my script to try and get mail() to work consistently. I should say that mail() has yet to report a failure, but that is irrelevant.

I have taken my exact 'send' code and put it into a script all by itself and have tested that little script repeatedly. I get an email every time.

I have added calls to error_log() wherever significant activites take place in my script, such as db connection, db queries, db fetch loops and final results produced. Basically an email is processed to grab some small pieces of data and then post them to my db. These log messages indicate my expected results every time as well as a 'successful' call to mail(). Still no emails arrive consistently. But when they do I am amazed. Today after perhaps 20 tests and 5-6 actual email submissions, I have received 2 emails back. I cannot figure out why my debugging process indicates such consistent activity and yet the email is so inconsistent.

To be sure I have used my host's webmail access to check my mailboxes and the messages that were sent by php are not there either (just in case my T'bird client was having a problem).

In case there is some question about my process it is this:

1 people send me an email with some input data

2 I see it come in and forward it to my special email address that is piping to this script after adding to the message to identify the input I need

3 the script sees the mail, reads it, finds the inputs that I added to the message

4 posts the input to my db

5 sends an email back to my inbox

all while writing info to the error log about what is transpiring (about 6-7 log msgs).

Pretty simple straight forward process one would think. I even added closecursor (pdo) calls after my 3 different queries in the script (all small query results btw). That didn't help.

If anyone thinks of some other 'cleanup' I should do, feel free.
×

Success!

Help @ginerjm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...