I worked on the code based on http://www.daniweb.com/web-development/php/threads/123389
but as per last answer to use the mail function outside loop ( http://www.daniweb.com/web-development/php/threads/123389/708835#post708835), I still cant work with this script. Please help

<?

$query="SELECT * FROM tbl_users WHERE ex_date between now() and adddate(now(), INTERVAL 7 DAY) ORDER BY user_id ASC";
$result=mysql_query($query);
 

$num=mysql_numrows($result);

?>

<?php
$i=0;
while ($i < $num) 

{
 
$id=mysql_result($result,$i,"user_id");
$email=mysql_result($result,$i,"login_email");
$loginname=mysql_result($result,$i,"loginname");
$ex_date=mysql_result($result,$i,"ex_date");
$ex_date = date("d-m-Y", strtotime($ex_date) );

?>

<?php
$i++;
}
?>
<?
if($query == TRUE)
    {
           
      //send email
   
      $to = "$email";
  
      $subject = "Expriy Notice";
   
      $from = "Admin";
   
  
      $msg .= "Hello $loginname,<BR><BR>";
  
      $msg .= "Your Account will expire in 7 days. Your Expiry Date is $ex_date<BR><BR>";

      $msg .= "Regards: <BR><BR>Admin";  	

      $msg .= "Admin";
 
       
  
      $headers = "MIME-Version: 1.0" . "\r\n";
  
      $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
  
      $headers .= "From: Admin\r\nReply-To:sammy@gmail.com" . "\r\n";
 
       
 
      $mailsend = mail("$to","$subject","$msg","$headers");
  
      echo $mailsend ? "Email was sent :-)." : " Email sending failed.";
}
      
?>

What am i doing wrong?

Recommended Answers

All 16 Replies

Member Avatar for diafol

So what's not working for you?

Hi ardav,

there are 3 clients email which suppose to go, but it only goes to 1st email and not the other two.

The sending of the email should be INSIDE the while loop, not after it. Unless you want to send one e-mail with everybody in CC. Tell us what you want to happen first.

i want to send email to respective member whose account will expire in certain time. but mail matter has to be individual and mail id has to be individual. and each mail should go to respective member with out cc or at once.

when i keep inside the while loop then it goes to all but meantime, one consolidate mail with all clients name and message matter goes, that was failed too

What do you mean with

one consolidate mail with all clients name and message matter goes, that was failed too

Your query should return only records for users whose expiry date is 7 days or less. That is why your mailing code should go into the while loop which steps through the result set. I suggest you check what your query returns. comment out lines 60 and 62 and add this code on line 63:

echo "$email<br />";

That should list emails that are supposed to get the message. Make sure that the query returns the emails you expect.

Also, what is the point in a condition in line 30

if($query == TRUE)

which is always true?

What do you mean with

I mean, when the mail part gets triggered, the mail goes like this to all individual mail as below:

Dear user1.
Your expiry date is on (date here).
Dear User2,
Your expiry date is on (date here).
Dear User3,
Your expiry date is on (date here).

this way it goes to each member a mail.

Your query should return only records for users whose expiry date is 7 days or less. That is why your mailing code should go into the while loop which steps through the result set. I suggest you check what your query returns. comment out lines 60 and 62 and add this code on line 63:

echo "$email<br />";

That should list emails that are supposed to get the message. Make sure that the query returns the emails you expect.

Hi thanks for helping me out, this only helped me to echo list of emails but the mail did not get sent.

But was the list of emails correct (only the users that were supposed to get the mail)?

Yes, the list is perfectly fine and as expected.

Can you put the if block in the while loop (on the end of the loop), insert this code before the end line in if block and post the result:

echo "<p>Variable i: $i<br />";
echo "Email: $to<br />";
echo "Subject: $subject<br />";
echo "Message: $msg</p>";

Other remarks:

You should change the code on line 42 to

$msg = "Hello $loginname,<BR><BR>";

(no concatenation on the beginning of building up a message, oterwise the $msg will grow).

The quotes arround the mail function parameters are unnecesary (they cause unnecesary parsing of variables in strings).

All code could be in one block of <?php ?>

Just to help you a little bit here is the code for easier debugging:

<?php
$query = "SELECT * FROM tbl_users WHERE ex_date between now() and adddate(now(), INTERVAL 7 DAY) ORDER BY user_id ASC";
$result = mysql_query($query);

$num = mysql_numrows($result);

/* DEBUG CODE*/
echo "<p>Number of rows: $num</p>";
/* END DEBUG CODE*/

$i = 0;

while ($i < $num)
{
    $id = mysql_result($result,$i,"user_id");
    $email = mysql_result($result,$i,"login_email");
    $loginname = mysql_result($result,$i,"loginname");
    $ex_date = mysql_result($result,$i,"ex_date");
    $ex_date = date("d-m-Y", strtotime($ex_date) );

    //send email

    $to = "$email";

    $subject = "Expriy Notice";

    $from = "Admin";

    $msg = "Hello $loginname,<BR><BR>";
    $msg .= "Your Account will expire in 7 days. Your Expiry Date is $ex_date<br /><br />";
    $msg .= "Regards: <br /><br />Admin";
    $msg .= "Admin";

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "From: Admin\r\nReply-To:sammy@gmail.com" . "\r\n";

    /* DEBUG CODE*/
    echo "<p>Variable i: $i<br />";
    echo "Email: $to<br />";
    echo "Subject: $subject<br />";
    echo "Message: $msg<br />";
    echo "Mail code: mail($to,$subject,$msg,$headers)</p>";
    /* END DEBUG CODE*/

    // Temporarily commented out
    // $mailsend = mail($to,$subject,$msg,$headers);

    // Temporarily commented out
    // echo $mailsend ? "Email was sent :-)." : " Email sending failed.";

    $i++;
}
?>

It might help if you post what gets displayed in your browser.

Firstly, you are a Life Saver and thank you very much for taking so much of time to work on my code, very appreciated.

Definitely Debug code helped me to understand the output and everything went smoothly.

I have only work on the header part as the header is going in the name of my server / host name rest is perfect.

Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.