PHPMailer 附件收不到
PHPMailer attachment is not receiving
我已经编写了以pdf文件作为附件发送邮件的代码,邮件发送正常。我用了class.phpmailer.php。下面是我的代码。
$mpdf=new mPDF();
$mpdf->ignore_invalid_utf8 = true;
$stylesheet = file_get_contents('appstyle_pdf.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($output);
$comname = preg_replace("/[^A-Za-z0-9]/","",$_POST['company']);
$name = $dirname.str_replace(" ","-",$comname)."_".$time_stamp.".pdf";
$mpdf->Output($name,"F");
$filename = basename($name);
$file_size = filesize($name);
$content = chunk_split(base64_encode(file_get_contents($name)));
$mail = new PHPMailer;
$msg = 'Message';
$body = '<html><body><p>' . $msg . '</p></body></html>'; //msg contents
$body = preg_replace("[\\]", '', $body);
$mail->AddReplyTo('no-replay@enkapps.com', "ACIC");
$mail->SetFrom('orders@enkapps.com', "ACIC Order");
$address = 'narendar_medoju@tecnics.com'; //email recipient
$mail->AddAddress($address, "NAME");
$mail->Subject = 'SUBJECT of ACIC order form';
$mail->MsgHTML($body);
$mail->AddStringAttachment($content , $filename);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent Successfully please check attachement!";
}
当我使用上面的代码时,附件会发送到邮件中,但文件已损坏。错误消息类似于 "Adobe reader could not open abc.pdf because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."
你为什么要这样做?
$content = chunk_split(base64_encode(file_get_contents($name)));
...
$mail->AddStringAttachment($content , $filename);
完全没有必要。只需这样做:
$mail->addAttachment($name);
我还怀疑您使用的是旧版本的 PHPMailer;从 github.
获取最新信息
我已经编写了以pdf文件作为附件发送邮件的代码,邮件发送正常。我用了class.phpmailer.php。下面是我的代码。
$mpdf=new mPDF();
$mpdf->ignore_invalid_utf8 = true;
$stylesheet = file_get_contents('appstyle_pdf.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($output);
$comname = preg_replace("/[^A-Za-z0-9]/","",$_POST['company']);
$name = $dirname.str_replace(" ","-",$comname)."_".$time_stamp.".pdf";
$mpdf->Output($name,"F");
$filename = basename($name);
$file_size = filesize($name);
$content = chunk_split(base64_encode(file_get_contents($name)));
$mail = new PHPMailer;
$msg = 'Message';
$body = '<html><body><p>' . $msg . '</p></body></html>'; //msg contents
$body = preg_replace("[\\]", '', $body);
$mail->AddReplyTo('no-replay@enkapps.com', "ACIC");
$mail->SetFrom('orders@enkapps.com', "ACIC Order");
$address = 'narendar_medoju@tecnics.com'; //email recipient
$mail->AddAddress($address, "NAME");
$mail->Subject = 'SUBJECT of ACIC order form';
$mail->MsgHTML($body);
$mail->AddStringAttachment($content , $filename);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent Successfully please check attachement!";
}
当我使用上面的代码时,附件会发送到邮件中,但文件已损坏。错误消息类似于 "Adobe reader could not open abc.pdf because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."
你为什么要这样做?
$content = chunk_split(base64_encode(file_get_contents($name)));
...
$mail->AddStringAttachment($content , $filename);
完全没有必要。只需这样做:
$mail->addAttachment($name);
我还怀疑您使用的是旧版本的 PHPMailer;从 github.
获取最新信息