使用jquery ajax和php发送带有附件的电子邮件

使用jquery ajax和php发送带有附件的电子邮件,php,jquery,ajax,Php,Jquery,Ajax,我已经创建了一个程序来发送带有附件的电子邮件。首先,我在没有ajax的情况下创建它。然后它正常工作了。但是当我使用jqueryajax时,它不起作用。当我点击应用按钮时,什么也没发生。我的代码如下 <form action="sendemail.php" enctype="multipart/form-data" method="post"> <h1 class="cta-title">Its a Call To Action</h1

我已经创建了一个程序来发送带有附件的电子邮件。首先,我在没有ajax的情况下创建它。然后它正常工作了。但是当我使用jqueryajax时,它不起作用。当我点击应用按钮时,什么也没发生。我的代码如下

<form action="sendemail.php" enctype="multipart/form-data" method="post">
                <h1 class="cta-title">Its a Call To Action</h1>
                <div class="cta-desc">
                    <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br>
                    <input type="text" value='<?= $row['company_name'];?>' readonly style="width:    75%"><br><br>
                    <input type="text" value='<?= $row['location'];?>' readonly style="width: 75%"><br><br>
                    <input type="text" value='<?= $row['qulification'];?>' readonly style="width: 75%"><br><br>
                    <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br>
                    <input type="text" value='<?= $row['indate'];?>' readonly style="width: 37.5%">&nbsp;
                    <input type="text" value='<?= $row['expdate'];?>' readonly style="width: 37.5%"><br>
                    <input type="text" id="email" name="email" value='<?= $row['email'];?>'><br>
                    <input type="file" name="uploaded_file" id="uploaded_file" class="text-center center-block well well-sm">
                    <input type="button" id="btn" name="btn" class="btn btn-primary" value="Apply">
                </div>
                    <script>
                        $('#btn').click(function () {
                            $.ajax({
                                method:"POST",
                                url:"sendemail.php",
                                data:{email:$('#email').val()},
                                success:function (data) {
                                    alert(data);
                                    return false;
                                }
                            });
                        });
                    </script>
                </form>

这是行动的号召

这是我的实现。我改变了一些属性。但请注意,为了我的目的,我将ssl更改为tls和587,这一点很重要

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

</head>

<body>


    <form onsubmit="return submitForm();" enctype="multipart/form-data" method="post" name="fileinfo"  id="fileinfo" >
                <h1 class="cta-title">Its a Call To Action</h1>
                <div class="cta-desc">
                    <input type="text" value='a' readonly style="width: 75%"><br><br>
                    <input type="text" value='b' readonly style="width:    75%"><br><br>
                    <input type="text" value='c' readonly style="width: 75%"><br><br>
                    <input type="text" value='d' readonly style="width: 75%"><br><br>
                    <input type="text" value='e' readonly style="width: 75%"><br><br>
                    <input type="text" value='f' readonly style="width: 37.5%">&nbsp;
                    <input type="text" value='g' readonly style="width: 37.5%"><br>
                    <input type="text" id="email" name="email" value='iordanhs92@hotmail.com'><br>
                    <input type="file" name="files" id="files" class="text-center center-block well well-sm">
                    <input type="submit" id="btn" name="btn" class="btn btn-primary" value="Apply">
                </div>
                    <script>
                       function submitForm() {

                        console.log("submit event");

                        var fd = new FormData(document.getElementById("fileinfo"));
                        fd.append("label", "WEBUPLOAD");

                        $.ajax({
                          url: "mailattach.php",
                          type: "POST",
                          data: fd,
                          processData: false,  // tell jQuery not to process the data
                          contentType: false   // tell jQuery not to set contentType
                        }).done(function( data ) {
                                console.log("PHP Output:");
                                console.log( data );
                            });
                            return false;
                        }
                    </script>
                </form>


</body>
</html>

这是行动的号召












函数submitForm(){ 控制台日志(“提交事件”); var fd=新表单数据(document.getElementById(“fileinfo”); 附加(“标签”、“网络上传”); $.ajax({ url:“mailattach.php”, 类型:“POST”, 数据:fd, processData:false,//告诉jQuery不要处理数据 contentType:false//告诉jQuery不要设置contentType }).完成(功能(数据){ log(“PHP输出:”); 控制台日志(数据); }); 返回false; }

因此,在php中,我也将upload_文件更改为files。mailattach中的php代码与您提供的代码相同,这是我的实现。我改变了一些属性。但请注意,为了我的目的,我将ssl更改为tls和587,这一点很重要

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

</head>

<body>


    <form onsubmit="return submitForm();" enctype="multipart/form-data" method="post" name="fileinfo"  id="fileinfo" >
                <h1 class="cta-title">Its a Call To Action</h1>
                <div class="cta-desc">
                    <input type="text" value='a' readonly style="width: 75%"><br><br>
                    <input type="text" value='b' readonly style="width:    75%"><br><br>
                    <input type="text" value='c' readonly style="width: 75%"><br><br>
                    <input type="text" value='d' readonly style="width: 75%"><br><br>
                    <input type="text" value='e' readonly style="width: 75%"><br><br>
                    <input type="text" value='f' readonly style="width: 37.5%">&nbsp;
                    <input type="text" value='g' readonly style="width: 37.5%"><br>
                    <input type="text" id="email" name="email" value='iordanhs92@hotmail.com'><br>
                    <input type="file" name="files" id="files" class="text-center center-block well well-sm">
                    <input type="submit" id="btn" name="btn" class="btn btn-primary" value="Apply">
                </div>
                    <script>
                       function submitForm() {

                        console.log("submit event");

                        var fd = new FormData(document.getElementById("fileinfo"));
                        fd.append("label", "WEBUPLOAD");

                        $.ajax({
                          url: "mailattach.php",
                          type: "POST",
                          data: fd,
                          processData: false,  // tell jQuery not to process the data
                          contentType: false   // tell jQuery not to set contentType
                        }).done(function( data ) {
                                console.log("PHP Output:");
                                console.log( data );
                            });
                            return false;
                        }
                    </script>
                </form>


</body>
</html>

这是行动的号召












函数submitForm(){ 控制台日志(“提交事件”); var fd=新表单数据(document.getElementById(“fileinfo”); 附加(“标签”、“网络上传”); $.ajax({ url:“mailattach.php”, 类型:“POST”, 数据:fd, processData:false,//告诉jQuery不要处理数据 contentType:false//告诉jQuery不要设置contentType }).完成(功能(数据){ log(“PHP输出:”); 控制台日志(数据); }); 返回false; }

因此,在php中,我也将upload_文件更改为files。mailattach中的php代码与您提供的相同

是的,我正在运行此程序。没有发生任何情况。但在使用ajax之前,此程序运行良好。是的。使用了jquery库,没有错误报告。将Apply按钮类型更改为Submit您应该使用
FormData
对象通过ajax上载文件。当前
$\u文件['uploaded\u file']
为空。检查:是的,我正在运行这个。没有发生任何事情。但是在使用ajax之前,它运行得很好。是的。使用了jquery库,没有错误报告。将“应用”按钮类型更改为“提交”。您应该使用
FormData
object通过ajax上载文件。当前
$\u文件['uploaded\u file']
为空。检查:提供错误消息。如果您在非https连接下运行,也可以使用。将ssl更改为tls,将端口更改为587。使用gmail进行测试,但您必须登录gmail帐户,并更改在“不安全”连接下发送电子邮件的权限提供错误消息。如果您在非https连接下运行,也可以使用。将ssl更改为tls,将端口更改为587。使用gmail进行测试,但您必须登录gmail帐户,并更改在“不安全”连接下发送电子邮件的权限