Page 1 of 2

Duplicate emails being sent

Posted: Tue Nov 18, 2008 4:53 pm
by pickle
Hi all,

I've got a fairly straightforward script that sends an email if an error was encountered. The weird thing is, I'm getting 2 emails sent, though I'm pretty sure I'm calling mail() only once.

Here's the script:

Code: Select all

<?PHP
session_start();
require('config.inc');
 
require('class.import.php');
require('class.class.php');
require('class.user.php');
 
$Import = new Import();
 
#TRUE means load from stored file rather than look for an uploaded file
if(!$Import->loadFile(TRUE))
    $message = 'Unable to load classes.txt';
else if(!$Import->commit())
    $message = 'Unable to commit data';
    
$message = 'This is a test message';
 
if($message)
{
    $full_message = <<<MESSAGE
The #### encountered an error while automatically synchronizing itself with ###.
 
The message was: $message
 
The error generated was: $Import->error
MESSAGE;
 
    mail('###@###.ca', '###### cron job error',$full_message,"From: ###### ### ###Cron <noreply@####.###.ca>");
    echo "error";
}
?>
error only gets output once, but I get 2 emails in my inbox. Any ideas? It's entirely possible it's the system that's misconfigured, but the sysadmin is the kind of guy who, if he was convinced he didn't feel pain, a punch to the face wouldn't change his mind. Needless to say, I want to be darn sure it's not me.

Thanks.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 4:56 pm
by Eran
This script is ran through a cron? Is it possible the cron job is misconfigured?

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 4:59 pm
by pickle
It will be run through a cron job eventually, but right now I'm just testing it through the browser.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:04 pm
by VladSun
What does happen if you just call the mail() function?

Code: Select all

<?PHP
    mail('###@###.ca', '###### cron job error','probe message',"From: ###### ### ###Cron <noreply@####.###.ca>");
?>

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:21 pm
by pickle
This sends 4 emails:

Code: Select all

<?PHP
    $full_message = 'test';
    mail('###','### cron job error',$full_message,"From: #### Cron <noreply@####.ca>");
    mail('###','Test','This is a test body');
?>
Ultimately, these emails will only be sent when something goes horribly wrong, which hopefully should be never, but I would like to figure out what's up.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:23 pm
by VladSun
If you put in this PHP file an additional line like this:

Code: Select all

error_log("executed"); 
how many lines do you get in you error log files?

PS: I'm looking for a strange redirect ...
PPS: What does happen if you run mail() in PHP CLI?

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:33 pm
by alex.barylski
Try using Swift and see if that solves the problem. Use an external SMTP like GMail for testing...

In theory it could be a lot of things

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:36 pm
by VladSun
LOL, I think I've got it ;)
Remove the echo line ;)

In fact, with crontab you don't need the mail(), because the output of any command executed by crontab will be emailed.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:43 pm
by aceconcepts
I'm taking the simplistic route here...maybe your script is just fine and maybe the problem lies elsewhere i.e. perhaps this script is called multiple times!!

Just a guess - i often find i overlook the simpler possibilities :D

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:45 pm
by pickle
The error log has "executed" twice.

When I execute the file manually through the CLI

Code: Select all

php -f cron.php
I get the same behaviour

I could use Swift, but that's an awful lot of work for just a simple "hey, this script is buggered" email.

Removing the echo didn't do anything. I tacked on the microtime() to the end of the message & it appears the emails are being generated up to 2 seconds apart.

I thought it might be called twice or in a funky loop or something. But the output only appears once.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:50 pm
by VladSun
The error log has "executed" twice.
So, it's not mail() function fault... Weird, isn't it ;)

So, you are sure that a PHP file with a single line like this:

Code: Select all

error_log('executed');
will put two lines in the error log? Positive?

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:51 pm
by Eran
Exim (or whatever mail server you use) is probably misconfigured.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:52 pm
by VladSun
pytrin wrote:Exim (or whatever mail server you use) is probably misconfigured.
If it so, why there are 2 lines in the error log per a single execution?

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 5:54 pm
by pickle
VladSun wrote:So, you are sure that a PHP file with a single line like this:

Code: Select all

error_log('executed');
will put two lines in the error log? Positive?
Positive.

If I make a PHP file with the a single line:

Code: Select all

echo 'test';
'test' only appears on the screen once.

Re: Duplicate emails being sent

Posted: Tue Nov 18, 2008 6:15 pm
by VladSun
pickle wrote:
VladSun wrote:So, you are sure that a PHP file with a single line like this:

Code: Select all

error_log('executed');
will put two lines in the error log? Positive?
Positive.
8O
pickle wrote:If I make a PHP file with the a single line:

Code: Select all

echo 'test';
'test' only appears on the screen once.
That's for PHP CLI, right?

PHP version?