佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1506|回复: 2

PHP 发邮件,附件不能收到

[复制链接]
发表于 8-3-2015 10:49 AM | 显示全部楼层 |阅读模式
这个是我的员工写的,因为他是学生,搞了这个几天了,都搞不好,我又很急用。
有谁能帮忙看看那里错吗?帮我修改,先谢谢各位了
发邮件了,附件在c panel能看到,可是to:接收人。没收到附件

<?php
    //did file get sent
    if(isset($_FILES)&&(bool)$_FILES)
    {
        //define allowed extensions
        $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt";
        $files = array();
   
        //loop through all the files
        foreach($_FILES as $name=>$file)
        {
            //define some variables
            $file_name = $file['name'];
            $temp_name = $file['tmp_name'];
            $file_type = $file['type'];
               
            //check if this file type is allowed
            $path_parts = pathinfo($file_name);
            $ext = $path_parts['extension'];
            if(!in_array($ext,$allowedExtensions))
            {
                die("extension not allowed";
            }
        
            //move this file to the server YOU HAVE TO DO THIS
            $server_file = "/home/goo8/public_html/form/emailForm/upload/$path_parts[basename]";
            move_uploaded_file($temp_name,$server_file);
                        
            //add this files to the array of files
            array_push($files,$server_file);

        }
        
        //define some mail variables
        $to = "[email protected]";
        $from = "[email protected]";
        $subject = "test attachment";
        $msg = "here we go";
        $headers = "From: $from";
        
        //define our boundary
        $semi_rand = md5(time());
        $mime_boundry = "==Multipart_Boundry_x{$semi_rand}x";
        
        //tell the header about the boundry
        $header = "Reply To: ". $replyto."\r\n";
        $header .="MIME-Version: 1.0\r\n";
        $header .="Content-Type: multipart/mixed; boundary=$mime_boundary"."\r\n\r\n";
            
        //part 1: define the plain text email
        $message = "--".$mime_boundary."\n";
        $message .= "Content-Type: text/plain; charset=\iso-8859-1\r\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n".$msg."\n\n";
        $message .= "--{$mime_boundry}\n";
               
        //part 2: loop and define mail attachment
        foreach($files as $file)
        {
            $aFile = fopen($file,"r";
            $data = fread($aFile,filesize($file));
            fclose($aFile);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: ".$file_type."; name=".$file."\"\n";
            $message .= "Content-Disposition: attachment; filename=\\".$file."\"\n";
            $message .= "Content-Transfer-Encoding: base64\n\n".$data."\n\n";
            $message .= "--{$mime_boundary}\n";
   
        }
            
        //send the email
        $ok = mail($to,$subject,$message,$headers);
        if($ok)
        {
            echo "<p>mail sent to $to!</p>";
        }
        else
        {
            echo "<p>mail colud not be sent!</p>";
        }
        die();
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Form Attachment</title>
</head>

<body>

    <form method="post" action="test.php" enctype="multipart/form-data">
        <input type="file" name="attach1"><br />
        <input type="file" name="attach2"><br />
        <input type="submit" value="submit" />
    </form>

</body>
</html>
回复

使用道具 举报


ADVERTISEMENT

发表于 26-4-2015 08:16 PM | 显示全部楼层
本帖最后由 hooiyijun 于 26-4-2015 08:19 PM 编辑

Maybe some mail server will not accept such mail code. You should try to use phpmailer for sending mail with attachment. https://code.google.com/a/apache ... wiki/UsefulTutorial. If you tried phpmailer and cannot see it works, then real issue is your mail server removed attachment from mail.
回复

使用道具 举报

发表于 27-4-2015 08:54 PM | 显示全部楼层
要真真拿来用的 ,为何不用别人的 library ?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 19-12-2024 05:11 PM , Processed in 0.169187 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表