smfdchief Posted July 31, 2007 Share Posted July 31, 2007 Hi - First, I have to admit that I know just enough to get me in over my head . . . I've got this script that I want to use to send SMTP mail from a form that allows the user to select recipients from a dropdown list that is created using database info and send a message. Two of the selections in the dropdown list are groups. My script works fine when the user selects only one recipient. I run into trouble when the user selects one of the two groups. The message ends up getting sent numerous times to the people in the group. I received the message 9 times. I think the script is looping more times than it should. Does this make sense so far? Here's the script that receives the form data and connects to the DB to get the recipient email addresses and then sends the message: <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); // send via SMTP $mail->Host = "mysmtpserver"; // SMTP servers $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "username"; // SMTP username $mail->Password = "password"; // SMTP password $mail->From = ($_POST["senderemail"]); $mail->FromName = ($_POST["name"]); $mail->Subject = ($_POST["thesubject"]); $mail->WordWrap = 50; // set word wrap $mail->IsHTML(false); // send as HTML $username="username"; $password="password"; $database="databasename"; $dbhost="dbhost"; $sendto = ($_POST["sendto"]); $conn = mysql_connect($dbhost, $username, $password); mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM phplog_users WHERE $sendto"; $result=mysql_query($query); mysql_close(); while ($row = mysql_fetch_array ($result)) { // HTML body $body = "From: " . ($_POST["name"]) . "<br>"; $body .= ($_POST["themessage"]); // Plain text body (for mail clients that cannot read HTML) $text_body = "From: " . ($_POST["name"]) . "\n"; $text_body .= ($_POST["themessage"]); $body = nl2br($body); $mail->Body = $body; $mail->AltBody = $text_body; $mail->AddBCC($row["pager1"]); $mail->AddBCC($row["pager2"]); $mail->AddBCC($row["pager3"]); if(!$mail->Send()) echo "There has been a mail error sending to " . $row["email"] . "<br>"; // Clear all addresses and attachments for next loop $mail->ClearAddresses(); $mail->ClearAttachments(); } echo "Message has been sent. <br><br>"; echo "Your message is: <br>"; echo " <br>" . $body . "<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/62605-help-with-code-that-sends-smtp-mail-from-database-info/ Share on other sites More sharing options...
smfdchief Posted August 1, 2007 Author Share Posted August 1, 2007 Bump . . . Quote Link to comment https://forums.phpfreaks.com/topic/62605-help-with-code-that-sends-smtp-mail-from-database-info/#findComment-312443 Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 i believe this is the error $query="SELECT * FROM phplog_users WHERE $sendto"; WHERE $sendto<----loook it should be WHERE field=$sendto am i wrong? Quote Link to comment https://forums.phpfreaks.com/topic/62605-help-with-code-that-sends-smtp-mail-from-database-info/#findComment-312447 Share on other sites More sharing options...
smfdchief Posted August 1, 2007 Author Share Posted August 1, 2007 Teng - Thanks for the reply, however, $sendto actually contains the field=x value passed from the form. I did this because, depending on what group the user selects, the fieldname changes. I did some further checking on this today and discovered that the group contains 9 people - coindidentally the message was sent to each person 9 times, rather than just once to each person. Quote Link to comment https://forums.phpfreaks.com/topic/62605-help-with-code-that-sends-smtp-mail-from-database-info/#findComment-312494 Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 can you do this and give us the ouput print_r(mysql_fetch_array ($result)); Quote Link to comment https://forums.phpfreaks.com/topic/62605-help-with-code-that-sends-smtp-mail-from-database-info/#findComment-312498 Share on other sites More sharing options...
smfdchief Posted August 1, 2007 Author Share Posted August 1, 2007 Okay, I put the suggested code inside my while brackets. I also told it to select just the first name of the recipients for simplicity. For some reason it's only returning 4 names out of the nine that are in the group. Just to clarify, $sendto is being pulled from the form and its value is actually "group=1". $conn = mysql_connect($dbhost, $username, $password); mysql_select_db($database) or die( "Unable to select database"); $query="SELECT FirstName FROM phplog_users WHERE $sendto"; $result=mysql_query($query); mysql_close(); while ($row = mysql_fetch_array ($result)) { print_r(mysql_fetch_array ($result)); } Quote Link to comment https://forums.phpfreaks.com/topic/62605-help-with-code-that-sends-smtp-mail-from-database-info/#findComment-312510 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.