php - 而不是上传到文件夹,发送电子邮件与附加的文件上传

时间:2015-06-11 15:19:36

标签: php email attachment

<?php
    //if there is post
if(isset($_POST) && !empty($_POST)){
        //if there is attachment
        if(!empty($_FILES['attachment']['name'])){
            //store some variables
            $file_name = $_FILES['attachment']['name'];
            $temp_name = $_FILES['attachment']['tmp_name'];
            $file_type = $_FILES['attachment']['type'];

            //get the extension of the file
            $base = basename($file_name);
            $extension = substr($base, strlen($base)-4,strlen($base));

            //only these file type will be allowed
            $allow_extensions = array(".doc","docx",".pdf",".zip",".png");

            //check if file is allowes
            if(in_array($extension,$allow_extensions)){

                //mail essentials
                $from = $_POST['email'];
                $to = "sampleemail@gmail.com";
                $replyto = $to;
                $subject = "email with attachment";
                $message = "this is a random message";

                //things you need
                $file = $temp_name;
                $content = chunk_split(base64_encode(file_get_contents($file)));
                $uid = md5(uniqid(time()));

                //standard mail headers
                $header = "From " . $from . "\r\n";
                $header .= "Reply-To: " . $replyto . "\r\n";
                $header .= "MIME-Version: 1.0\r\n";

                //declairing we hav e multiple parts of message like text
                $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                $header .= "This is a multi-part message in MIME format. \r\n";

                //plain text part
                $header .= "--".$uid."\r\n";
                $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
                $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
                $header .= $message."\r\n\r\n";

                //file attachment
                $header .= "--".$uid."\r\n";
                $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
                $header .= "Content-Transfer-Encoding: base64\r\n";
                $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
                $header .= $content. "\r\n\r\n";

                //send mail 
                if(mail($to, $subject, "", $header)){
                    echo "Mail Sent";
                }
                else
                {
                    echo "Failed";
                }

            }
            else{
                echo "file type not allowed";
            }


        }
        else{
            echo "no file posted";
        }
    }
?>


<html>
    <head>

    </head>
    <body>
    <form method="post" action="index.php" enctype="multipart/form-data">
        <input type="text" name="email" value="from" />
        <br>
        <input type="file" name="attachment" />
        <br>
        <input type="submit" value="Send Mail" />
    </form>     
    </body>
</html>

无法找到我的代码中的错误,我没有收到我的附件链接.T_T请帮忙。我不擅长边界.T_T我尝试使用phpmailer但是不能让它工作,有没有关于如何设置它的文件?我真的只想制作一个带附加按钮的简单表格,让申请人发送简历..

1 个答案:

答案 0 :(得分:0)

$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";之后的所有内容都应该在$body中,而不是标题。

我会摆脱这个:$header .= "This is a multi-part message in MIME format. \r\n";

$content之后,您实际上需要在mime分隔符中添加另一行。

$body .= $content . "\r\n" . '--' . $uid . '--';

最后

if (mail($to, $subject, $body, $header)) {...