Duplicate emails being sent

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Duplicate emails being sent

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Duplicate emails being sent

Post by Eran »

This script is ran through a cron? Is it possible the cron job is misconfigured?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Duplicate emails being sent

Post by pickle »

It will be run through a cron job eventually, but right now I'm just testing it through the browser.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Duplicate emails being sent

Post 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>");
?>
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Duplicate emails being sent

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Duplicate emails being sent

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Duplicate emails being sent

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Duplicate emails being sent

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Duplicate emails being sent

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Duplicate emails being sent

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Duplicate emails being sent

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Duplicate emails being sent

Post by Eran »

Exim (or whatever mail server you use) is probably misconfigured.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Duplicate emails being sent

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Duplicate emails being sent

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Duplicate emails being sent

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply