문제

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 ());
}
도움이 되었습니까?

해결책

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

}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top