php PHPMailer AddStringAttachment with PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18753122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:09:28  来源:igfitidea点击:

PHPMailer AddStringAttachment with PDF

phppdfphpmaileremail-attachments

提问by user2233333

I am new to phpmailer and I am able to send emails, emails with attachments, and stringattachments that are .txt files however I cannot send stringattachments with PDF's. The email is sent, but the PDF is corrupted/unable to open. Can anyone help to send the AddStringAttachment with the attachment being a PDF rather than a .txt? Thanks

我是 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('[email protected]');
$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())

;
?>

Again, if I just change Filename.pdf to Filename.txt everything works so I'm assuming the problem is with the encoding but I can't figure it out. Please help so I can send stringattachment PDF's. Thanks.

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

回答by webmaster

No need to chunk_split base64encode the pdf string just do

无需对 pdf 字符串进行 chunk_split base64encode 就可以了

$mail->addStringAttachment($pdfString, 'vouchers.pdf');

回答by user3840513

Try

尝试

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

it will work

它会工作

回答by Hugo Caldeira

This worked fine for me:

这对我来说很好:

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

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