Ajax联系表单脚本不会在电子邮件中发送消息

时间:2017-10-31 21:27:42

标签: php ajax email

所以我在ajax中编写了一个脚本,连接到一个PHP mail()脚本,向info@example.com发送一封电子邮件(显然用我的域名替换了example.com),电子邮件发送正常,一切都安装好了除外一个问题:

     $.ajax({
        url:'contact.php',
        type:'POST',
        data: {
           name:name,
           email:email,
           message:message
          },
        dataType: 'json',
        success: function(response){
            alert(response);
        },
        error: function(response){
            alert("Oops, something went wrong. Please try again.");
        }
    })
    return false;
    event.preventDefault();
    })

我收到的电子邮件的格式与我的contact.php完全相同:

<?php
    $to = "info@example.com";
    $from = $_POST['email'];
    $name = $_POST['name'];
    $message =$_POST['message'];
    echo $_POST['test'];
    $subject = "Contact form from: ".$name;
    $body = 'Hello developer(s)!<br><br>
The following is a submission from the Contact Form on <a target="_blank" href="https://example.com">example.com</a>. Please respond using your own email (like info@example.com).<br><br>
Name: '.$name."<br>
Email: ".$from."<br>
Message: <br>".$message."<br><br>
Thank you!";
    $headers = "From: ".$name." <".$from."> \r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail($to,$subject,$body,$headers);
    echo "Success!<br></br>";
?>

其中输出以下内容:

Hello developer(s)!

The following is a submission from the Contact Form on example.com. Please respond using your own email (like info@example.com).

Name: Owen Sullivan
Email: sulliops@gmail.com
Message: 


Thank you!

请注意,即使在表单中提交了邮件,邮件也会丢失。知道为什么这是唯一丢失的部分吗?

编辑:这是表单代码:

<form method="post" name="cform" id="cform" action="contact.php" class="contact-form">
    <div class="row">
        <div class="col-sm-6">
            <input name="name" id="name" type="text" class="form-control" placeholder="Your Name..." required>
        </div>
        <div class="col-sm-6">
            <input name="email" id="email" type="email" class="form-control" placeholder="Your Email..." required>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <textarea name="message" id="message" cols="" rows="4" class="form-control" placeholder="Your Message..." required></textarea>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-12 text-right">
            <input type="submit" id="submit" name="send" class="submitBnt btn btn-custom" value="Send Message">
        </div>
    </div>
    <div id="simple-msg"></div>
</form>

修复的AJAX代码:

$.ajax({
        url:'contact.php',
        type:'POST',
        data: $(this).serialize()
        dataType: 'json',
        success: function(response){
            alert(response);
        },
        error: function(response){
            alert("Oops, something went wrong. Please try again.");
        }
    })
    return false;
    event.preventDefault();
    })

2 个答案:

答案 0 :(得分:0)

您需要检查您获取&#34;消息&#34;的值的位置。 textarea。确保您使用的是.val().value。您可能在所有表单字段中使用.text(),而textarea则失败。

答案 1 :(得分:0)

以下是我建议的一些更改

// You have a syntax error on yours, you need a listener
$(document).ready(function() {
    /** Listen for submit **/
    $("#cform").on("submit",function(event) {
        // Stop form from submitting normally
        event.preventDefault();
        // Send ajax
        $.ajax({
            url: 'contact.php',
            type: 'POST',
            // Serialize the form
            data: $(this).serialize(),
            // Remove the json
            success: function(response){
                alert(response);
            },
            error: function(response){
                alert("Oops, something went wrong. Please try again.");
            }
        });
    });
});

然后在你的PHP中会是这样的:

    <?php
    $to      = "info@example.com";
    $from    = trim($_POST['email']);
    # Stop if you don't have a valid address
    if(!filter_var($from,FILTER_VALIDATED_EMAIL))
        die('Invalid email address.');
    # Strip out stuff from the submission
    $name    = htmlspecialchars(strip_tags(trim($_POST['name'])));
    $message = htmlspecialchars(strip_tags(trim($_POST['message'])));

    $subject = "Contact form from: ".$name;
    $body    = 'Hello developer(s)!<br /><br />
The following is a submission from the Contact Form on <a target="_blank" href="https://example.com">example.com</a>. Please respond using your own email (like info@example.com).<br><br>
Name: '.$name."<br>
Email: ".$from."<br>
Message: <br />".$message."<br /><br />
Thank you!";
    $headers = "From: ".$name." <".$from."> \r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    # You need to make a condition to make sure it does, in fact, send
    echo (mail($to,$subject,wordwrap($body, 70, "\r\n"),$headers))? 'Success!' : 'Failed to send message.';

修改

  

注意:您有两个ajax提交,即您要添加的提交以及<script src="js/app.js"></script>文件中从76开始的提交。您正在复制再次添加它的工作。您将需要编辑该块...或删除它并构建上述内容。