Getting multiple emails from a single mail()...
Getting multiple emails from a single mail()...
am 01.08.2007 23:49:23 von crazycooter
I found an old thread on this (http://groups.google.com/group/alt.php/
browse_thread/thread/751edb9c723316c4/ea9bf92a9c6b807c?lnk=g st&q=mail()
+duplicate&rnum=7#ea9bf92a9c6b807c), but there didnt seem to be a
resolution. I also noticed that this was a pretty old thread and there
didn't seem to be anything newer in the search results.
Regardless, I have a very similar problem; I am consistently getting
five emails per each PHP mail() call. While this originally showed up
in a loop generating a flood of email, I explicitly made a single
direct mail call using hard coded strings and I confirmed the same
behavior. I have spent most of today fighting with this and I can
confidently rule out that the mail() function is being accidentally
called multiple times (unless PHP is reloading the page multiple times
before finally rendering back or something else really bizarre)
Here is an example:
$result = mail($sendto, $subject, $messagebody, $headers);
If it matters, this is being called via a wrapper function, like this:
function send_email($email_from, $email_to, $subject, $msg,
$show_result = null)
{
...
$headers = "From: $from \r\n";
$headers .= "Reply-To: $from \r\n";
...
$result = mail($sendto, $subject, $messagebody, $headers);
...
}
The values of the vars don't matter, as I ran a very simple mail with
hard coded strings and got exactly the same behavior.
I have searched extensively and I cant seem to find anything that
specifically nails down why this might happen. For those interested in
the mail headers generated by a single mail() call, here is what I get
(some data scrubbed out):
########################
Received: from crazycooter [67.110.67.194] by XXXXX.marketnet.com
(SMTPD-9.10) id A208038C; Wed, 01 Aug 2007 12:25:28 -0500
Date: Wed, 01 Aug 2007 12:25:17 -0500
Subject: Subject
To: YYYYY.ZZZZZ@MarketNet.com
From: AAAAA.BBBBB@MarketNet.com
Reply-To: AAAAA.BBBBB@MarketNet.com
Message-Id: <200708011225863.SM03756@crazycooter>
X-RCPT-TO:
Status:
X-UIDL: 471469545
X-IMail-ThreadID: c208071f0000b881
########################
Received: from crazycooter [67.110.67.194] by XXXXX.marketnet.com
(SMTPD-9.10) id A2020370; Wed, 01 Aug 2007 12:25:22 -0500
Date: Wed, 01 Aug 2007 12:25:11 -0500
Subject: Subject
To: YYYYY.ZZZZZ@MarketNet.com
From: AAAAA.BBBBB@MarketNet.com
Reply-To: AAAAA.BBBBB@MarketNet.com
Message-Id: <200708011225800.SM02212@crazycooter>
X-RCPT-TO:
Status:
X-UIDL: 471469541
X-IMail-ThreadID: c20100e10000b872
########################
Received: from crazycooter [67.110.67.194] by XXXXX.marketnet.com
(SMTPD-9.10) id A20304C4; Wed, 01 Aug 2007 12:25:23 -0500
Date: Wed, 01 Aug 2007 12:25:13 -0500
Subject: Subject
To: YYYYY.ZZZZZ@MarketNet.com
From: AAAAA.BBBBB@MarketNet.com
Reply-To: AAAAA.BBBBB@MarketNet.com
Message-Id: <200708011225488.SM03132@crazycooter>
X-RCPT-TO:
Status:
X-UIDL: 471469542
X-IMail-ThreadID: c20301a00000b875
########################
Received: from crazycooter [67.110.67.194] by XXXXX.marketnet.com
(SMTPD-9.10) id A20503A8; Wed, 01 Aug 2007 12:25:25 -0500
Date: Wed, 01 Aug 2007 12:25:15 -0500
Subject: Subject
To: YYYYY.ZZZZZ@MarketNet.com
From: AAAAA.BBBBB@MarketNet.com
Reply-To: AAAAA.BBBBB@MarketNet.com
Message-Id: <200708011225363.SM03132@crazycooter>
X-RCPT-TO:
Status:
X-UIDL: 471469543
X-IMail-ThreadID: c20507190000b87a
########################
Received: from crazycooter [67.110.67.194] by XXXXX.marketnet.com
(SMTPD-9.10) id A20703A8; Wed, 01 Aug 2007 12:25:27 -0500
Date: Wed, 01 Aug 2007 12:25:16 -0500
Subject: Subject
To: YYYYY.ZZZZZ@MarketNet.com
From: AAAAA.BBBBB@MarketNet.com
Reply-To: AAAAA.BBBBB@MarketNet.com
Message-Id: <200708011225706.SM03132@crazycooter>
X-RCPT-TO:
Status:
X-UIDL: 471469544
X-IMail-ThreadID: c20600e10000b87d
########################
I am developing locally right now on a WinXP box (SP2) with PHP 5 via
IIS. If it matters, I had to configure IIS such that .html extensions
are mapped to "C:\Progra~1\PHP\php5isapi.dll" for GET,POST, and HEAD
since this clients site is all .html files with embedded PHP. The SMTP
is an exchange server.
I didn't develop this app, but I do need to support it, so any
guidance from someone more knowledgeable would be greatly appreciated.
Regards,
CrazyCooter
Re: Getting multiple emails from a single mail()...
am 02.08.2007 00:03:18 von ivansanchez-alg
crazycooter@gmail.com wrote:
> [...] The SMTP is an exchange server.
Can you telnet to the SMTP server and send a raw email by hand? It mat help
you to know if the error is whithin that exchange server.
(Have a look at Wikipedia for hints on the SMTP commands. They go something
like "HELO", "RCPT TO", and the message body followed by a line with a
single dot, IIRC.)
Hope it helps,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Re: Getting multiple emails from a single mail()...
am 02.08.2007 00:19:04 von crazycooter
I followed through the steps (pretty cool hehe) and I got a single
email as a result.
CrazyCooter
Re: Getting multiple emails from a single mail()...
am 02.08.2007 05:18:54 von Jerry Stuckle
crazycooter@gmail.com wrote:
> I followed through the steps (pretty cool hehe) and I got a single
> email as a result.
>
> CrazyCooter
>
It really looks like you're calling mail() five times. Try sometning
like this:
function send_email($email_from, $email_to, $subject, $msg,
$show_result = null)
{
static count = 1;
...
$headers = "From: $from \r\n";
$headers .= "Reply-To: $from \r\n";
...
$messagebody = "Count: " . $count++ . "\n\n" . $messagebody;
$result = mail($sendto, $subject, $messagebody, $headers);
...
}
This will give an incrementing count at the start of the message body,
starting at 1.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Getting multiple emails from a single mail()...
am 02.08.2007 05:59:15 von luiheidsgoeroe
On Thu, 02 Aug 2007 05:18:54 +0200, Jerry Stuckle =
wrote:
> crazycooter@gmail.com wrote:
>> I followed through the steps (pretty cool hehe) and I got a single
>> email as a result.
>> CrazyCooter
>>
>
> It really looks like you're calling mail() five times. Try sometning =
=
> like this:
>
> function send_email($email_from, $email_to, $subject, $msg,
> $show_result =3D null)
> {
> static count =3D 1;
$count
> ...
> $headers =3D "From: $from \r\n";
> $headers .=3D "Reply-To: $from \r\n";
> ...
> $messagebody =3D "Count: " . $count++ . "\n\n" . $messagebody;
> $result =3D mail($sendto, $subject, $messagebody, $headers);
> ...
> }
>
> This will give an incrementing count at the start of the message body,=
=
> starting at 1.
Indeed. Possibly use debug_backtrace() to check where it happens, but mo=
st =
likely it's an undesired loop and linenumbers will be the same.
-- =
Rik Wasmus
Re: Getting multiple emails from a single mail()...
am 02.08.2007 06:01:45 von Jerry Stuckle
Rik wrote:
> On Thu, 02 Aug 2007 05:18:54 +0200, Jerry Stuckle
> wrote:
>
>> crazycooter@gmail.com wrote:
>>> I followed through the steps (pretty cool hehe) and I got a single
>>> email as a result.
>>> CrazyCooter
>>>
>>
>> It really looks like you're calling mail() five times. Try sometning
>> like this:
>>
>> function send_email($email_from, $email_to, $subject, $msg,
>> $show_result = null)
>> {
>> static count = 1;
> $count
>
>> ...
>> $headers = "From: $from \r\n";
>> $headers .= "Reply-To: $from \r\n";
>> ...
>> $messagebody = "Count: " . $count++ . "\n\n" . $messagebody;
>> $result = mail($sendto, $subject, $messagebody, $headers);
>> ...
>> }
>>
>> This will give an incrementing count at the start of the message body,
>> starting at 1.
>
> Indeed. Possibly use debug_backtrace() to check where it happens, but
> most likely it's an undesired loop and linenumbers will be the same.
> --Rik Wasmus
Thanks, Rik. I should know not to try to post code at midnight... :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Getting multiple emails from a single mail()...
am 02.08.2007 14:02:00 von colin.mckinnon
On 1 Aug, 22:49, crazycoo...@gmail.com wrote:
>
> Regardless, I have a very similar problem; I am consistently getting
> five emails per each PHP mail() call. While this originally showed up
> in a loop generating a flood of email, I explicitly made a single
> direct mail call using hard coded strings and I confirmed the same
> behavior. I have spent most of today fighting with this and I can
> confidently rule out that the mail() function is being accidentally
> called multiple times (unless PHP is reloading the page multiple times
> before finally rendering back or something else really bizarre)
>
> I am developing locally right now on a WinXP box (SP2) with PHP 5 via
> IIS. If it matters, I had to configure IIS such that .html extensions
> are mapped to "C:\Progra~1\PHP\php5isapi.dll" for GET,POST, and HEAD
> since this clients site is all .html files with embedded PHP. The SMTP
> is an exchange server.
>
You should really think about upgrading to a proper web host and MTA.
There are 3 obvious possiblities:
1) one invocation of mail() is sending lots of messages
2) the page is being invoked multiple times for a single request
3) the MTA is translating one request from the webserver into multiple
emails
Check your webserver log to see how many times the page is being
rendered (2)
Use Wireshark to see how many times the page is being requested by
your browser(2) and what requests it is sending to the MTA (3).
If you're still stuck ask for more help explaining what you have now
discovered.
HTH
C.
Re: Getting multiple emails from a single mail()...
am 02.08.2007 16:00:48 von crazycooter
Unfortunately I don't have any control over this clients hosting
environment and development path, I just need to fix this up and move
on down the road.
As stated, I am 100% sure my logic looping isn't the problem. I
explicitly added a single line, out side of any loop, with static
arguments and received the same behavior (per original post).
Thanks Colin, I will try that.
I will have to confirm there isn't multiple requests (2) for some odd
reason. I did manually submit an email to the server (previous post)
and I only got one as a result, however I will try out WireShark and
see what is actually being submitted (3).
Thanks!
Crazy Cooter
Re: Getting multiple emails from a single mail()...
am 02.08.2007 17:07:24 von crazycooter
This app has the developmestruction scheme (http://
worsethanfailure.com/Articles/The_Developmestuction_Environm ent.aspx)
so it took a while to find the right thing to comment out such that I
was able to isolate the problem. The feed back above pointed me in the
right direction and I found something I probably wouldn't have
otherwise.
I don't fully understand all the mechanics here and since the security
model of the site will change I don't have time to dig and fix. It
looks like broken authentication code, but maybe it really works.
Here it is...
$auth_usr = $_SERVER['PHP_AUTH_USER'];
$auth_pswd = $_SERVER['PHP_AUTH_PW'];
if(!$auth_usr)
{ header('WWW-Authenticate: Basic realm="Auth"');
}
elseif(in_array(strtolower($auth_usr), $usr))
{ $access = 2;
}
If I comment this code out, I only get one email. If I leave this code
in, I get five emails. I can only assume its bouncing back and forth
to the client for the auth, each time causing the page to reload and
running the mail code again.
Debugging output didn't help (unless it would have been to a log file)
since its only going to show output for the last request which
actually rendered.
Thanks all!
CrazyCooter
Re: Getting multiple emails from a single mail()...
am 02.08.2007 17:13:47 von luiheidsgoeroe
On Thu, 02 Aug 2007 17:07:24 +0200, wrote:
> This app has the developmestruction scheme (http://
> worsethanfailure.com/Articles/The_Developmestuction_Environm ent.aspx)
> so it took a while to find the right thing to comment out such that I
> was able to isolate the problem. The feed back above pointed me in the=
> right direction and I found something I probably wouldn't have
> otherwise.
>
> I don't fully understand all the mechanics here and since the security=
> model of the site will change I don't have time to dig and fix. It
> looks like broken authentication code, but maybe it really works.
>
>
> Here it is...
>
> $auth_usr =3D $_SERVER['PHP_AUTH_USER'];
> $auth_pswd =3D $_SERVER['PHP_AUTH_PW'];
>
>
> if(!$auth_usr)
> { header('WWW-Authenticate: Basic realm=3D"Auth"');
echo 'unauthorised';
exit;
> }
> elseif(in_array(strtolower($auth_usr), $usr))
> { $access =3D 2;
> }
>
> If I comment this code out, I only get one email. If I leave this code=
> in, I get five emails. I can only assume its bouncing back and forth
> to the client for the auth, each time causing the page to reload and
> running the mail code again.
Yup, exit after an auth header, possibly supplying a 'not authorised' pa=
ge =
just before. If you don't, the code could be run anyway.
-- =
Rik Wasmus
Re: Getting multiple emails from a single mail()...
am 02.08.2007 19:01:23 von Jerry Stuckle
crazycooter@gmail.com wrote:
> This app has the developmestruction scheme (http://
> worsethanfailure.com/Articles/The_Developmestuction_Environm ent.aspx)
> so it took a while to find the right thing to comment out such that I
> was able to isolate the problem. The feed back above pointed me in the
> right direction and I found something I probably wouldn't have
> otherwise.
>
> I don't fully understand all the mechanics here and since the security
> model of the site will change I don't have time to dig and fix. It
> looks like broken authentication code, but maybe it really works.
>
>
> Here it is...
>
> $auth_usr = $_SERVER['PHP_AUTH_USER'];
> $auth_pswd = $_SERVER['PHP_AUTH_PW'];
>
>
> if(!$auth_usr)
> { header('WWW-Authenticate: Basic realm="Auth"');
> }
> elseif(in_array(strtolower($auth_usr), $usr))
> { $access = 2;
> }
>
> If I comment this code out, I only get one email. If I leave this code
> in, I get five emails. I can only assume its bouncing back and forth
> to the client for the auth, each time causing the page to reload and
> running the mail code again.
>
> Debugging output didn't help (unless it would have been to a log file)
> since its only going to show output for the last request which
> actually rendered.
>
> Thanks all!
>
> CrazyCooter
>
Or, if you set up your webserver configuration properly, the user won't
even be able to get to this page unless he's authorized. The webserver
will stop them.
Then you don't even need this code.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================