hoopplaya4 Posted October 15, 2008 Share Posted October 15, 2008 Hi, I'm pretty new at PHP, and I've been searching the net for a while, but haven't been able to find anything conclusively to help me out. I'm trying to create an email form where the user can select multiple email addresses, pulled from the database, (see screenshot below) and send all selected users an email using PHPMailer. I've been trying to make it work, but just can't do it. If anyone can help out with the script, that'd be greatly appreciated!! Thank you. If you can help me out with both the PHP and HTML, that'd be VERY HELPFUL! Thank you. Here's the form code: <form name="filterplayer" method="post" action="emailscript.php"> <select multiple name="players" id="players" size="5"> <?php $sql = "SELECT usrID, usrEmail"; $sql .= " FROM tblUsers"; $sql .= " WHERE usrActive = 1"; $sql .= " ORDER BY usrEmail"; require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link); if ($rs) { while ($row=mysql_fetch_array($rs)){ print("<option value='" . $row["usrID"] . "'>"); print("" . $row["usrEmail"] . "</option>\n"); } // end while } else { // No Events found print("No Active players"); mysql_close($link); } // end else ($rs) ?> </select> <br><br> <span class="style3">Subject</span><br /> <input name="subjectpost" type="text" size="50" id="subjectpost"><br /><br /> <span class="style3">Message</span><br /> <textarea name="bodypost" cols="50" rows="10" id="bodypost"></textarea> <br /><br /> <input type="submit" name="submit" value="Email!"> </form> And here's the PHP: <?php #include PHPMailer class and database connection include("../phpmailer/class.phpmailer.php"); include("../connection.php"); #remove slashes from content $html = stripslashes($_POST["bodypost"]); $plain = stripslashes($_POST["bodypost"]); #initiate PHPMailer class $mail = new PHPMailer(); #set the from e-mail address $mail->From = "myemail@gmail.com"; #set the from name $mail->FromName = "Scheduler"; #set the e-mail type to HTML $mail->IsHTML(true); #the subject of the email $mail->Subject = stripslashes($_POST["subjectpost"]); #the HTML content of the email $mail->Body = $html; #the plain text version $mail->AltBody = $plain; #add subscribers address as the recipient $mail->AddAddress = ($_POST["players"]); #sends the newsletter $mail->Send(); #clears the recipient address #$mail->ClearAddresses(); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted October 15, 2008 Share Posted October 15, 2008 What is not working? Quote Link to comment Share on other sites More sharing options...
Andy17 Posted October 15, 2008 Share Posted October 15, 2008 Try to change $sql .= " WHERE usrActive = 1"; To $sql .= " WHERE usrActive = '1'"; Quote Link to comment Share on other sites More sharing options...
hoopplaya4 Posted October 15, 2008 Author Share Posted October 15, 2008 Hi revraz and andy: Thanks for the reply. I tried to fix the '1' portion, but that didn't work. The specific issue is: the emails are not sending. I am trying to select one email address (or multiple) and it is not sending to those addresses. I am able to send to the whole database when I run a $row, however, I want to be able to send to selected email addresses. Any other ideas? Thanks again! Quote Link to comment Share on other sites More sharing options...
hoopplaya4 Posted October 15, 2008 Author Share Posted October 15, 2008 OK, so I figured out why it wasn't sending. In emailscript.php the AddAddress command was not correct. So, I changed it to: $mail->AddAddress($_POST["players"]); as opposed to the incorrect: $mail->AddAddress = ($_POST["players"]); Now, however, it will only work when I select one of the email addresses in the list. However, if I try to hold CTRL, and select multiple email addresses, it will only send to one. Is there a way I can get it to send to several selected emails? ??? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted October 15, 2008 Share Posted October 15, 2008 would you not have to set it so each email is divided by a ; me@u.com;nextemail@u.com .. otherwise from what your trying to do you will end up with email@u.comnext@u.com ect ect.... i would say add each email to the list 1 by 1 so select email add... click add to recipatents and carry on.. hope that makes sence, best way to think about it is how most email systems work such as hotmail and yahoo Quote Link to comment Share on other sites More sharing options...
hoopplaya4 Posted October 15, 2008 Author Share Posted October 15, 2008 Thanks for the reply. How would I write a script for something like that? Also, even when I select multiple emails, it still sends to one email address, so it must not be coming out as something like: email1@email.comemail2@email.com Hmm....hopefully we can get to the bottom of this. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted October 15, 2008 Share Posted October 15, 2008 think you would have to loop them form the db /* CONNECT TO DB HERE */ $sql = "SELECT * FROM mail_list" ; //<-- DB STUFF $result = mysql_query ($sql) ; while ($row = mysql_fetch_array($result)) //<-- LOOP THE RESULTS { mail($row["Email"], $subject, $body, $headers) ; // Send Email for each record found } but not sure how youd select the right ones Quote Link to comment Share on other sites More sharing options...
hoopplaya4 Posted October 15, 2008 Author Share Posted October 15, 2008 Okay, so here's what I did and it worked!! Form: <select multiple name="players[]"> //loop out players from database </select> Script: $players = $_POST['players']; foreach ( $players as $p ) { $mail->AddAddress ($p); } 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.