Solved

Why is PHP mail sending me duplicates?

Posted on 2013-05-30
6
222 Views
Last Modified: 2013-06-01
Hi Guys,
I need some diagnostic help desperately!
I created this PHP mail program for a client and in testing everything functioned properly -- until yesterday when the client sent out an email to her customers.

There were 62 customers in the db and they all went out properly as best as I could tell.
The problem is that I included a Blind CC to me just so that I could monitor it for any problems. And boy did I get problems!

Instead of getting 62 emails sent to my mailbox I got over 1200 messages! There were anywhere between 18 and 25 duplicates of each of the 62 messages!

I don't know if any of the recipients received more than one message, and I don't want to ask my client if she's experienced any problems or complaints until I get a solution.

Why is it doing that? Where did I screw up?
Thanks in advance for any help on this.
Sas
<?php
$admin_ID=$_REQUEST['admin_ID'];
$PIN=$_REQUEST['P'];
$admin_title=$_REQUEST['aTitle'];
$eMail=$_REQUEST['aEmail'];
$admin_f_name=$_REQUEST['aFname'];
$admin_m_name=$_REQUEST['aMname'];
$admin_l_name=$_REQUEST['aLname'];
$p_pin=$_REQUEST['P'];
$eMail=$_REQUEST['eMail'];	

$TableName=$_REQUEST['TableName'];

$Subject=$_REQUEST['Subject'];
$Subject=stripslashes($Subject);
$SubjectAlt = str_replace("&","[amp]", $Subject);
$SubjectAlt = str_replace("%","[percent]", $SubjectAlt);
$SubjectAlt = str_replace("+","[plus]", $SubjectAlt);
$SubjectAlt = str_replace("=","[equal]", $SubjectAlt);
$SubjectAlt = str_replace("#","[number]", $SubjectAlt);
$SubjectAlt = str_replace("'","[apost]", $SubjectAlt);


$Headline=$_REQUEST['Headline'];
$Headline=stripslashes($Headline);
$HeadlineAlt = str_replace("&","[amp]", $Headline);
$HeadlineAlt = str_replace("%","[percent]", $HeadlineAlt);
$HeadlineAlt = str_replace("+","[plus]", $HeadlineAlt);
$HeadlineAlt = str_replace("=","[equal]", $HeadlineAlt);
$HeadlineAlt = str_replace("#","[number]", $HeadlineAlt);
$HeadlineAlt = str_replace("'","[apost]", $HeadlineAlt);

$SubHead=$_REQUEST['SubHead'];
$SubHead=stripslashes($SubHead);
$SubHeadAlt = str_replace("&","[amp]", $SubHead);
$SubHeadAlt = str_replace("%","[percent]", $SubHeadAlt);
$SubHeadAlt = str_replace("+","[plus]", $SubHeadAlt);
$SubHeadAlt = str_replace("=","[equal]", $SubHeadAlt);
$SubHeadAlt = str_replace("#","[number]", $SubHeadAlt);
$SubHeadAlt = str_replace("'","[apost]", $SubHeadAlt);

$MainBox=$_REQUEST['MainBox'];
$MainBox=stripslashes($MainBox);
$MainBoxAlt = str_replace("&","[amp]", $MainBox);
$MainBoxAlt = str_replace("%","[percent]", $MainBoxAlt);
$MainBoxAlt = str_replace("+","[plus]", $MainBoxAlt);
$MainBoxAlt = str_replace("=","[equal]", $MainBoxAlt);
$MainBoxAlt = str_replace("#","[number]", $MainBoxAlt);
$MainBoxAlt = str_replace("'","[apost]", $MainBoxAlt);

$HeadlineTD=$_REQUEST['HeadlineTD'];
$HeadlineTD=stripslashes($HeadlineTD);
$SubHeadTD=$_REQUEST['SubHeadTD'];
$SubHeadTD=stripslashes($SubHeadTD);

$bgcolor=$_REQUEST['bgcolor'];

// subject
$subject = "$Subject";
$HeadlineTD = "$HeadlineTD";

$TableName=$_REQUEST['TableName'];	

$WHICH_eMail="E_eMail";	

// ............................HARVEST EMAIL DATA...................................... //

include "../../../../db_SasNaktivsClient.inc";

$link = mysql_connect("$dbhost", "$dbusername", "$dbuserpass");
mysql_select_db("$dbname", $link);

