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">
    
    点赞 评论

相关推荐 更多相似问题