我曾使用phpmailer发送电子邮件。现在我必须在电子邮件中附上pdf文件。它附上了pdf,但pdf无法打开。并且表明格式存在问题。 AddStringAttachment不适用于pdf吗?我该怎么办?
require_once('././php_mailer/class.phpmailer.php');
$this->load->helper('mail_html');
$body2 = "You are receiving this email because the Transfer Application submitted for transferring to g is missing required documentation.
Please see the note below for details. The transfer application will not be reviewed until all of the necessary materials are received by the UHSAA.
";
$body = 'test';
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = 'smtp.gmail.com'; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com'; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'nabina.smartmobe@gmail.com'; // SMTP account username
$mail->Password = '*******'; // SMTP account password
$mail->AddAddress('nabina.shahi@gmail.com','nabina');
$mail->SetFrom('no-reply@nayacinema.com.np', 'no-reply@nayacinema.com.np');
$mail->AddReplyTo('no-reply@nayacinema.com.np', '');
$mail->IsHTML(true);
$mail->Subject ='NayaCinema: Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->AddStringAttachment($body2, 'Filename.pdf', 'base64', 'application/pdf');
if($mail->Send()){
echo 'sent'; die;
return true;
}else{
echo ' not sent'; die;
return false;
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
谢谢
答案 0 :(得分:1)
您可以使用另一个PHP类TCPDF创建.pdf。您可以在示例中看到它是如何发生的。如果您不想在服务器中保存PDF,您可以像您一样发送,只需要更改TCPDF $ pdf-> ...代码的输出行,如下所示:
$attachdata = $pdf->Output('foo.pdf','S'); // return the document as a string (name is ignored)
然后你可以发送它:
$mail->AddStringAttachment($attachdata, 'Filename.pdf');