php 发送带pdf附件的email 出现的是一长串字符串 请问如何解决

WBOY
Release: 2016-06-23 14:22:45
Original
1227 people have browsed it

php email pdf

RT,发出的email附件不是一个pdf档,而变成了一长串字符串,

我的发送email的代码如下:

function mail($mailto, $userid, $data){
    global $dsql,$cfg_adminemail,$cfg_webname,$cfg_basehost,$cfg_memberurl;
    $mailtitle = $cfg_webname.":通知";
$file=fopen("D:/xampp/htdocs/pdftemp/dsfasdf.pdf","r"); 
$fileName=$data['oid'].'.pdf';
$mimeType ="application/pdf"; 
$boundary = "nextpart_".uniqid("");  //分隔符1
$boundary2 = "nextpart2_".uniqid(""); //分隔符2

        //email头
$headers = "From: ".$cfg_adminemail."\r\nReply-To: $cfg_adminemail";
$headers .= "Content-type: multipart/mixed; boundary=\"$boundary1\"\r\n";

        //读取文件
        $read = fread($file, filesize("D:/xampp/htdocs/pdftemp/dsfasdf.pdf")); 
//我们用base64方法把它编码 
$read = base64_encode($read); 
//把这个长字符串切成由每行76个字符组成的小块 
$read = chunk_split($read);

        //以下是mailbody正文 
$mailbody = "--$boundary1----$boundary2 
Content-type: text/plain;charset=iso-8859-1;
Content-transfer-encoding: 8bit";
    $mailbody .= '致: '.$userid.',



This is mailbody--$boundary2--';
        
        //以下是附件部分
$mailbody .="--$boundary1 
Content-type: text/html; name=$fileName 
Content-disposition: inline; filename=$fileName 
Content-transfer-encoding: 8bit
$read 
--$boundary1--";


    $status = smail($mailto,$mailtitle,$mailbody,$headers);
    setMail($mailto, $mailtitle, $mailbody, $headers, $status);
    return $status;
}

回复讨论(解决方案)

你用phpmailer把

你的这个可能是没有指定attachment的类型?

// attachment$body .= "--".$separator.$eol;$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; $body .= "Content-Transfer-Encoding: base64".$eol;$body .= "Content-Disposition: attachment".$eol.$eol;$body .= $attachment.$eol;$body .= "--".$separator."--";
Copy after login


推荐楼上的
$email = new PHPMailer();$email->From      = 'you@example.com';$email->FromName  = 'Your Name';$email->Subject   = 'Message Subject';$email->Body      = $bodytext;$email->AddAddress( 'destinationaddress@example.com' );$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );return $email->Send();
Copy after login

可能是boundary设置有误,导致解析错误。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template