// 打开文件 $fp = fopen($file, "r"); // 把整个文件读入一个变量 $read = fread($fp, filesize($file)); //我们用base64方法把它编码 $read = base64_encode($read); //把这个长字符串切成由每行76个字符组成的小块 $read = chunk_split($read); //现在我们可以建立邮件的主体 $body = "--$boundary Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 8bit $text --$boundary Content-type: $mimeType; name=$fileName Content-disposition: attachment; filename=$fileName Content-transfer-encoding: base64 $read --$boundary--"; //发送邮件 if(mail($to, $subject,$body,$headers)) print "OK! the mail $from --- $to has been send<br>"; else print "fail to send mail <br>"; ?> 看不明白没关系,我来说明一下: 1,邮件头的构造 :一般包括 内容类型(Content-type)要发送附件,设置为 multipart/mixed 意思是多个部分 (邮件本身+附件)。 boundary ,就是上面提到的分界线,他的值用php自带的 uniqid();函数取得 接受方,抄送等,在后面加上 From: Cc:。与上面的 Content-type boundary 之间用 \r\n 分割 。 2 邮件体 如果是纯文本的邮件内容 它的格式如下: Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 8bit 后面再紧接着加上 邮件的文本内容。 如果是附件: Content-type: $mimeType; name=$fileName
(编辑:aniston)
|