欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-04-05 15:12:18
...
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."--";


推荐楼上的
$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();

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