donglang9880 2015-08-11 11:06
浏览 44
已采纳

HTML php联系表 - 附件不可用

I am working with the following codeto send email to others with attachment.

html code

    <!DOCTYPE html>
    <html>
    <head>
    <link rel='stylesheet' type='text/css' href='sendEmail.css'/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

     <style type="text/css">
    .contactform
    {
        font-family: Verdana, Arial, sans-serif;
        width:550px;
    }
    .contactform label
    {
        float: left;
        text-align: right;
        margin-right: 15px;
        width: 200px;
        padding-top: 5px;
    }
    .contactform input{
        width:300px;
        padding: 5px;
        font-family: Helvetica, sans-serif;
        margin: 0px 0px 10px 0px;
        border: 2px solid #ccc;
    }
    .contactform textarea {
        vertical-align: bottom;
        width: 310px;
        height:150px;
        color: #777;
    }
    .submit-btn input
    {
        width:50px;
        float: left;
        text-align: right;
        margin-left: 480px;
        padding-top: 5px;
    }
    </style>

    <script type='text/javascript'>
    function validateForm()
    {
        var validation = true;
       if(!validateEmail(document.contactform.semail.value))
       {
         alert( "invalide sender email!" );
         $("#semail").css("border-color", "red");
         validation=false;
       }
       if(!validateEmail(document.contactform.remail.value))
       {
         alert( "invalide recipient email" );
         $("#remail").css("border-color", "red");
         validation=false;
       }
       return validation;
    }
    function validateEmail(email) { 
        var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        return re.test(email);
    }
    </script>
    </head>
    <body>
        <form name="contactform" class="contactform" method="post" onsubmit="return validateForm()"  action="sendEmail.php">
            <label>Sender Email:</label>
            <input type="text" id="semail" name="semail" />
            <label>Recipient Email:</label>
            <input type="text" id="remail" name="remail" value="" />
            <label for="Subject">Subject:</label>
            <input type="text" name="Subject" id="Subject" />
            <label for="Attachment">Attachment:</label>
            <input type="file" name="attach1" id="attach1" />
            <label for="Message">Message:</label><br />
            <textarea name="Message" rows="20" cols="20" id="Message"></textarea>
            <div class="submit-btn">
            <input type="submit"/>
            </div>
        </form>
    </body>
    </html>

php code for sending mail with attachment.

    <?php
    ini_set('display_errors', 1);
    $senderEmail = filterInput($_POST['semail']);
    $recipientEmail = filterInput($_POST['remail']); 
    $message = filterInput($_POST['Message']);
    $subject = filterInput($_POST['Subject']);

    echo $senderEmail.$recipientEmail.$message.$subject;
    if(sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message))
    {
        echo "Email sent successfully!";
    }
    else
    {
        echo "Failed to send the email...";
    }

    function filterInput($data)
    {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
    }
    function sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message){


        if(isset($_FILES)) {
    echo "iamhere";
            $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","JPG","png","PNG","rtf","txt","xml");

            $files = array();
            foreach($_FILES as $name=>$file) {
                //die("file size: ".$file['size']);
                if($file['size']>=5242880)//5mb
                {
                    $fileSize=$file['size'];
                    return false;
                }
                $file_name = $file['name']; 
                $temp_name = $file['tmp_name'];
                $file_type = $file['type'];
                $path_parts = pathinfo($file_name);
                $ext = $path_parts['extension'];
                if(!in_array($ext,$allowedExtensions)) {
                    return false;
                    die("File $file_name has the extensions $ext which is not allowed");
                }

                //move the file to the server, cannot be skipped
                $server_file="/tmp/$path_parts[basename]";
                move_uploaded_file($temp_name,$server_file);
                array_push($files,$server_file);
                //array_push($files,$temp_name);
            }

            // email fields
            $headers = "From: $senderEmail";


            // boundary 
            $semi_rand = md5(time()); 
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

            // headers for attachment 
            $headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$mime_boundary}\""; 

            // multipart boundary 
            $message = "This is a multi-part message in MIME format.

" . "--{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" . "Content-Transfer-Encoding: 7bit

" . $message . "

"; 
            $message .= "--{$mime_boundary}
";

            // preparing attachments
            for($x=0;$x<count($files);$x++){
                $file = fopen($files[$x],"rb");
                $data = fread($file,filesize($files[$x]));
                fclose($file);
                $data = chunk_split(base64_encode($data));
                $message .= "Content-Type: {\"application/octet-stream\"};
" . " name=\"$files[$x]\"
" . 
                "Content-Disposition: attachment;
" . " filename=\"$files[$x]\"
" . 
                "Content-Transfer-Encoding: base64

" . $data . "

";
                $message .= "--{$mime_boundary}
";
            }

            // send
            return @mail($recipientEmail, $subject, $message, $headers);

        }
    }   

Email is being send without issues, but unable to get the attachemnt in that email.

can anyone give me a right way in this?

  • 写回答

1条回答 默认 最新

  • dongpai6567 2015-08-11 11:12
    关注

    You are missing enctype.

    Add enctype="multipart/form-data" to your form attribute

    <form name="contactform" class="contactform" enctype="multipart/form-data" method="post" onsubmit="return validateForm()"  action="sendEmail.php">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器