gpt4 book ai didi

javascript - 没有显示 Ajax 附件的文件数据

转载 作者:行者123 更新时间:2023-11-28 10:51:46 25 4
gpt4 key购买 nike

因此,我在尝试向自己发送带有附件的电子邮件时遇到了一些问题。我没有使用标准的 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错误。在表单上设置 enctype 会产生相同的错误。将其设置为 false 会发送消息,但我没有附件。

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

Array
(
[name] => 20150625_140017.jpg
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)

最佳答案

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

将提交者的地址设置为“发件人”地址将导致 SPF 检查失败 - 将您自己的地址放在“发件人”中,将提交者的地址放在回复中。

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

关于javascript - 没有显示 Ajax 附件的文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32099799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com