我已经编写了使用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!";
}
当我使用上面的代码时,附件即将发送,但文件正在损坏。错误消息类似于&#34; Adobe读者无法打开abc.pdf,因为它不是受支持的文件类型,或者因为文件已损坏(例如,它是作为电子邮件附件发送的,并且不是&t; t正确解码)。&#34;
答案 0 :(得分:1)
你为什么这样做?
$content = chunk_split(base64_encode(file_get_contents($name)));
...
$mail->AddStringAttachment($content , $filename);
这完全没必要。就这样做:
$mail->addAttachment($name);
我还怀疑你使用的是旧版本的PHPMailer;从github获取最新信息。