【问题标题】:PHPMailer Ajax AddAttachment not workingPHPMailer Ajax AddAttachment 不起作用
【发布时间】:2018-07-22 11:03:41
【问题描述】:

我有一个带有输入类型文件的 HTML 表单,我希望将提交的文件附在发送的电子邮件中。

事实上,我没有收到文件,但所有其他信息,如姓名、电子邮件地址、电话号码,都快来了。

这是我的 HTML(仅使用输入文件进行了简化):

<form enctype="multipart/form-data" id="contact-form-cv" name="contact-form-cv" method="POST" data-name="Contact Form CV">

<div class="form-group">

    <div class="controls">

        <!-- FILE -->
        <input type="hidden" name="MAX_FILE_SIZE" value="300000">
        <input type="file" name="cv-file" id="file" class="input-file form-control special-form my-file">
          <label for="file" class="btn btn-tertiary js-labelFile">

            <span class="js-fileName"><i class="fa fa-upload"></i>&nbsp; Attach CV*</span>
          </label>

        <!-- Button -->
        <button id="cv-valid-form" type="submit" class="btn btn-lg submit">Submit</button>

    </div>

</div>

JS

我有一个 JS 文件,用于在用户填写表单时显示警报消息:

$("#contact-form-cv [type='submit']").click(function(e) {
e.preventDefault();

// Get input field values of the contact form
var cvuser_file       = $('input[name=cv-file]').val();

// Datadata to be sent to server
post_data = {'cvuserFile':cvuser_file};

// Ajax post data to server
$.post('../contact-me-cv.php', post_data, function(response){  

    // Load json data from server and output message    
    if(response.type == 'error') {

        ...

    } else {

        ...

    }

}, 'json');

});

PHP

<?php

// Use PHP To Detect An Ajax Request
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    // Exit script for the JSON data
    $output = json_encode(
    array(
        'type'=> 'error',
        'text' => 'Request must come from Ajax'
    ));

    die($output);
}

if(empty($_POST["cvuserFile"])) {
    $output = json_encode(array('type'=>'error', 'text' => '<i class="icon ion-close-round"></i> Please attach your CV'));
    die($output);
}

$path = 'upload/' . $_FILES["cvuserFile"]["name"];
move_uploaded_file($_FILES["cvuserFile"]["tmp_name"], $path);

require 'php/class/class.phpmailer.php';

$mail = new PHPMailer();

//Set PHPMailer to use SMTP.
$mail->IsSMTP();       
//Set SMTP host name                          
$mail->Host = 'smtp.gmail.com';                           
//Set TCP port to connect to 
$mail->Port = '587';
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;

$mail->isHTML(true);

//Provide username and password yo your google account   
$mail->Username = "*****@gmail.com";                 
$mail->Password = "*******";   

$mail->WordWrap = 50;

$mail->From = $_POST["cvuserEmail"];
$mail->FromName = $_POST["cvuserName"];
$mail->setFrom('*****', '**** ****');
$mail->addAddress('*****', 'John Doe');
//Set the subject line

$mail->AddAttachment($path);

$mail->Subject = 'New message from my website!';

$mail->Body = 'Hello' . "\r\n" ;

if(!$mail->send()) 
{
    $output = json_encode(array('type'=>'error', 'text' => '<i class="icon ion-close-round"></i> Oops! Looks like something went wrong, please check your PHP mail configuration.'));
    die($output);
    unlink($path);
} 
else 
{
    $output = json_encode(array('type'=>'message', 'text' => '<i class="icon ion-checkmark-round"></i> Hello '.$_POST["cvuserName"] .', Your message has been sent, we will get back to you asap !'));
    die($output);
}
?>

在没有 AJAX 的情况下使用表单工作顺利,但不像上面那样使用它。

【问题讨论】:

    标签: php ajax file input phpmailer


    【解决方案1】:

    您不能像现在这样通过 ajax 上传文件。当您执行$('input[name=cv-file]').val() 时,您将获得文件名,但文件未上传。

    查看此帖子以获取有效方法:Sending multipart/formdata with jQuery.ajax

    【讨论】:

    • 谢谢你的回答,我正在查看你发给我的帖子:-)
    • 我找不到如何调整我的代码,我尝试了您发送给我的链接上提出的几种解决方案,但总是有一些东西不起作用:(
    猜你喜欢
    • 2014-02-06
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多