$result = mysql_query("SELECT * FROM $TableName WHERE  E_status=1", $link);
$num_rows = mysql_num_rows($result);

$Targeted = mysql_query("SELECT * FROM $TableName  where E_status='1'  ", $link);
$Targeted_rows = mysql_num_rows($Targeted);

$getlist="SELECT * from $TableName WHERE  E_status='1'  order by 'E_l_name' ASC "; //lists employees in ABC order 
      $getlist2=mysql_query($getlist) or die("Could not get your mailing list");
      while($getlist3=mysql_fetch_array($getlist2))
      {

$Target_eMail= $getlist3['E_eMail'];
$Target_ID= $getlist3['id'];

// multiple recipients
$to  = "$Target_eMail" . ', '; 

// message
$message = "

<html>
<head>
    <title>$Subject</title>
    <style type=\"text/css\">
div.c8 {text-align: center}
    td.SubHead {font-size:22px;font-family:arial;text-align:center}
    td.TAG {FONT-SIZE:20px; font-family:Arial; color: #840822}
    td.c7 {font-size:9px;font-family:arial;text-align:right}
    td.c6 {FONT-SIZE:8px; font-family:Arial; color: #000000}
    span.c5 {color: gray}
    td.c4 {font-size:18px;font-family:arial;color:#960F30;}
    td.c3 {font-size:15px;font-family:arial;padding-left: 40px;text-decoration:none}
    th.c2 {text-align:right;font-size:26px;font-family:arial;color:#800822;}
    th.c1 {font-size:56px;font-family:gill sans,arial;line-height:52px;letter-spacing:-2px;}
    
    
a:link    {color:#840822;text-decoration:none;}
a:visited {color:#840822;text-decoration:none;}
a:hover   {color:#009F07;text-decoration:none;}
a:active  {color:#FFD100;text-decoration:none;}
    </style>

</head>

<body>

<!-- ............................................................................... START WEB DATA -->
<center>
<FONT face='arial,verdana,helvetica' size='2' color='#000000' >
<A HREF='http://SasNaktivsClient.com/DIRECTORY/W_D_TARGET.php?bgcolor=$bgcolor&Subject=$subjectAlt&Target_ID=$Target_ID&Headline=$HeadlineAlt&SubHead=$SubHeadAlt&MainBox=$MainBoxAlt&Target_eMail=$Target_eMailAlt&Subject=$subjectAlt&TableName=$TableName&WHICH_eMail=$WHICH_eMail' >
If you're having trouble viewing this email, click here to see it online.</A>
</font>
</center><P>
<!-- ................................................................................. END WEB DATA -->

    <div class=c8><center>
        <table border=1 border-color=#800822 width=500 cellpadding=5 cellspacing=0>
            <tr>
                <td colspan=2 bgcolor=#$bgcolor>
                    <table border=0 width=500 cellpadding=0 cellspacing=8>
                        <tr>
                            <td colspan=2>
                                <hr size=1 width=500 color=#840822>
                            </td>
                        </tr>

$HeadlineTD
$SubHeadTD
                              
<tr><td class=c3><span style=color:#000000;>
  $MainBox</td></tr>

                        <tr>
                            <td colspan=2 valign=middle align=right width=500>
                                <table border=0 width=500>
                                    <tr>
                                        <td colspan=2></td>
                                    </tr>

                                    <tr>
                                        <td colspan=2 valign=top align=right class=c4><b><img src=http://www.SasNaktivsClient.com/FILES/ART/Tag_640x125.png alt=\"Sas Naktivs Client\" border=0 width=500></b></td>
                                    </tr>
                                </table>

                                <table border=0 width=500 bgcolor=#$bgcolor>
                                    <tr>
                                        <td  valign=bottom colspan=3 class=c7>
                                            <hr size=1 width=100% color=#800822>
                                            <br>
                                            If you do not want anymore email from Sas Naktivs Client you can 
                                            <a href='http://SasNaktivsClient.com/WEB/OptOut_D_Promo.php?Target_eMail=$Target_eMail&Target_ID=$Target_ID&TableName=$TableName&WHICH_eMail=$WHICH_eMail' Method=GET>
                                            Opt-Out</a>.
                                       
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
   </center> </div>
</body>
</html>
";
$Target_eMail= $getlist3['E_eMail'];

$to  = "$Target_eMail" . ', '; // note the comma

$message ="$message" ;
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: Sas Naktivs Client <NoReply@SasNaktivsClient.com>' . "\r\n";
$headers .= 'Bcc: sasnaktiv@XXXXXXXXXXXX.com' . "\r\n";
// Mail it
mail($getlist3['E_eMail'], $subject, $message, $headers);
}
?>
<!-- TELL THE CLIENT THE EMAIL WAS SUCCESSFULLY SENT -->
<? date_default_timezone_set('America/New_York');       
$Today = date ("l, F d, Y, h:i A");    
echo"<center>
<P>&nbsp;<P> &nbsp;<P> 
        <table border=0 border-color=#800822 width=500 cellpadding=5 cellspacing=0>
            <tr>
                <td colspan=1 bgcolor=#FFFFFF>

Subject: $Subject <BR>Headline: $Headline <BR><P><B>$Targeted_rows SENT </B><BR>$Today 

<form action='../MainMenu.php' method='POST'>
<input type='HIDDEN'  name='admin_eMail' id='admin_eMail' value=$admin_eMail >
<input type='HIDDEN'  name='eMail' id='eMail' value=$eMail >
<input type='HIDDEN'  name='D_PIN' id='D_PIN'  value='$p_pin' >
<input type='HIDDEN'  name='admin_ID' id='admin_ID'  value='$admin_ID' >
 
<input type='submit' id='submit' value='Return to the Main Menu'  style='width: 200px; height:22px;  FONT-SIZE:12px; font-family:Arial; color: #EE0000'>
</form>
</td></tr></table></center>
";?>

Open in new window

0
Comment
Question by:sasnaktiv
  • 3
  • 3
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39209277
I can't follow the logic here.  You might want to learn about coding standards.  This will make your code much more readable and therefore easier to debug.

Here is what I would do.  Create the SSCCE from this example.  Throw away everything except the logic necessary to create the mail messages.   Then instead of sending them, just write them to a log file.  You can look at the log file and maybe you will be able to discern a pattern that would suggest why you got so many messages.

Maybe this will help.  I assume you have scrubbed the data base to ensure there are no duplicate records, right?

<?php

// RAISE THE ERROR REPORTING LEVEL AND DO INITIALIZATION 
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');       


$admin_ID=$_REQUEST['admin_ID'];
$PIN=$_REQUEST['P'];
$admin_title=$_REQUEST['aTitle'];
$eMail=$_REQUEST['aEmail'];
$admin_f_name=$_REQUEST['aFname'];
$admin_m_name=$_REQUEST['aMname'];
$admin_l_name=$_REQUEST['aLname'];
$p_pin=$_REQUEST['P'];
$eMail=$_REQUEST['eMail'];	

$TableName=$_REQUEST['TableName'];

$Subject=$_REQUEST['Subject'];
$Subject=stripslashes($Subject);
$SubjectAlt = str_replace("&","[amp]", $Subject);
$SubjectAlt = str_replace("%","[percent]", $SubjectAlt);
$SubjectAlt = str_replace("+","[plus]", $SubjectAlt);
$SubjectAlt = str_replace("=","[equal]", $SubjectAlt);
$SubjectAlt = str_replace("#","[number]", $SubjectAlt);
$SubjectAlt = str_replace("'","[apost]", $SubjectAlt);


$Headline=$_REQUEST['Headline'];
$Headline=stripslashes($Headline);
$HeadlineAlt = str_replace("&","[amp]", $Headline);
$HeadlineAlt = str_replace("%","[percent]", $HeadlineAlt);
$HeadlineAlt = str_replace("+","[plus]", $HeadlineAlt);
$HeadlineAlt = str_replace("=","[equal]", $HeadlineAlt);
$HeadlineAlt = str_replace("#","[number]", $HeadlineAlt);
$HeadlineAlt = str_replace("'","[apost]", $HeadlineAlt);

$SubHead=$_REQUEST['SubHead'];
$SubHead=stripslashes($SubHead);
$SubHeadAlt = str_replace("&","[amp]", $SubHead);
$SubHeadAlt = str_replace("%","[percent]", $SubHeadAlt);
$SubHeadAlt = str_replace("+","[plus]", $SubHeadAlt);
$SubHeadAlt = str_replace("=","[equal]", $SubHeadAlt);
$SubHeadAlt = str_replace("#","[number]", $SubHeadAlt);
$SubHeadAlt = str_replace("'","[apost]", $SubHeadAlt);

$MainBox=$_REQUEST['MainBox'];
$MainBox=stripslashes($MainBox);
$MainBoxAlt = str_replace("&","[amp]", $MainBox);
$MainBoxAlt = str_replace("%","[percent]", $MainBoxAlt);
$MainBoxAlt = str_replace("+","[plus]", $MainBoxAlt);
$MainBoxAlt = str_replace("=","[equal]", $MainBoxAlt);
$MainBoxAlt = str_replace("#","[number]", $MainBoxAlt);
$MainBoxAlt = str_replace("'","[apost]", $MainBoxAlt);

$HeadlineTD=$_REQUEST['HeadlineTD'];
$HeadlineTD=stripslashes($HeadlineTD);
$SubHeadTD=$_REQUEST['SubHeadTD'];
$SubHeadTD=stripslashes($SubHeadTD);

$bgcolor=$_REQUEST['bgcolor'];

// subject
$subject = "$Subject";
$HeadlineTD = "$HeadlineTD";

$TableName=$_REQUEST['TableName'];	

$WHICH_eMail="E_eMail";	

// ............................HARVEST EMAIL DATA...................................... //

include "../../../../db_SasNaktivsClient.inc";

$link = mysql_connect("$dbhost", "$dbusername", "$dbuserpass");
mysql_select_db("$dbname", $link);

// OMIT THESE UNUSED LINES OF CODE
// $result = mysql_query("SELECT * FROM $TableName WHERE  E_status=1", $link);
// $num_rows = mysql_num_rows($result);

// WHY NOT USE THE MYSQL_NUM_ROWS ON THE QUERY THAT BUILDS THE TRUE RESULTS SET?
$Targeted = mysql_query("SELECT * FROM $TableName  where E_status='1'  ", $link);
$Targeted_rows = mysql_num_rows($Targeted);

$getlist="SELECT * from $TableName WHERE  E_status='1'  order by 'E_l_name' ASC "; //lists employees in ABC order 
$getlist2=mysql_query($getlist) or die("Could not get your mailing list");
while($getlist3=mysql_fetch_array($getlist2))
{
    $Target_eMail= $getlist3['E_eMail'];
    $Target_ID= $getlist3['id'];

    // multiple recipients
    $to  = "$Target_eMail" . ', '; 

    // message
    $message = "
<html>
<head>
    <title>$Subject</title>
    <style type=\"text/css\">
div.c8 {text-align: center}
    td.SubHead {font-size:22px;font-family:arial;text-align:center}
    td.TAG {FONT-SIZE:20px; font-family:Arial; color: #840822}
    td.c7 {font-size:9px;font-family:arial;text-align:right}
    td.c6 {FONT-SIZE:8px; font-family:Arial; color: #000000}
    span.c5 {color: gray}
    td.c4 {font-size:18px;font-family:arial;color:#960F30;}
    td.c3 {font-size:15px;font-family:arial;padding-left: 40px;text-decoration:none}
    th.c2 {text-align:right;font-size:26px;font-family:arial;color:#800822;}
    th.c1 {font-size:56px;font-family:gill sans,arial;line-height:52px;letter-spacing:-2px;}
    
    
a:link    {color:#840822;text-decoration:none;}
a:visited {color:#840822;text-decoration:none;}
a:hover   {color:#009F07;text-decoration:none;}
a:active  {color:#FFD100;text-decoration:none;}
    </style>

</head>

<body>

<!-- ............................................................................... START WEB DATA -->
<center>
<FONT face='arial,verdana,helvetica' size='2' color='#000000' >
<A HREF='http://SasNaktivsClient.com/DIRECTORY/W_D_TARGET.php?bgcolor=$bgcolor&Subject=$subjectAlt&Target_ID=$Target_ID&Headline=$HeadlineAlt&SubHead=$SubHeadAlt&MainBox=$MainBoxAlt&Target_eMail=$Target_eMailAlt&Subject=$subjectAlt&TableName=$TableName&WHICH_eMail=$WHICH_eMail' >
If you're having trouble viewing this email, click here to see it online.</A>
</font>
</center><P>
<!-- ................................................................................. END WEB DATA -->

    <div class=c8><center>
        <table border=1 border-color=#800822 width=500 cellpadding=5 cellspacing=0>
            <tr>
                <td colspan=2 bgcolor=#$bgcolor>
                    <table border=0 width=500 cellpadding=0 cellspacing=8>
                        <tr>
                            <td colspan=2>
                                <hr size=1 width=500 color=#840822>
                            </td>
                        </tr>

$HeadlineTD
$SubHeadTD
                              
<tr><td class=c3><span style=color:#000000;>
  $MainBox</td></tr>

                        <tr>
                            <td colspan=2 valign=middle align=right width=500>
                                <table border=0 width=500>
                                    <tr>
                                        <td colspan=2></td>
                                    </tr>

                                    <tr>
                                        <td colspan=2 valign=top align=right class=c4><b><img src=http://www.SasNaktivsClient.com/FILES/ART/Tag_640x125.png alt=\"Sas Naktivs Client\" border=0 width=500></b></td>
                                    </tr>
                                </table>

                                <table border=0 width=500 bgcolor=#$bgcolor>
                                    <tr>
                                        <td  valign=bottom colspan=3 class=c7>
                                            <hr size=1 width=100% color=#800822>
                                            <br>
                                            If you do not want anymore email from Sas Naktivs Client you can 
                                            <a href='http://SasNaktivsClient.com/WEB/OptOut_D_Promo.php?Target_eMail=$Target_eMail&Target_ID=$Target_ID&TableName=$TableName&WHICH_eMail=$WHICH_eMail' Method=GET>
                                            Opt-Out</a>.
                                       
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
   </center> </div>
</body>
</html>
";

    // REDUNDANT?  SEE LINE 90
    $Target_eMail = $getlist3['E_eMail'];

    $to  = "$Target_eMail" . ', '; // note the comma

    $message ="$message" ;
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'From: Sas Naktivs Client <NoReply@SasNaktivsClient.com>' . "\r\n";
    $headers .= 'Bcc: sasnaktiv@XXXXXXXXXXXX.com' . "\r\n";
    
    // DO NOT Mail it
    // mail($getlist3['E_eMail'], $subject, $message, $headers);
    
    //  LOG THE MESSAGE INSTEAD
    var_dump($getlist3);
}
?>
<!-- TELL THE CLIENT THE EMAIL WAS SUCCESSFULLY SENT -->
<?php
$Today = date ("l, F d, Y, h:i A");    
echo "<center>
<P>&nbsp;<P> &nbsp;<P> 
        <table border=0 border-color=#800822 width=500 cellpadding=5 cellspacing=0>
            <tr>
                <td colspan=1 bgcolor=#FFFFFF>

Subject: $Subject <BR>Headline: $Headline <BR><P><B>$Targeted_rows SENT </B><BR>$Today 

<form action='../MainMenu.php' method='POST'>
<input type='HIDDEN'  name='admin_eMail' id='admin_eMail' value=$admin_eMail >
<input type='HIDDEN'  name='eMail' id='eMail' value=$eMail >
<input type='HIDDEN'  name='D_PIN' id='D_PIN'  value='$p_pin' >
<input type='HIDDEN'  name='admin_ID' id='admin_ID'  value='$admin_ID' >
 
<input type='submit' id='submit' value='Return to the Main Menu'  style='width: 200px; height:22px;  FONT-SIZE:12px; font-family:Arial; color: #EE0000'>
</form>
</td></tr></table></center>
";

Open in new window

0
 
LVL 1

Author Comment

by:sasnaktiv
ID: 39209345
Correct Ray. No duplicates in the db.
Sas
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39209368
Why do you add commas at the end of the email address (line 190 above)?  Is there a strategy at work there?

If you choose mysql_fetch_assoc() or mysql_fetch_object() you will get half as much data back from the query results set.  I don't think it affects functionality here, but it's a big performance hit to choose mysql_fetch_array() when you can avoid it.

Try running the script I posted and see what you get (use "view source" to make the data easy to read).  It would be interesting to see if the script has a logic error or if the email server got the hiccups.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sasnaktiv
ID: 39209906
Thanks Ray, will do.
Sas
0
 
LVL 1

Author Closing Comment

by:sasnaktiv
ID: 39212236
The latest is that I can't seem to duplicate the problem. Everything is working fine with the original script as well as the adjustments you suggested. Maybe it was something my client did.
Thanks for the help & have a good weekend,
Sas
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39212805
Wow, I'm glad it's OK now.  Thanks for the points and good weekend to you, too. ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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.

679 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