Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Posts
    184
    Thanks
    19
    Thanked 0 Times in 0 Posts

    First record missing when sending out PHP mail

    Have a strange issue when sending out mail from a simple form with a PHP mail handler behind it. It grabs the email addresses from a field called "Email" in the mySQL db.

    If I have say five email addresses (five records) in the table, it will send out emails to addresses 2 to 5 but not 1 (i.e not the first record in the table)

    If I have three, it will send out to 2 and 3, but again not 1.

    if I have just one, it doesn't send out the email.

    It seems to just ignore the first record in the table but I can't see anything in the code that would make it do this.

    This is the code, anyone know what the problem might be?

    PHP Code:
    <?php
    include ('../inc/dbconnect.php');

    $emailtext mysql_real_escape_string(@$_POST['_EmailText']);

    // Build SQL Query  
    $query "select * from emailcontacts ORDER BY Email ASC"// specify the table and field names for the SQL query
     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);
    // get results
      
    $result mysql_query($query) or die("Couldn't execute query");
      
    // display the results returned
    while ($rowmysql_fetch_array($result)) {
      
    $emailaddress $row["Email"];
      
    $id $row["ID"];

        
    $mailto $emailaddress;
        
    $mimebound md5(uniqid(time()));
        
    $subject "Email from Emailer Function";
        
        
    $headers .="From: Emailer <sales@mysite.com>\n";
        
    $headers .="MIME-Version: 1.0\n";
        
    $headers .="Content-Type: multipart/mixed; boundary=\"".$mimebound."\"\n\n";
        
    $headers .="This is a multi-part message in MIME format.\n\n";
        
        while(
    $row mysql_fetch_array($result)){
                
    $message .="--".$mimebound."\n";
                
    $message .="Content-type:text/plain; charset=iso-8859-1\n";
                
    $message .="Content-Transfer-Encoding: 7bit\n";
                
    $message .="Content-Disposition: inline\n\n";
                
    $message .="Enquiry from the website\n";
                
    $message .="--------------------\n\n";
                
    $message .= mysql_real_escape_string(@$_POST['_EmailText']) . "\n";
                
    $message .="--\n\n";
             
    mail($row['Email'], $subject$message$headers);
            }
            
    $count++ ;
    }
    echo 
    '</table>';
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You've two queries with the same result name and you are fetching twice in a "single" iteration.
    Remove these three lines:
    PHP Code:
     $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);
    // get results
      
    $result mysql_query($query) or die("Couldn't execute query"); 
    And replace them with
    PHP Code:
    $result mysql_query($query) or die("Couldn't execute query " mysql_error()); 
    And remove the nested while loop. The body of it can retained, just not the loop.
    Dumped the count of records as well since its not used in here anywhere.

  • #3
    Regular Coder
    Join Date
    Sep 2009
    Posts
    184
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Thanks, that sort of works, but the odd thing I found when testing is that if an email is sent out, and then another email sent out to the same address, it repeats the message in the body of the email for some reason? It seems to add the message again for each time it sends an email to that address- so having tested with the same email addresses a few times I've found that they're getting the message repeated several times in the email...

    This is the code I now have:

    PHP Code:
    <?php
    include ('../inc/dbconnect.php');

    $emailtext $_POST['_EmailText'];

    // Build SQL Query  
    $query "select * from emailcontacts ORDER BY Email ASC"// specify the table and field names for the SQL query

    $result mysql_query($query) or die("Couldn't execute query " mysql_error());
      
    // display the results returned
    while ($rowmysql_fetch_array($result)) {
      
    $emailaddress $row["Email"];
      
    $id $row["ID"];

        
    $mailto $emailaddress;
        
    $mimebound md5(uniqid(time()));
        
    $subject "Email from Emailer Function";
        
        
    $headers .="From: Emailer <sales@mysite.com>\n";
        
    $headers .="MIME-Version: 1.0\n";
        
    $headers .="Content-Type: multipart/mixed; boundary=\"".$mimebound."\"\n\n";
        
    $headers .="This is a multi-part message in MIME format.\n\n";
        
                
    $message .="--".$mimebound."\n";
            
    $message .="Content-type:text/plain; charset=iso-8859-1\n";
                
    $message .="Content-Transfer-Encoding: 7bit\n";
                
    $message .="Content-Disposition: inline\n\n";
                
    $message .="Enquiry from the website\n";
                
    $message .="----------------\n\n";
                
    $message .= $_POST['_EmailText'] . "\n";
                
    $message .="--\n\n";
             
    mail($row['Email'], $subject$message$headers);
        
    }
    echo 
    '</table>';
    ?>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Because $message has not been declared within the while loop, only appending is occurring. Add a $message = ""; within the first step of the loop. Same goes for any of these concatenated variables like $headers.

  • Users who have thanked Fou-Lu for this post:

    galahad3 (04-19-2012)

  • #5
    Regular Coder
    Join Date
    Sep 2009
    Posts
    184
    Thanks
    19
    Thanked 0 Times in 0 Posts
    That's done the trick, emptying both headers and message. Thanks for your help with that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •