9

我是 phpmailer 的新手,我可以发送电子邮件、带有附件的电子邮件和 .txt 文件的字符串附件,但是我不能发送带有 PDF 的字符串附件。电子邮件已发送,但 PDF 已损坏/无法打开。任何人都可以帮助发送附件是 PDF 而不是 .txt 的 AddStringAttachment 吗?谢谢

<?php

require_once('class.phpmailer.php');
$mail = new PHPMailer();

$body2 = "You are receiving this email because the Transfer Application submitted for $Name transferring to $Receiving 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.

    <p> Details:
        $Notes ";

$mail->IsSMTP();
$mail->AddAddress($emailp);
$mail->AddCC('transfers@uhsaa.org');
$mail->AddStringAttachment($body2, 'Filename.pdf', 'base64', 'application/octet-stream');

$mail->Subject = "Test";
$body = ("See attachment");
$mail->MsgHTML($body);
$mail->AddAddress($address);
$mail->AddCC($address);
if(!$mail->Send())

;
?>

同样,如果我只是将 Filename.pdf 更改为 Filename.txt 一切正常,所以我假设问题出在编码上,但我无法弄清楚。请帮忙,以便我可以发送字符串附件 PDF。谢谢。

4

3 回答 3

12

无需对 pdf 字符串进行 chunk_split base64encode 即可

$mail->addStringAttachment($pdfString, 'vouchers.pdf');
于 2014-04-22T21:17:52.770 回答
8

尝试

$mail->AddStringAttachment($body2, 'Filename.pdf', 'base64', 'application/pdf');

它会起作用的

于 2014-04-22T13:31:35.377 回答
4

这对我来说很好:

$mail->addStringAttachment(base64_decode($attachmentBase64), $filename);

于 2020-02-13T10:44:23.553 回答