Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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_Jquery_Ajax_Phpmailer - Fatal编程技术网

Javascript 使用Ajax时没有显示附件的文件数据

Javascript 使用Ajax时没有显示附件的文件数据,javascript,php,jquery,ajax,phpmailer,Javascript,Php,Jquery,Ajax,Phpmailer,因此,当我试图给自己发送一封带有附件的电子邮件时,我遇到了一点问题。我没有使用标准的php邮件函数,我使用PHPMailer是为了方便。我正在通过一个Ajax调用运行这篇文章,我已经做了大量的研究,以确保它的格式正确 现在的情况是,当我提交表单时,所有数据都正确发送。我得到了图像名称,但没有其他与之相关的内容。我的内容类型设置正确,其他所有内容都设置正确,所以我只是对正在发生的事情感到困惑,需要一些了解 我所有的代码都在下面分段 html form index.php PHP callbacks

因此,当我试图给自己发送一封带有附件的电子邮件时,我遇到了一点问题。我没有使用标准的php邮件函数,我使用PHPMailer是为了方便。我正在通过一个Ajax调用运行这篇文章,我已经做了大量的研究,以确保它的格式正确

现在的情况是,当我提交表单时,所有数据都正确发送。我得到了图像名称,但没有其他与之相关的内容。我的内容类型设置正确,其他所有内容都设置正确,所以我只是对正在发生的事情感到困惑,需要一些了解

我所有的代码都在下面分段

html form index.php

PHP callbacks.PHP

我彻底地遵循了PHPMailer文档,并仔细检查了我在这里写的内容,但似乎没有什么能给我正确的解决方案

编辑:

设置内容类型:“multipart/form data”会提供一个未定义索引的PHP错误:callback。在表单上设置enctype会出现相同的错误。将其设置为false会发送消息,但我没有附件

另外,在查看devlooper工具时,当我检查response选项卡时,我在运行print\u r$\u FILES['images']时会得到这个结果

看一看。在PHP中,您缺少了一些处理文件上传的标准内容,但在该示例中,这是正确的

将提交人的地址设置为发件人地址将无法通过SPF检查-将您自己的地址输入发件人,并将提交人的地址作为回复


看起来您的代码基于一个过时的示例,因此请确保您也有最新的PHPMailer。

您使用的是有效的enctype吗?如果不是,您的意思是像多部分/表单数据一样吗?这不是在内容类型选项下设置的吗?此外,如果内容类型没有设置为false,那么这不会提升ajax的整个代码结构吗?很高兴再次见到您@Fred-ii-我正在尝试这个方法,内容类型为false,设置为multipart/form data,如果失败,我也将在form标记中设置enctype,看看会发生什么。请待命!这也有助于做好标记。我在问题中添加了您先前的评论,但无法进一步评论,因为我无法为您解决问题。我对这个问题投了赞成票,希望有人能理解。祝你好运,马克,我希望你能解决这个问题:干杯,我认为你没有完全阅读这个问题,也没有看上面的编辑。这与电子邮件实际发送的时间无关。那很好用。我还使用了最新版本的PHPMailer。发生的情况是,文件数据(如类型、大小和tmp_名称)没有发送到服务器,它们是空的。我想知道这是怎么发生的,为什么会发生。然后你需要把你的问题分开,把不起作用的部分分离出来。PHPMailer可以为您设置类型和大小,但在$\u文件中设置值的是PHP本身,而不是您的客户机。我所说的一切都是真的。虽然这可能是真的,但我的版本仍然有效,也不是问题所在。事实证明,我需要一个不同的托管计划,允许文件大小大于500KB。fml
<form id='requestArtForm' action='' method='post'>
    <div class='row'>
        <section class='form-group col-sm-6 col-xs-12'>
            <label class='control-label req'>Name</label>
            <input type='text' class='form-control' id='name' data-validator='notEmpty'/>
        </section>
        <section class='clearfix'></section>
        <section class='form-group col-sm-6 col-xs-12'>
            <label class='control-label req'>E-Mail Address</label>
            <input type='email' class='form-control' id='email_address' data-validator='notEmpty|isEmail' />
        </section>
        <section class='clearfix'></section>
        <section class='form-group col-sm-6 col-xs-12'>
            <label class='control-label'>Phone Number</label>
            <input type='tel' class='form-control' id='phone' data-validator='isPhoneNumber' />
        </section>
        <section class='clearfix'></section>
        <section class='form-group col-sm-6 col-xs-12'>
            <label class='control-label'>Upload an Image</label>
            <input type='file' name='images[]' multiple/>
        </section>
        <section class='clearfix'></section>
        <section class='form-group col-xs-12'>
            <label class='control-label req'>Additional Comments</label>
            <textarea class='form-control' rows='5' id='comments' data-validator='notEmpty'></textarea>
        </section>
        <section class='form-group col-xs-12'>
            <button type='submit' class='btn btn-primary pull-right'>Send Message</button>
        </section>
    </div>
