我发送带有工资单的payslip邮件作为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
?>
答案 0 :(得分:1)
在循环中创建一个新实例会起作用,但效率非常低,意味着你不能使用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 - 所以请获取最新版本,并将您的代码基于邮件列表示例。
答案 1 :(得分:0)
$ mail = new PHPMailer; //这应该在我的内心,我想......
答案 2 :(得分:0)
感谢@jonStirling和@toor的帮助。 其他求助者的完整工作代码:
Purchase