Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发送带有多个附件和JavaScript Ajax请求的电子邮件_Javascript_Php_Html_Ajax - Fatal编程技术网

发送带有多个附件和JavaScript Ajax请求的电子邮件

发送带有多个附件和JavaScript Ajax请求的电子邮件,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我有一个弹出式表单,提交后会发送电子邮件。一切都很好。我正在尝试为要添加的文档添加3个上载文件字段。我尝试使用input type=file,但没有成功。我已经按照要求用我的全部代码编辑了这个问题。我在底部添加了发送邮件的代码 这个表格很好用。我只是缺少这3个文件上传字段 我的javascript: <script language="javascript"> function validate(){ var f=document.formED; if(f.idnum

我有一个弹出式表单,提交后会发送电子邮件。一切都很好。我正在尝试为要添加的文档添加3个上载文件字段。我尝试使用input type=file,但没有成功。我已经按照要求用我的全部代码编辑了这个问题。我在底部添加了发送邮件的代码

这个表格很好用。我只是缺少这3个文件上传字段

我的javascript:

<script language="javascript">
function validate(){
    var f=document.formED;
    if(f.idnumber.value==''){
        alert('ID Number is required');
        f.idnumber.focus();
        return false;
    }
    /** doing the same for each field => remove here **/

    //f.command.value='btn1';
    //f.submit();

}
    function showHide(){
    var f=document.formED;
    if(f.fullnames.value==''){
        f.fullnames.focus();
        return false;
    }
    /** doing the same for each field => remove here **/

    //create an object reference to the div containing images 
    var oimageDiv=document.getElementById('searchingimageDiv') 
    //set display to inline if currently none, otherwise to none 
    oimageDiv.style.display=(oimageDiv.style.display=='none')?'inline':'none' 
    }
</script>
<div class="col-md-8">
    <div class="panel panel-white">
        <!-- Removed the irrelevant HTML content for this question -->
        <div class="panel-body">
            <form onsubmit="return validate()" autocomplete="off" name="formED" action="#" method="post" role="form">
                <!-- Removed the irrelevant HTML content for this question -->

                <div class="form-group">
                    <label for="exampleInputEmail1">License Disk Copy:(Please press control while selecting to choose
                        multiple images)</label>
                    <input type="file" name="multiple_files[1]" multiple="multiple">

                </div>

                <div class="form-group">
                    <label for="exampleInputEmail1">Picture Of Damage:(Please press control while selecting to choose
                        multiple images)</label>
                    <input type="file" name="multiple_files[2]" multiple="multiple">
                </div>

                <div class="form-group">
                    <label for="exampleInputEmail1">License Copy: (Please press control while selecting to choose
                        multiple images)</label>
                    <input type="file" name="multiple_files[3]" multiple="multiple">
                </div>

                <!-- Removed the irrelevant HTML content for this question -->
                <button type="submit" name="newClient" class="btn btn-info" onclick="showHide()">Submit <i
                            class="fa fa-check" aria-hidden="true"></i></button>
            </form>
        </div>
    </div>
</div>
发送邮件的php:

<?php
$to = 'info@****';
$subject = "New Assessment Request has been submitted by ".$fullnames;
// Get HTML contents from file
$htmlContent = '<div style="font-family:HelveticaNeue-Light,Arial,sans-
serif;background-color:#FFFFFF"><!-- Removed the irrelevant HTML content for this question -->
</div>';

// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Additional headers
$headers .= 'From: Digiteks Assessment Request<info@****>' . "\r\n";

// Send email
if(mail($to,$subject,$htmlContent,$headers)):

else:
//$errorMsg = 'We are unable to send you a mail, possible your email is a scam..';
endif;  
?>          
通常需要将enctype=multipart/form数据添加到from字段中。如果向输入字段添加多个属性,则用户可以一次选择多个文件,或者使用3个单个输入字段进行选择。 type=文件是否正确:

<form method="POST" action="/upload" accept-charset="UTF-8" enctype="multipart/form-data">
    <input type="file" name="multiple_files[]" multiple="multiple">
    <input type="file" name="single_file1">
    <input type="file" name="single_file2">
    <input type="file" name="single_file3">
    <input value="Upload" type="submit">
</form>
如果不使用Ajax,代码中就会缺少enctype=multipart/form数据部分。字段名称也有语法错误。它是多个_文件1[]或多个_文件[1][] 没有多个_文件[1]

在邮件功能中,您不处理文件,因此即使其余部分正确,您也根本不发送文件,而使用邮件很难做到这一点

从此处尝试PHPMailer脚本: 但是如果你真的想用邮件来做这件事,这里有一个你如何做的教程:


因为您没有共享Ajax代码,例如jQuery Ajax,所以我无法检查您的JavaScript代码中是否有错误。

在处理了一段时间的标题和MIME后,我让它开始工作

<?php

 error_reporting(E_ALL);
 ini_set('display_errors', 1);

if(isset($_FILES) && (bool) $_FILES) {

$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");

$files = array();
foreach($_FILES as $name=>$file) {
    $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)) {
        die("File $file_name has the extensions $ext which is not allowed");
    }
    array_push($files,$file);
}

// email fields: to, from, subject, and so on
$to = "***";
$from = "***"; 
$subject ="New Assessment Request";
$message = "Test Message";

$headers = "From: $from";

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

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

// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";

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

    $ok = mail($to, $subject, $message, $headers); 
if ($ok) { 
     echo '<script language="javascript">';
     echo 'alert("Your Message successfully sent, we will get back to you ASAP.");';
     echo 'window.location.href="index.php";';
     echo '</script>';

} else { 
    echo "<p>mail could not be sent!</p>"; 
} 
}   

?>

这是Ajax,先生,很抱歉,好的,先生,这就是我之前的PHP代码错误。对不起,我没有看到你用邮件将表单发送到服务器的部分。这将是一项艰巨的工作。试试看我的答案。