</form>
$('#requestArtForm').submit(function(e){
    e.preventDefault();
    $('body').spin('large');
    var formData = new FormData();
    formData.append('callback', 'requestDrawing');
    formData.append('parameters[]', $('#requestArtForm #name').val());
    formData.append('parameters[]', $('#requestArtForm #email_address').val());
    formData.append('parameters[]', $('#requestArtForm #phone').val());
    formData.append('parameters[]', $('#requestArtForm #comments').val());
    $.each($('[name="images[]"]')[0].files, function(i, file){
        formData.append('images[]', file);
    });
    if(validator($(this))){
        $.ajax({
            url : "<?=APP_BASE?>assets/server/callbacks.php",
            type : 'POST',
            data : formData,
            dataType : 'JSON',
            contentType : 'multipart/form-data',
            processData : false,
            success : function(data){
                $('body').spin(false);
                if(!data.errors){
                    }else{
                    }
            }
        });
    }else{
        $('body').spin(false);
    }
});
$enabledFunctions = array();
$MAIL = new PHPMailer();

function enableFunction($function){
    global $enabledFunctions;
    array_push($enabledFunctions, $function);
}

function requestDrawing($name, $email_address, $phone, $comments){
    global $MAIL;
    $response = array();

    if(empty($name) or empty($email_address)){
        $response['errors'] = true;
        $response['message'] = "Please make sure all the required fields are filled out. Fields marked with an asterisk are required.";
    }else{

        $body = "You have a new message from $name\n\n";
        $body .= "Name: $name\n";
        $body .= "Email Address: $email_address\n";
        $body .= "Phone Number: $phone\n";
        $body .= "Comments: $comments\n\n";

        $MAIL->From = "$email_address";
        $MAIL->FromName = "$name";
        $MAIL->AddAddress("mark@neartist.com", "Mark Hill");
        $MAIL->AddReplyTo("$email_address", "$name");
        if (isset($_FILES['images']) && $_FILES['images']['error'] == UPLOAD_ERR_OK) {
            $MAIL->AddAttachment($_FILES['images']['tmp_name'], $_FILES['images']['name']);
        }
        $MAIL->Subject = "New Message from $name";
        $MAIL->Body = $body;

        if(!$MAIL->Send()){
            $response['errors'] = true;
            $response['success'] = "There was an error when trying to send you message, please try again later.";
        }else{
            $response['success'] = "Your message has been sent!";
        }
    }

    return $response;
}
enableFunction('requestDrawing');

$function = $_POST['callback'];
$parameters = isset($_POST['parameters']) ? array_values($_POST['parameters']) : "";
if(empty($parameters) && in_array($function, $enabledFunctions)){
    echo json_encode(call_user_func($function));
}elseif(in_array($function, $enabledFunctions)){
    echo json_encode(call_user_func_array($function, $parameters));
}
Array
(
    [name] => 20150625_140017.jpg
    [type] => 
    [tmp_name] => 
    [error] => 1
    [size] => 0
)