Jump to content

[SOLVED] PHPMailer MySQL Query - Multiple Email


hoopplaya4

Recommended Posts

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

?> 

 

optionqr5.th.jpgthpix.gif

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?    ???

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.