4

I am sending emails using PHPmailer. As of now, I am successful in sending email to one address. Now, I want to send multiple emails in just one click.

PROBLEM: I have tried to use some loops below to send multiple email but I get wrong outpout. Yes, it sends email but to only one address, and the email address is getting all of the emails that are supposed to be emailed to other emails.

For example, when I send 17 emails, those 17 emails are sent to only one address. The emails should be sent according to the addresses in the database, with corresponding unique attachments. Example: abc@gmail.com should have abc.pdf attached, and 123@gmail.com should have 123.pdf attached.

I think it's in the loop. Please help me figure it out. Thanks.

require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php"); 

$mail             = new PHPMailer();

$body             = file_get_contents('phpmailer/body.html');
$body             = preg_replace('/\/b]/','',$body);

$file ='phpmailer/mailpass.txt';
    if($handle = fopen($file,"r")){
        $contentpass = fread($handle,'15');
        fclose($handle);
        }

$mail->IsSMTP(); 
$mail->Host       = "smtp.gmail.com"; 
$mail->SMTPDebug  = 1;                   

$mail->SMTPAuth   = true;                  
$mail->SMTPSecure = "tls";                 
$mail->Host       = "smtp.gmail.com";      
$mail->Port       = 587;                   
$mail->Username   = "email@gmail.com";  
$mail->Password   = $contentpass;           

$mail->SetFrom("email@gmail.com", "Subject");

$mail->AddReplyTo("email@gmail.com","Subject");

$mail->Subject    = "Subjects";

$mail->AltBody    = "Subject";

$mail->MsgHTML($body);


$file='current_schoolyear.txt';
    if($handle = fopen($file,"r"))
    {
        $content = fread($handle,'9');
            fclose($handle);
    }



$input = addslashes($_POST['depchair']);                        


$email = "select email_address  from sa_student where schoolyear = '$input'"; 



if ($p_address=mysql_query($email))
{ 



  while($row = mysql_fetch_assoc($p_address))
  {



    $mail->AddAddress($row['email_address']);

    $input = addslashes($_POST['depchair']);                                                                                    

    $control = "select control_no  from sa_student where schoolyear = '$input'";

    if($ctrl=mysql_query($control)){

        $ctrl_no = mysql_result($ctrl, 0);


        $mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");  


    }
    else
    {

        echo "No attached document.";

    }

            if(!$mail->Send()) {
                    $message = "<div class=\"nNote nFailure\" >
                                    <p>Error sending email. " . $mail->ErrorInfo ."</p>
                                </div>";

            } else { 
                    $message = "<div class=\"nNote nSuccess\" >
                                    <p> Email have been sent to the examinees in ".$input_depchair. "! </p>
                                </div>";                            

                        }



       }

    }



else
{
    echo (mysql_error ());
}

UPDATED CODE: After running the code below, I was able to send an email and with the correct attachment. However, there was only ONE email sent (the last email address in the database), and the rest of the emails were not sent.

$input = addslashes($_POST['depchair']);                        


$email = "select email_address, control_no  from sa_student where schoolyear = '$input'"; 



if ($p_address=mysql_query($email))
{ 



  while($row = mysql_fetch_assoc($p_address))
  {

    $cloned = clone $mail;

    $cloned->AddAddress($row['email_address']);




        $cloned->AddAttachment("fpdf/pdf_reports/document/".$row['control_no'].".pdf");  




            if(!$cloned->Send()) {
                    $message = "<div class=\"nNote nFailure\" >
                                    <p>Error sending email. " . $mail->ErrorInfo ."</p>
                                </div>";

            } else { 
                    $message = "<div class=\"nNote nSuccess\" >
                                    <p> Email have been sent to the examinees in ".$input_depchair. "! </p>
                                </div>";                            

                        }
unset( $cloned );


       }

    }



else
{
    echo (mysql_error ());
}
user3404474
  • 45
  • 1
  • 5

2 Answers2

6

After you send an email $mail->Send(), execute this:

$mail->ClearAllRecipients();

in your while loop.
So your basic while loop structure looks like this:

while($row = mysql_fetch_assoc($p_address)){

    $mail->AddAddress($row['email_address']);
    $mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");
    $mail->send();
    $mail->ClearAllRecipients(); 
    $mail->ClearAttachments();   //Remove all attachements

}
Krimson
  • 6,306
  • 9
  • 47
  • 86
2

Within your loop, create a clone of the $mail object - before you add the recipient and attachment - then use the clone to send the email. The next loop iteration will create a new clone free of the previous address and attachment:

while($row = mysql_fetch_assoc($p_address)) {

    $cloned = clone $mail;

    $cloned->AddAddress($row['email_address']);

    // add attchment to $cloned, etc.

    if ( $cloned->send() ) { /* etc */ }

    unset( $cloned );

}

This will "clear" your per-iteration changes (like address, attachment, etc) without you having to reenter config properties (like, from, host, etc.)

Addendum:

Your attachments will likely be all the same because you're not fetching new results for these lines (within your loop):

$input=addslashes($_POST['depchair']);

$control = "select control_no  from sa_student where schoolyear = '$input'";

if ($ctrl=mysql_query($control)) {

    $ctrl_no = mysql_result($ctrl, 0);

    $mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");

}

$ctrl_no will always return the same result because (I'm assuming) $_POST['depchair'] does not change - thus $input, $control, $ctrl, and $ctrl_no all remain (effectively) the same for each loop. You need to find whatever it is your actually intend to be the $ctrl_no for each loop - right now you're using the same one over and over.

The following query could probably help:

// replace
// $email = "select email_address  from sa_student where schoolyear = '$input'";

// with:
$students_query = "select email_address, control_no from sa_student where schoolyear = '$input'";

// then
// if ($p_address=mysql_query($email)) {
// while($row = mysql_fetch_assoc($p_address)) {

// becomes
if ( $students=mysql_query($students_query) ) {
while ( $row = mysql_fetch_assoc( $students ) ) {

// so that finally, etc
$cloned->AddAddress($row['email_address']);
$ctrl_no = $row['control_no'];

This pulls both the student email address and their control_no in the same query, making sure they stay associated with each other through the loop. You can then get rid of the second mid-loop query, since all the results you need were pulled in the first out-of-loop query. The above isn't all the code you need to change, just the critical parts.

Brian North
  • 1,382
  • 1
  • 14
  • 19
  • Hi, it was successful in sending emails, but the emails have the same attachments. It should be unique. – user3404474 Mar 11 '14 at 04:58
  • If you're attaching the right file to `$cloned` (**not** `$mail`) then there is no reason it should be the same attachment to all emails. Either you're still attaching it to `$mail` or your `$ctrl_no = mysql_result($ctrl, 0);` line is not producing unique results – Brian North Mar 11 '14 at 05:01
  • The control_no in the database is unique. I think it's in the way how I fetch it. Do you have any idea how to do it right? – user3404474 Mar 11 '14 at 05:26
  • It doesn't work. Now all of the emails contain all of the attachments. Like an email has 17 attachments now. – user3404474 Mar 11 '14 at 05:55
  • The probable culprit is you're still using `$mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");` and not `$cloned->AddAttachment...` With all the changes so far, probably best to post an update to your code so it's easier to keep track of. – Brian North Mar 11 '14 at 05:57
  • Brian? Are you still there? – user3404474 Mar 11 '14 at 07:01