Santiago De Pol
Santiago De Pol

Reputation: 101

base64 string into a mail attachment pdf

i have this code that takes a PDF encoded file as a Base64 URI string that is generated from another window and is declared as a php session variable, finally this is the function that i'm coding and it sends the email but with the Base64 decoded as tons of characters, not as a attachment properly done.

function MailWithAttachment($to, $subject, $message, $senderMail, $senderName, $files){

$from = $senderName." <".$senderMail.">"; 
$headers = "From: $from";$headers = "MIME-Version: 1.0$newline".
       "Content-Type: application/pdf;".
       "boundary = \"$boundary\"$newline$newline".
       "--$boundary$newline".
       "Content-Type: application/pdf; charset=ISO-8859-1$newline; Content-Disposition: attachment;".
       "Content-Transfer-Encoding: base64$newline$newline"; 
echo "<script>alert('$files')</script>";
$headers .= rtrim(chunk_split($files));
$returnpath = "-f" . $senderMail;
//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath); 
if($mail){ return TRUE; } else { return FALSE; }
}

Upvotes: 4

Views: 6136

Answers (1)

Santiago De Pol
Santiago De Pol

Reputation: 101

Finally, i previously decoded my base64 pdf file and saved the decoded string into a variable, and setted that variable in the attachment atribute.

BTW, i've used PHP Mailer

<?php
$_SESSION["sesDataPDF"] = $_POST["hdn_UriPdf"];
$b64file = $_SESSION["sesDataPDF"];
$decodPdf;
if ($b64file){
    $nomPdf = ;
    $nomPdf .= ".pdf";
    $nomPdf = (string)$nomPdf;
    $b64file= trim(str_replace('data:application/pdf;base64,', "", $b64file) );
    $b64file= str_replace( ' ', '+', $b64file);
    $decodPdf= base64_decode($b64file);
    file_put_contents($nomPdf, $decodPdf);
    $mail = new PHPMailer;
    $mail->isSendmail();
    $mail->setFrom('', '');
    $mail->addAddress($mail, $name);
    //Set the subject line
    $mail->Subject = '';
    $mail->Body = '';
    $mail->AltBody = '';
    $mail->addAttachment($nomPdf);
    $mail->Timeout=60;
}
?>

Upvotes: 6

Related Questions