Jump to content

store while loop for mail


Bertholt

Recommended Posts

Hello,

 

It is probably a simple question but I am new to php...

 

How can i insert the results of a while loop into an email? How can i store the results of the loop and later add this into an email send with php? I added the loop below. 

 

Thanx in advance....

 

 

<?php

$sql = "SELECT * FROM machine WHERE DATE_SUB(NOW(),INTERVAL 7 DAY) <= `update_time`";

$result1 = mysql_query($sql);

$EnDecryptText = new EnDecryptText();

 

    while($row = mysql_fetch_assoc($result1))

    {

        echo $row['name'].'<br>';

$machine_encrypt = $EnDecryptText->Encrypt_Text($row['machine_id']);

        echo'http://'.$_SERVER['HTTP_HOST'].'/machine_new.php?'.$machine_encrypt.'<br><br><br>';

    }

    ?>

Link to comment
Share on other sites

Create a blank string outside the while loop ($msg=""; ).  Then, keep filling it with .= for whatever you want.  Send that as your email message after you get out of the loop.

 

mail ($email, $sub, $msg);

 

I have tried to create the file but it still wont work: Here is my code...

 

if (mysql_num_rows($result1) > 0) {

while ($email_arr = mysql_fetch_array ($result2)) {

 

$to = $email_arr['email'];

 

$from = "info@test.com";

$subject = "Registration";

$msg="Dear ".$email_arr['firstname']." ".$email_arr['lastname']."\n\n";

$msg.="We have added some new items that might interest you.\n\n";

$msg.="Click on the following link to visit items :\n";

$msg1="";

while($row = mysql_fetch_assoc($result1))

    {

        $msg1.= $row['name'].'<br>';

$machine_encrypt = $EnDecryptText->Encrypt_Text($row['machine_id']);

        $msg1.= 'http://'.$_SERVER['HTTP_HOST'].'/machine_new.php?'.$machine_encrypt.'<br><br><br>';

 

 

$mailsend = mail($to, $subject, $msg1, "From: $from");

$send_info_email_arr .= "\n".$to."\n";

 

}

 

 

any ideas?

 

 

Link to comment
Share on other sites

When you say, "it won't work," what do you mean?  What do you get in the email (or not), and what would you like to see exactly? 

 

BTW, your $msg is not getting sent, only $msg1.  You are also missing a semi-colon on that second line in the loop.

Link to comment
Share on other sites

When you say, "it won't work," what do you mean?  What do you get in the email (or not), and what would you like to see exactly? 

 

BTW, your $msg is not getting sent, only $msg1.  You are also missing a semi-colon on that second line in the loop.

 

Well, i created msg1 just for a test and i get the mail but the data from the while loop aren't in it .. It is just an empty mail.

 

Link to comment
Share on other sites

Thanks for the quick replies. Underneath I add the whole php code. I am sure the loop works because i tested him in an new empty document. I get the mails send to the $to variable. I also get the $msg text (Dear NAME etc. ) i just dont get the data from the while loop (see BOLD text)

 

 

 

<?php 


// Connection  database + encrypt script

require_once('../Connections/conn.php'); 
include('../lib/endecrypt.php');

mysql_select_db($database_conn, $conn);

// Get machines of last week

$sql = "SELECT * FROM machine WHERE DATE_SUB(NOW(),INTERVAL 7 DAY) <= `update_time`"; 
$result1 = mysql_query($sql);
$EnDecryptText = new EnDecryptText();

// Get the data from the users

$selectSQL = "SELECT * FROM user WHERE keepinformed = 1";
if ($rowMachine['status_id'] != 5) {

// If not 'for sale' only select the valid S&N-users
$selectSQL .= " AND valid = 1";
}
$result2 = mysql_query($selectSQL); 

	     
// Send Mail

if (mysql_num_rows($result1) > 0) {
while ($email_arr = mysql_fetch_array ($result2)) {

//$to = $email_arr['email'];
$to = "test@test.be";
$from = "info@test.com";
$subject = "Registration test.com";
$msg="Dear ".$email_arr['firstname']." ".$email_arr['lastname']."\n\n";
$msg.="We have added some new machines at Secondhandbev.com that might interest you.\n\n";
$msg.="Click on the following link to visit the machine sheets :\n";
while($row = mysql_fetch_assoc($result1))
    {
        $msg.= $row['name'].'<br>';
	$machine_encrypt = $EnDecryptText->Encrypt_Text($row['machine_id']);
        $msg.= 'http://'.$_SERVER['HTTP_HOST'].'/machine_new.php?'.$machine_encrypt.'<br><br><br>';
    }


$mailsend = mail($to, $subject, $msg1, "From: $from");
$send_info_email_arr .= "\n".$to."\n";

}

// Send delivery mail 
$info_msg.= "I sent mail to the following people: <BR><BR>\n";
$info_msg.= $send_info_email_arr;
$info_mail = mail('test@test.com', "Cron job", $info_msg, "From: webmaster@test.com");

} else{

$info_msg = " no results this week";
$info_mail = mail('test@test.com',"Cron job voor machines", $info_msg, "From: webmaster@test.com");

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.