GamerGun Posted March 22, 2010 Share Posted March 22, 2010 Dear, The following code is meant to send the same single 'letter' to all recipients (with an confirmed email address) within the database. This works, but unfortunately it screws up the 'sorting' of addresses and names, so that person X gets the name from person Y. The DB column is set up like this: ID - naam (name) - email - code - bevestigd (confirmed) Any idea on how to fix this? For the record; running this script outputs "Mail verzonden!" 3 times, which matches with the 3 valid email address within the database. Thanks in advance. Best regards, Tommy <?php include 'config/connect.php'; $sendmail = mysql_real_escape_string($_GET['sendmail']); if ($sendmail=="yes") { $result = mysql_query("SELECT * FROM berichten WHERE TO_DAYS(NOW())-TO_DAYS(datum) <=7 ORDER BY id DESC") or die(mysql_error()); function neat_trim($str, $n, $delim='...') { $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } $sql = "SELECT * FROM nieuwsbrief WHERE bevestigd = '1'"; $query = mysql_query($sql); $num_rows = mysql_num_rows($query); if ($num_rows == 0){ echo "Het databeest is boos..."; } else { while ($row = mysql_fetch_array($query)) { $naam = $row['naam']; $email = $row['email']; $to = "$email"; $from = "admin@watmijoverkwam.nl"; $headers = "Van: $from\r\n"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=utf-8\r\n"; $subject = "Nieuwsbrief www.watmijoverkwam.nl"; $body .= "Beste $naam,"; $body .= "<br /><br />"; $body .= "Hierbij sturen we je de nieuwste verhalen van afgelopen week."; $body .= "<br /><br />"; while($row = mysql_fetch_array($result)){ $id = $row['id']; $message = $row['bericht']; $onderwerp = $row['onderwerp']; $titel = $row['titel']; $titel2 = eregi_replace(' ', '-', $titel); $titel2 = strtolower($titel2); $titellink = "<a href=\"http://watmijoverkwam.nl/$titel2-$id.html\">$titel</a>"; $verderlezen = "<a href=\"http://watmijoverkwam.nl/$titel2-$id.html\">Lees verder</a>"; $dezepagina = "<a href=\"http://watmijoverkwam.nl/afmelden.html\">afmeldpagina</a>"; $url = "<a href=\"http://watmijoverkwam.nl/\">http://watmijoverkwam.nl/</a>"; $body .= $titellink; $body .= " ("; $body .= $onderwerp; $body .= ")"; $body .= "<br />"; $body .= neat_trim($message, 120); $body .= "<br />"; $body .= $verderlezen; $body .= "<br /><br />"; } $body .= "Je ontvangt deze nieuwsbrief omdat je bent aangemeld op $url."; $body .= "<br />"; $body .= "Wil je deze nieuwsbrief niet meer ontvangen?"; $body .= "<br />"; $body .= "Meldt je dan af op de "; $body .= $dezepagina; $body .= "."; $body .= "<br /><br />"; $body .= "Met vriendelijke groeten,"; $body .= "<br />"; $body .= "$url"; mail($to, $subject, $body, $headers); echo "Mail verzonden!"; } } } else { echo "We versturen niks!"; } ?> Quote Link to comment Share on other sites More sharing options...
GamerGun Posted March 25, 2010 Author Share Posted March 25, 2010 Well, i changed the code into this: <?php include 'config/connect.php'; $sendmail = mysql_real_escape_string($_GET['sendmail']); if ($sendmail=="yes") { $from="admin@watmijoverkwam.nl"; $table="nieuwsbrief"; $table_email="email"; $table_naam="naam"; $resultquery = mysql_db_query($database, "select * from $table where bevestigd='1'"); $result = mysql_query("SELECT * FROM berichten WHERE TO_DAYS(NOW())-TO_DAYS(datum) <=7 ORDER BY id DESC") or die(mysql_error()); function neat_trim($str, $n, $delim='...') { $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } while ($query = mysql_fetch_array($resultquery)) { $beste=$query[$table_naam]; while($row = mysql_fetch_array($result)){ $headers = "Van: $from\r\n"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=utf-8\r\n"; $subject = "Nieuwsbrief www.watmijoverkwam.nl"; $body .= "Beste $beste,"; $body .= "<br /><br />"; $body .= "Hierbij sturen we je de nieuwste verhalen van afgelopen week."; $body .= "<br /><br />"; $id = $row['id']; $message = $row['bericht']; $onderwerp = $row['onderwerp']; $titel = $row['titel']; $titel2 = eregi_replace(' ', '-', $titel); $titel2 = strtolower($titel2); $titellink = "<a href=\"http://watmijoverkwam.nl/$titel2-$id.html\">$titel</a>"; $verderlezen = "<a href=\"http://watmijoverkwam.nl/$titel2-$id.html\">Lees verder</a>"; $dezepagina = "<a href=\"http://watmijoverkwam.nl/afmelden.html\">afmeldpagina</a>"; $url = "<a href=\"http://watmijoverkwam.nl/\">http://watmijoverkwam.nl/</a>"; $body .= $titellink; $body .= " ("; $body .= $onderwerp; $body .= ")"; $body .= "<br />"; $body .= neat_trim($message, 120); $body .= "<br />"; $body .= $verderlezen; $body .= "<br /><br />"; $body .= "Je ontvangt deze nieuwsbrief omdat je bent aangemeld op $url."; $body .= "<br />"; $body .= "Wil je deze nieuwsbrief niet meer ontvangen?"; $body .= "<br />"; $body .= "Meldt je dan af op de "; $body .= $dezepagina; $body .= "."; $body .= "<br /><br />"; $body .= "Met vriendelijke groeten,"; $body .= "<br />"; $body .= "$url"; } $mailto=$query[$table_email]; mail($mailto, $subject, $body, $headers); echo 'Mail sent to '.$beste.' '.$mailto.'<br>'; sleep($seconds); } echo 'Mails sent.'; } else { echo 'Error.'; } ?> The database contains: Name: A Email: A@A.com Name: B Email: B@B.com Name: C Email: C@C.com (All confirmed) When running the script, it says: Mail sent to A A@A.com Mail sent to B B@B.com Mail sent to C C@C.com Mails sent. So that is correct, but all the users (A, B and C) get an e-mail which says: Beste (Dear) A. So something goes wrong with the loop and the $body .= "Beste $beste,"; Any idea why? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
prometheos Posted March 25, 2010 Share Posted March 25, 2010 it might be because $beste is only ever assigned a value once? Quote Link to comment Share on other sites More sharing options...
GamerGun Posted March 25, 2010 Author Share Posted March 25, 2010 No, $beste=$query[$table_naam]; is in the same loop as $mailto=$query[$table_email]; and that one works fine. Only difference is the placing of the variables (at the top and at the bottom). Thx Quote Link to comment Share on other sites More sharing options...
GamerGun Posted March 29, 2010 Author Share Posted March 29, 2010 Fixed it, changed the way of working but oh well: send.php <?php include '../header.php'; ?> <div id="content"> <div id="contentleft"> <h2>Nieuwsbrief opstellen</h2> <form method="post" action="mailer.php"> Speed<br> <select name="speed"> <option value="1">Insane</option> <option value="4">Fast</option> <option value="8" selected="selected">Normal</option> <option value="12">Slow</option> </select> <br> Message:<br> <textarea name="message" rows="25" cols="75" wrap="off"> <?php $result = mysql_query("SELECT * FROM berichten WHERE TO_DAYS(NOW())-TO_DAYS(datum) <=14 ORDER BY id DESC") or die(mysql_error()); function neat_trim($str, $n, $delim='...') { $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } while($row = mysql_fetch_array($result)){ $id = $row['id']; $message = $row['bericht']; $onderwerp = $row['onderwerp']; $titel = $row['titel']; $titel2 = eregi_replace(' ', '-', $titel); $titel2 = strtolower($titel2); $titellink = "<a href=\"http://domain/$titel2-$id.html\">$titel</a>"; $verderlezen = "<a href=\"http://domain/$titel2-$id.html\">Lees verder</a>"; echo $titellink; echo " ("; echo $onderwerp; echo ")"; echo "\n<br />"; echo neat_trim($message, 120); echo "\n<br />"; echo $verderlezen; echo "\n\n<br /><br />"; } ?> </textarea> <br> <input type="submit" name="start sending" value="Start Sending" > </form> <br /> </div><!-- Closing contentleft --> <?php include '../menu.php'; ?> <?php include '../footer.php'; ?> </div> </body> </html> mailer.php <?php include '../header.php'; ?> <div id="content"> <div id="contentleft"> <h2>Nieuwsbrieven versturen</h2> <?php $inum1 = rand(0,255); $inum2 = rand(0,255); $inum3 = rand(0,255); $inum4 = rand(0,255); $ip = $inum1.'.'.$inum2.'.'.$inum3.'.'.$inum4; $sql = mysql_query('SELECT * FROM `nieuwsbrief` WHERE `bevestigd` = 1') or die(mysql_error()); $num_rows = mysql_num_rows($sql)or die(mysql_error()); while ($row = mysql_fetch_array($sql)) { $slp = $_POST["speed"]; sleep($slp); $beste = $row["naam"]; $to = $row["email"]; $week = date('W'); $subject = "Nieuwsbrief www.domain week $week"; $sender = "admin@domain"; $headers = "From: $sender\r\n"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=utf-8\r\n"; $headers .= "Reply-To: $sender"; $dezepagina = "<a href=\"http://domain/afmelden.html\">afmeldpagina</a>."; $url = "<a href=\"http://domain\">http://domain/</a>"; $msg = $_POST["message"]; $var1 = "Beste $beste,<br /><br />Hierbij sturen we je de nieuwste verhalen van afgelopen week.<br /><br />"; $var2 = $msg; $var3 = "Je ontvangt deze nieuwsbrief omdat je bent aangemeld op $url.<br />Wil je deze nieuwsbrief niet meer ontvangen?<br />Meldt je dan af op de $dezepagina"; $var4 = "<br /><br />Met vriendelijke groeten,<br />$url"; $fullmsg = $var1.$var2.$var3.$var4; mail($to,$subject,$fullmsg,$headers); echo "Nieuwsbrief verzonden aan: " . $row["naam"] . " - " . $row["email"] . "<br />\n"; } ?> <br /> </div><!-- Closing contentleft --> <?php include '../menu.php'; ?> <?php include '../footer.php'; ?> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.