13

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.

user2233333
  • 183
  • 1
  • 1
  • 6

3 Answers3

16

No need to chunk_split base64encode the pdf string just do

$mail->addStringAttachment($pdfString, 'vouchers.pdf');
webmaster
  • 1,960
  • 24
  • 29
9

Try

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

it will work

Castro Roy
  • 7,623
  • 13
  • 63
  • 97
user3840513
  • 91
  • 1
  • 3
5

This worked fine for me:

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

Hugo Caldeira
  • 51
  • 1
  • 2