Hi everyone
I'm new here and posting because I have a problem with a PHP script I'm trying to write. I'm testing it as it goes along, but having a problem.

I want to select certain people from my database, and send each of them an e-mail. To test, I replaced their e-mail address with mine, so from the sample criteria I chose (ID greater than 800) I should have 5 e-mails, each personalised.

However, the first e-mail is fine, but the 2nd e-mail contains details of person 1 and person 2. The 3rd e-mail contains details of person 1, person 2 and person 3 etc. The screen echo shows 'Mail was sent' 5 times.

I think this is to do with how it is looping, i.e. where the } is? But I can't figure it out.

Please could you nice people take a look and tell me where it's going wrong?

Here is the code:

<?
    mysql_connect("xxx.xx.xxx.xx","username","password");
    mysql_select_db("database");
    $query="select * from tbl_signup_user WHERE userID > 800"; 
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    while ($i < $num)
    {
        $userName='$row[user_name]';
        $firstName='$row[first_name]';
        $endDate='$row[endDate]';
    //$id=mysql_result($result,$i,"id");
    $firstName=mysql_result($result,$i,"first_name");
    $userName=mysql_result($result,$i,"user_name");
    $endDate=mysql_result($result,$i,"endDate");


    if($query == TRUE)
    {

    //send email
    $to = "test@mydomain.co.uk";
    $subject = "Profile expiration";
    $from = "Admin";

    $msg .="Hi $firstName <BR><BR>As you may be aware your $userName profile on";
    $msg .=" our website is due to expire on $endDate <BR><BR>"; 
    $msg .="If you wish to continue being a member of our directory, please renew.<BR><BR>"; 
    $msg .="Thanks,<BR><BR>"; 
    $msg .="Admin";

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "From: My website renewals <test@mydomain.co.uk>" . "\r\n";

    $mailsend = mail("$to","$subject","$msg","$headers");
    echo $mailsend ? "Email was sent" : "Email sending failed";

    $i++;
    }
    }
    ?>

Hope you can help
Thanks
Andy

Member Avatar for LastMitch

I think this is to do with how it is looping, i.e. where the } is? But I can't figure it out.

What do you mean you don't know where the close bracket?

You code is correct.

There's no need another close bracket.

However, the first e-mail is fine, but the 2nd e-mail contains details of person 1 and person 2. The 3rd e-mail contains details of person 1, person 2 and person 3 etc. The screen echo shows 'Mail was sent' 5 times.

I don't know what are you trying to accomplish here?

You want a foreach loop all the address with messages?

@andymcr, my bad if you got spammed lol. I was just testing your script and forgot to change out your email address >.<

Initialize $msg each time inside the loop, maybe somewhere near line 27 add

$msg = '';

commented: Nice one! +5

Thanks @chrisranjana - that worked perfectly! :)

Hey andymcr, if all is good with this, please mark the thread solved for us :)

Thanks

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.