将Base64字符串转换为邮件附件PDF

3

我有这段代码,它将以Base64 URI字符串编码的PDF文件作为PHP会话变量从另一个窗口生成,最终这是我正在编写的函数,它发送电子邮件,但是附件被解码为大量字符,而不是正确完成的附件。

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; }
}
1个回答

5

最后,我之前解码了我的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;
}
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接