没有显示附件与Ajax的文件数据

时间:2015-08-19 15:29:25

标签: javascript php jquery ajax phpmailer

因此,在尝试使用附件向自己发送电子邮件时,我遇到了一些问题。我没有使用标准的php邮件功能,因为方便,我使用的是PHPMailer。我通过Ajax调用运行该帖子,并且我已经进行了大量研究以确保其格式正确。

发生的事情是,当我提交表单时,所有数据都会正确发送。我得到的是图像名称,但没有其他内容。我的内容类型设置正确,其他所有内容都设置正确,所以我只是对正在发生的事情感到困惑,并且可以使用一点点洞察力。

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

html表单 index.php

<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>

javascript main_scripts.php

$('#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);
    }
});

PHP callbacks.php

$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));
}

我彻底关注了PHPMailer文档并仔细检查了我在这里写的内容,但似乎没有给我正确的解决方案。

修改

设置content-Type : 'multipart/form-data'会导致PHP错误Undefined index: callback。在表单上设置enctype会提供相同的错误。将其设置为false会发送消息,但我没有附件。

另外,在查看devloper工具时,当我检查响应选项卡时,我会在运行print_r($_FILES['images']

时得到此信息
Array
(
    [name] => 20150625_140017.jpg
    [type] => 
    [tmp_name] => 
    [error] => 1
    [size] => 0
)

1 个答案:

答案 0 :(得分:-1)

看看the example provided with PHPMailer。您在PHP中缺少处理文件上传的标准内容,但在该示例中已正确完成。

将提交者的地址设置为发件人地址将无法进行SPF检查 - 将您自己的地址放在From和提交者的回复中。

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