Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer AddStringAttachment with PDF

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('[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.

like image 769
user2233333 Avatar asked Sep 11 '13 23:09

user2233333


3 Answers

No need to chunk_split base64encode the pdf string just do

$mail->addStringAttachment($pdfString, 'vouchers.pdf');
like image 124
webmaster Avatar answered Oct 21 '22 09:10

webmaster


Try

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

it will work

like image 9
user3840513 Avatar answered Oct 21 '22 10:10

user3840513


This worked fine for me:

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

like image 5
Hugo Caldeira Avatar answered Oct 21 '22 09:10

Hugo Caldeira