Jump to content
phppup

send email from database problem

Recommended Posts

I am having a LOOP problem but cannot seem to resolve it.  Maybe some better skilled eyes can re-organize and remedy this.  In its presented form, I receive 4 blank emails to 'myrealaddress@xyz.com' which is connecting to my table 'x' which holds 213 records (of which I am querying records with ID>=210.  The problem is that the content is missing.

 

At best, I've been able to get the content to display in the first email sent [iD: 210] and then the next email (which would be for ID 211 is blank.  No other emails follow, so I assume the loop is being broken.

 

Please assist.


echo "HELLLOOOO <br />";

  
    $email_to = "myrealaddress@xyz.com";        
    $email_subject = " email subject line";

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];

  $query  = "SELECT * FROM x WHERE id>='210'  "; 


$result = mysqli_query($link, $query);    

if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

@mail($email_to, $email_subject, $email_message, $headers); 

 echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ;

 
}


$headers = 'From: '.$row['email']."\r\n".
'Reply-To: '.$row['email']."\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail($email_to, $email_subject, $email_message, $headers);  

    $email_message = "Form details below.\n\n";    
 
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    } 
 

    $email_message .= "ID: ".clean_string($row['id'])."\n";
    $email_message .= "First Name: ".clean_string($row['first_name'])."\n";
    $email_message .= "Last Name: ".clean_string($row['last_name'])."\n";
    $email_message .= "Email: ".clean_string($row['email'])."\n";
    $email_message .= "Telephone: ".clean_string($row['telephone'])."\n\n";

  
echo "end <br/>";


/*

$headers = 'From: '.$row['email']."\r\n".
'Reply-To: '.$row['email']."\r\n" .
'X-Mailer: PHP/' . phpversion();
//  @mail($email_to, $email_subject, $email_message, $headers);  

 */

 } 
 
 
echo "Thank you for contacting us. We will be in touch with you very soon.<br />";


Share this post


Link to post
Share on other sites

You've got alot of things out of order here. You're creating your headers after you send the e-mail. You've got the ending bracket for your database loop before the headers.  This may not be perfect. But it should get you closer. 


echo "HELLLOOOO <br />";

  
    $email_to = "myrealaddress@xyz.com";        
    $email_subject = " email subject line";

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];

  $query  = "SELECT * FROM x WHERE id>='210'  "; 


$result = mysqli_query($link, $query);    

if (mysqli_num_rows($result) > 0) {
 
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

// Let's create the headers //
$headers = 'From: '.$row['email']."\r\n".
'Reply-To: '.$row['email']."\r\n" .
'X-Mailer: PHP/' . phpversion();

$email_message = "Form details below.\n\n"; 
$email_message .= "ID: ".clean_string($row['id'])."\n";
$email_message .= "First Name: ".clean_string($row['first_name'])."\n";
$email_message .= "Last Name: ".clean_string($row['last_name'])."\n";
$email_message .= "Email: ".clean_string($row['email'])."\n";
$email_message .= "Telephone: ".clean_string($row['telephone'])."\n\n";
@mail($email_to, $email_subject, $email_message, $headers); 

 echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ;

 
}

           
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    } 
 

   

  
echo "end <br/>";

 } 
 
 
echo "Thank you for contacting us. We will be in touch with you very soon.<br />";


Share this post


Link to post
Share on other sites

Same problem exists, just a different way of getting there.  Your solution gives me field names in email message, but no variable info.

Thanks for the effort, but I'm still stuck on this one.

Share this post


Link to post
Share on other sites

Does table "x" have columns called

  • first_name
  • last_name
  • email
  • telephone

?

Share this post


Link to post
Share on other sites

Yes, table x has those columns.

As indicated initially in my second paragraph, I have managed to receive 1 email that has complete message content. My problem at that point, is that only one additional email is dispatched, and it has a totally blank message.

I am convinced that there is an issue with my loop but I don't see it.

Share this post


Link to post
Share on other sites

Are you posting your actual code?

 

The first code you posted sent all four emails before any content was put into the $email_message variable, so it's hard to see how you managed to get even one correct email.

Share this post


Link to post
Share on other sites

That is it.

Can it be juggled or repaired to function as desired?

Share this post


Link to post
Share on other sites

comment out the line that actually sends the e-mail then do a print_r on the $row to make sure you're actually getting the info you're expecting

//@mail($email_to, $email_subject, $email_message, $headers); 
print_r($row); 

Share this post


Link to post
Share on other sites

That is it.

Can it be juggled or repaired to function as desired?

 

That is what taquitosensei's code did for you.

Share this post


Link to post
Share on other sites

I will check it again tomorrow and see if there are any abnormalities. Thanks for the help.

Share this post


Link to post
Share on other sites

And can you you post the output from the  print_r($row)  that he suggested you put in. We can't see your screen so you need to show the results if want us to diagnose the fault.

Share this post


Link to post
Share on other sites

Will do.

While on the subject, where would I include COUNT and "echo $count" in order to get a final total of number of emails sent?

Share this post


Link to post
Share on other sites

Inside the loop - I would iincrement a counter when the call to mail() is successful.

Share this post


Link to post
Share on other sites

Can you show me the correct formatting of the code, please.

Share this post


Link to post
Share on other sites

$count = 0;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

// create header and message here

if ( mail($email_to, $email_subject, $email_message, $headers) ) {
++$count;
echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ;
}
}
echo "$count messages were sent<br>";

