简体   繁体   中英

multiple attachments going with single mail from phpmailer

I am sending payslip mails with payslips as attachment with phpmailer class. the problem is the first mail is going with one attachment but the sedonf mail is going with the first and the second attachments together. For example: mail for employee name : A is going with A.pdf mail for employee name : B is going with A.pdf and B.pdf

need some help. my project completion date is tomorrow and I am stuck in this last problem. this is my code:

<?php
 require_once 'mailerClass/PHPMailerAutoload.php';
 require_once '../connect.php';

 $mail = new PHPMailer;

//$mail->isSMTP();

$sql = "SELECT * FROM mail ORDER BY Id";
$query = mysqli_query($con, $sql);
    while($row = mysqli_fetch_array($query, MYSQL_ASSOC)){

    $mail->SMTPDebug = 2;

    $mail->Debugoutput = 'html';

    $mail->Host = 'smtp.gmail.com';

    $mail->Port = 587;

    $mail->SMTPSecure = 'tls';

    $mail->SMTPAuth = false;

    $mail->Username ='riteshrc13@gmail.com';

    $mail->Password = "password";

    $mail->setFrom('64mediakraft@gmail.com', 'Mediakraft');

    $mail->addAddress($row['Email'], $row['Name']);

    $mail->Subject = "Payslip of " . $row['Name'];  

    $mail->Body = "payslip email";                      

    $mail->AltBody = 'Payslip Email for the month. Please find the payslip attached.';

    $mail->isHTML(true);  

    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );


    $pdf = "C:/Reports/" . $row['Name']. ".pdf";
    $mail->addAttachment($pdf);     


    if ($mail->send()) {
        echo "<script>alert('Mail Sent success');</script>";
    //  header("Location:index.php");
    }
    else {
        echo "<script>alert('Mailer Error: ' $mail->ErrorInfo);</script>";
    //  header("Location: index.php");
    } 
    $pdf = "";

    } //endwhile

?>

Creating a new instance inside the loop will work, but it's very inefficient and means you can't use keepalive, which makes a huge difference to throughput.

Base your code on the mailing list example provided with PHPMailer which shows how to send most efficiently, and read the docs on sending to lists . To paraphrase that example, it should go roughly like this:

$mail = new PHPMailer;
//Set properties that are common to all messages...
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->Host = 'mail.example.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Subject = 'Hello';
$mail->From = 'user@example.com';
//etc
//Loop over whatever resource gives you your recipients
foreach ($result as $target) {
  //Set properties that are specific to this message
  $this->addAddress($target['email']);
  $this->addAttachment($target['file']);
  //Send the message
  $this->send();
  //All done, so clear recipients and attachments for next time around
  $mail->clearAddresses();
  $mail->clearAttachments();
}

Don't forget to add some error checking in there, and I can also see that you're using an old version of PHPMailer - so get the latest, and base your code on the mailing list example.

$mail = new PHPMailer; // this should be inside of while, I think...

Thanks to @jonStirling and @toor for the help. complete working code for other help seekers:

<?php
 require_once 'mailerClass/PHPMailerAutoload.php';
 require_once '../connect.php';

//$mail->isSMTP();

$counter =  1;

$sql = "SELECT * FROM mail ORDER BY Id";
$query = mysqli_query($con, $sql);
    while($row = mysqli_fetch_array($query, MYSQL_ASSOC)){

    $mail = new PHPMailer;

    $mail->SMTPDebug = 2;

    $mail->Debugoutput = 'html';

    $mail->Host = 'smtp.gmail.com';

    $mail->Port = 587;

    $mail->SMTPSecure = 'tls';

    $mail->SMTPAuth = false;

    $mail->Username ='riteshrc13@gmail.com';

    $mail->Password = "password";

    $mail->setFrom('64mediakraft@gmail.com', 'Mediakraft');

    $mail->addAddress($row['Email'], $row['Name']);

    $mail->Subject = "Payslip of " . $row['Name'];  

    $mail->Body = "payslip email";                      

    $mail->AltBody = 'Payslip Email for the month. Please find the payslip attached.';

    $mail->isHTML(true);  

    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );


    $pdf = "C:/Reports/" . $row['Name']. ".pdf";
    $mail->addAttachment($pdf);     


    if ($mail->send()) {
        echo "<script>alert('Mail Sent success');</script>";
    //  header("Location:index.php");
    }
    else {
        echo "<script>alert('Mailer Error: ' $mail->ErrorInfo);</script>";
    //  header("Location: index.php");
    } 
    $pdf = "";
    $mail->clearAttachments();
    } //endwhile

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM