Jump to content

Recommended Posts

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>";

?>

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.

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));

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.