【问题标题】:multiple attachments going with single mail from phpmailer多个附件与来自 phpmailer 的单个邮件一起使用
【发布时间】:2018-08-15 21:39:38
【问题描述】:

我正在发送带有工资单的工资单邮件作为 phpmailer 类的附件。问题是第一封邮件带有一个附件,但 sedonf 邮件带有第一个和第二个附件。 例如: 员工姓名的邮件:A 与 A.pdf 一起使用 员工姓名的邮件:B 与 A.pdf 和 B.pdf 一起使用

需要帮助。我的项目完成日期是明天,我陷入了最后一个问题。 这是我的代码:

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

?>

【问题讨论】:

标签: php mysql phpmailer


【解决方案1】:

$mail = 新的 PHPMailer; // 这应该在while里面,我想...

【讨论】:

    【解决方案2】:

    感谢@jonStirling 和@toor 的帮助。 其他求助者的完整工作代码:

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

    【讨论】:

      【解决方案3】:

      在循环内创建一个新实例会起作用,但它的效率非常低,并且意味着您不能使用 keepalive,这会对吞吐量产生巨大影响。

      您的代码基于the mailing list example provided with PHPMailer,它展示了如何最有效地发送,并阅读the docs on sending to lists。套用这个例子,它应该大致是这样的:

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

      不要忘记在其中添加一些错误检查,我还可以看到您使用的是旧版本的 PHPMailer - 所以获取最新版本,并将您的代码基于邮件列表示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-03
        • 2019-09-10
        • 2014-02-03
        相关资源
        最近更新 更多