网上扒了一个加附件发送邮件的方法代码?反复修改,总是发送失败,贴上来请大家研究研究??<html>
<body>
<form method="post" name="sndml" action="2.php" ENCTYPE="multipart/form-data">
<table>
<tr ><td>邮件标题:</td>
<td><input type="text" name="subject" ></td>
</tr>
<tr ><td>发送者:</td>
<td><input type="text" name="from" ></td>
</tr>
<tr ><td>接受者:</td>
<td><input type="text" name="to" ></td>
</tr>
<tr ><td>下载提示:</td>
<td><input type="text" name="text" ></td>
</tr>
<tr ><td>源数据文件:</td>
<td><input type="file" name="upload_file" size="40"></td>
</tr>
<tr><td>&nbsp</td>
<td><input type="submit" value="确定">
</td>
</tr>
</table>
</form>
</body>
</html> 
<?php
//文本内容
$text = $_POST['text'];
//标题
$subject = $_POST['subject'];
//发送者
$from = $_POST['from'];
//接受者
$to = $_POST['to'];//附件
$file = $_FILES['upload_file']['tmp_name'];
// 定义分界线
$boundary = uniqid( "");
$headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
$headers .= "From:$from\r\n";
//确定上传文件的MIME类型
if($_FILES['upload_file']['type'])
$mimeType = $_FILES['upload_file']['type'];
else
$mimeType ="application/unknown";
//文件名
$fileName = $_FILES['upload_file']['name'];// 打开文件
$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.   

    你的boundary都一样,所以失败了。
    body一个boundary,附件一个boundary,要区别开。
      

  2.   

    $pdfname="test.pdf";
    $email="[email protected]";
    $text = "您好,附件中是您需要的PDF文件。请点击下载。<br><a href=http://www.singlestudio.net.cn>www.singlestudio.net.cn</a>";  //文本内容 $text = base64_encode($text);  //用base64方法把它编码 
    $text = chunk_split($text);     //把这个长字符串切成由每行76个字符组成的小块 $subject = $pdfname;  //标题 
    $from = "[email protected]";  //发送者 
    $to = $email;  //接受者 //附件 
    // 定义分界线 
    $boundary = "NextPart_".uniqid(""); 
    $boundary2 = "NextPart_".uniqid(""); 
    $headers = "To: $to\r\n";
    $headers .= "From: $from\r\n"; 
    $headers .="Mime-Version: 1.0\r\n";
    $headers .= "Content-type: multipart/mixed; 
                boundary=\"----=_$boundary\"\r\n"; $read=file_get_contents($pdfname);$read = base64_encode($read);  //用base64方法把它编码 
    $read = chunk_split($read);     //把这个长字符串切成由每行76个字符组成的小块 //现在我们可以建立邮件的主体 
    $body = "This is a multi-part message in MIME format.------=_$boundary
    Content-Type: multipart/alternative;
    boundary=\"----=_$boundary2\";------=_$boundary2
    Content-Type: text/html;
    charset=\"gbk\"
    Content-Transfer-Encoding: base64$text------=_$boundary2--------=_$boundary
    Content-Type: application/octet-stream;
    charset=\"GBK\";
    name=\"$pdfname\"
    Content-Disposition: attachment; filename=\"$pdfname\"
    Content-Transfer-Encoding: base64$read-------=_$boundary--"; if(mail($to, $subject,$body,$headers)) 
       echo "您需要的PDF文件(".$pdfname.")已经发往您的邮箱:".$to."。<br>请查收。"; 
    else 
       echo "抱歉,发送失败了。<br>"; 
      

  3.   

    回复2楼:我照着您的方式写了一遍,还是发送不出去
    HTML 还是和上面的一样,但是还是发送失败?,麻烦了.....<?php
    $pdfname = $_POST['upload_file']; 
    $email= $_POST['to'];
    $text = $_POST['text']; //文本内容 $text = base64_encode($text); //用base64方法把它编码 
    $text = chunk_split($text); //把这个长字符串切成由每行76个字符组成的小块 $subject = $_POST['subject'];         //标题 
    $from = $_POST['from'];     //发送者
    $to = $email; //接受者 //附件 // 定义分界线
    $boundary = "NextPart_".uniqid(""); 
    $boundary2 = "NextPart_".uniqid(""); 
    $headers = "To: $to\r\n"; 
    $headers .= "From: $from\r\n"; 
    $headers .="Mime-Version: 1.0\r\n"; 
    $headers .= "Content-type: multipart/mixed;
                boundary=\"----=_$boundary\"\r\n"; $read=file_get_contents($pdfname); $read = base64_encode($read); //用base64方法把它编码 
    $read = chunk_split($read); //把这个长字符串切成由每行76个字符组成的小块 //现在我们可以建立邮件的主体 
    $body = "This is a multi-part message in MIME format.
    ------=_$boundary 
    Content-Type: multipart/alternative;
    boundary=\"----=_$boundary2\"; 
    ------=_$boundary2 
    Content-Type: text/html; 
    charset=\"gbk\" 
    Content-Transfer-Encoding: base64
    $text
    ------=_$boundary2-- 
    ------=_$boundary 
    Content-Type: application/octet-stream; 
    charset=\"GBK\";
    name=\"$pdfname\" 
    Content-Disposition: attachment; filename=\"$pdfname\" 
    Content-Transfer-Encoding: base64 $read -------=_$boundary--"; if(mail($to,$subject,$body,$headers)) 
    echo "您需要的PDF文件(".$pdfname.")已经发往您的邮箱:".$to."。<br>请查收。"; 
    else
    echo "抱歉,发送失败了。<br>";
    ?> 
      

  4.   

    是什么系统?linux下是否安装了sendmail?
    windows下IIS里是否安装了SMTP?PHP.INI里是否设置了相关数据?
    如果你mail之后直接得到发送失败,就是设置问题了。