Share this post


Link to post
Share on other sites

Substituted //@mail($email_to, $email_subject, $email_message, $headers);
print_r($row);

 

and got no results. 

 

Although I've not used print_r and am unsure of what to expect I have been monitoring my live email. the results are as follows:

My initial 'bad' script sends ONLY one email with a blank message.

The repaired script sends no emails but displays my "HELLO" message only.

The repaired script with an invalid database name sends no emails but displays my "HELLO" and "Thank you...." messages.

 

Clearly, the items in the loop are not making contact correctly.

HELP, please.

Share this post


Link to post
Share on other sites

Post the exact code you have right now, so we can verify that you've implemented the diagnostics correct.

 

On the face of it, having no output indicates that the query is either failing entirely or generating no results, so that explains why you would have no email. 

Share this post


Link to post
Share on other sites

I copy and pasted the exact code from taquitosensei's post.

Then went back to code from my original post.

Share this post


Link to post
Share on other sites

Then went back to code from my original post.

Why? We told you it's wrong. Or do you think if you run it enough times it may work?

 

Why haven't you got error reporting turned on? You should be getting a screenful of warnings and notices from that code.

 

From what you have told us so far, I think the fault is with the data (database, table, connection or the query).

 

Each print_r() should output something like

 

Array ( [ID] => 1 [firstname] => John [last_name] => Doe [email] => j.doe@gmail.com [telephone] => 1236549087 )

showing the column names with their values.

Share this post


Link to post
Share on other sites

Even my limited knowledge has allowed me to have confidence in the db and table that I am using.

I have a separate script with the same or similar connections to the same table and it successfully echoes all variables in a table.

My sense is that there is a disorder in the format of my email or a misplaced } .

Either way it appears that the code is allowing a break in the loop (which actually seems NOT to be looping, but rather only running once(at best).

Sadly, my original code still comes closest to a successful outcome (since it at least Manfred to send one email. LoL

Share this post


Link to post
Share on other sites

You have more confidence in your db than I have.

 

I have created a test table

CREATE TABLE `x` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(45) DEFAULT NULL,
  `last_name` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `telephone` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and put a couple of records into it

+----+------------+-----------+--------------+-----------+
| id | first_name | last_name | email        | telephone |
+----+------------+-----------+--------------+-----------+
|  1 | John       | Doe       | jdoe@gm.com  | 123       |
|  2 | Jane       | Doe       | janed.gm.com | 234       |
+----+------------+-----------+--------------+-----------+

This code, which is basically what taquitosensei posted

echo "HELLLOOOO <br />";

 
$email_to = "myrealaddress@xyz.com";        
$email_subject = " email subject line";

$query  = "SELECT * FROM x WHERE id>='1'  ";

$result = mysqli_query($link, $query);    

if (mysqli_num_rows($result) > 0) {

    $count = 0;
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

        // Let's create the headers //
        $headers = 'From: '.$row['email']."\r\n".
        'Reply-To: '.$row['email']."\r\n" .
        'X-Mailer: PHP/' . phpversion();

        $email_message = "Form details below.\n\n";
        $email_message .= "ID: ".clean_string($row['id'])."\n";
        $email_message .= "First Name: ".clean_string($row['first_name'])."\n";
        $email_message .= "Last Name: ".clean_string($row['last_name'])."\n";
        $email_message .= "Email: ".clean_string($row['email'])."\n";
        $email_message .= "Telephone: ".clean_string($row['telephone'])."\n\n";
    //    if ( mail($email_to, $email_subject, $email_message, $headers) ) {          // temporarily suppress email
        if (true) {
            echo '<pre>', print_r($row, 1), '</pre>';
            echo "<pre>$email_message</pre>";
            ++$count;
            echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ;
        }
    }
    echo "$count emails sent <br/>";
    echo "Thank you for contacting us. We will be in touch with you very soon.<br />";

 }
 
 
 
         
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
 

gave this result

Edited by Barand

Share this post


Link to post
Share on other sites

I had decided not to let this script bother me, but I couldn't keep away from it. Then I saw your reply.

Created a new file with my connection info and WHAM-O, I got a printout of 223 records (not Ordered By) with the printed message stop of each one and all data fields for each.

THANK YOU.

I notice that you "suppressed email" and I don't want to damage this code. If you can guide me to enable it I can give it a real run.

I will compare it with the other messes that I have to see where my mistakes were.

Thank you for the lesson.

Share this post


Link to post
Share on other sites

Also, noticed that you left function clean_string at the bottom of script. Does this make it ineffective? Do I actually need it anyway (if variables are being validated before inserting into db?

Share this post


Link to post
Share on other sites

Uncomment the if (mail() ) line and remove the if (true) line.

 

You also need to change your WHERE clause back to your value.

 

You can now remove the debugging print lines too.

 

Also, noticed that you left function clean_string at the bottom of script. Does this make it ineffective? Do I actually need it anyway (if variables are being validated before inserting into db?

That is where function definitions should go (or at the top), not in the middle of the flow of code logic.

Share this post


Link to post
Share on other sites

Tested live and all confirmation messages printed as desired.

Then waited for email.

And waited.

And worried.

And waited.

Then remembered a little thing called a spam folder. ROFLMAO.

Emails were received and message contents corresponds accordingly.

Thanks again.

(I'll wait till next week to compare and educate.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.