PHPMailer AddStringAttachment with PDF

2019-04-24 08:01发布

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

<?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())

;
?>

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.

2条回答
Fickle 薄情
2楼-- · 2019-04-24 08:42

No need to chunk_split base64encode the pdf string just do

$mail->addStringAttachment($pdfString, 'vouchers.pdf');
查看更多
Summer. ? 凉城
3楼-- · 2019-04-24 08:49

Try

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

it will work

查看更多
登录 后发表回答