将base64字符串转换为邮件附件pdf

base64 string into a mail attachment pdf

我有这段代码将 PDF 编码文件作为从另一个 window 生成的 Base64 URI 字符串,并声明为 php 会话变量,最后这是我的函数我正在编码并发送电子邮件,但 Base64 解码为大量字符,而不是正确完成的附件。

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; }
}

最后,我之前解码了我的 base64 pdf 文件并将解码后的字符串保存到一个变量中,并将该变量设置在附件属性中。

顺便说一句,我用过 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;
}
?>