Hey everyone! ;D
I am working on a small/simple email subscribe-unsubscribe script. I have everything working the way I want it, mostly. When an admin would like to send out a message to all of the subscribers, it needs to add the unsubscribe link to the end of the message. My code is doing this, however, it then sends the next person in the list all of the pervious recipients unsubscribe links. I am really at a loss here, can anyone spot what is wrong? Any help is greatly appreciated!!! Thanks in advance.
[php]<?php
$cansend = $_POST[“canSend”];
if($cansend == 1)
{
$mailfrom = "[email protected]";
$mailsubject = mysql_real_escape_string($_POST[“txtSubject”]);
$mailbody = mysql_real_escape_string($_POST[“txtBody”]);
//$mailsto is a with values filled out from users table,
//one of the options selects everyone from the table separated, by, commas.
$mailsto = mysql_real_escape_string($_POST[“txtMailTo”]);
$mailarray = split(",",$mailsto);
$headers = “MIME-Version: 1.0\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1\r\n”;
$headers .= “From: $mailfrom\r\n”;
foreach($mailarray as $mailto)
{
// this is where I am running in to problems,
// the script correctly selects the hash and email from the table
//however, for each iteration of the loop it sends the all previous
//persons unsubscribe link to the next persons email.
$query = mysql_query("SELECT hash FROM users where email=\"$mailto\"");
$hash = mysql_fetch_array($query);
$mailbody .= "Don't like these annoying emails? <a href=\"http://domain.com/unsubscribe.php?id=" . $hash[0] . "\">Unsubscribe</a>";
if(mail($mailto,$mailsubject,$mailbody,$headers))
{
$status = 1;
}
else
{
$status = 2;
$problemid .= $mailto;
}
}
if($status == 1){
echo "<div class=\"alert alert-block alert-success\">
<button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>
<h4>Success!</h4>
Your message has been sent...
</div>";
}
else{
echo "<div class=\"alert alert-block alert-error\">
<button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>
<h4>Error!</h4>
Your message has not been sent...
</div>";
}
}
?>[/php]