php 发送带pdf附件的email 出现的是一长串字符串 请问如何解决
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;
}
php email pdf 分享到:
------解决方案--------------------
你用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设置有误,导致解析错误。