Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Php邮件通过简单的POST请求发送,但在使用AJAX时不发送_Javascript_Php_Ajax_Email_Openshift - Fatal编程技术网

Javascript Php邮件通过简单的POST请求发送,但在使用AJAX时不发送

Javascript Php邮件通过简单的POST请求发送,但在使用AJAX时不发送,javascript,php,ajax,email,openshift,Javascript,Php,Ajax,Email,Openshift,因此,我有一个网站与一个简单的联系形式,我想通过电子邮件接收信息 我创建了我的mail.php处理程序: <?php $owner_email = "somebody@somemail.com"; $headers = 'From:' . $_POST["email"]; $subject = 'A message from your site visitor ' . $_POST["name"]; $messageBody = ""; // so

因此,我有一个网站与一个简单的联系形式,我想通过电子邮件接收信息

我创建了我的mail.php处理程序:

<?php
    $owner_email = "somebody@somemail.com";
    $headers = 'From:' . $_POST["email"];
    $subject = 'A message from your site visitor ' . $_POST["name"];
    $messageBody = "";

    // some verifications

    try{
        if(!mail($owner_email, $subject, $messageBody, $headers)){
            throw new Exception('mail failed');
        }
        else{
            echo 'mail sent';
        }
    }
    catch(Exception $e){
        echo $e->getMessage() ."\n";
    }
?>
log()显示:“邮件已发送”,因此正确调用mail.php


该应用程序托管在OpenShift上,使用PHP5.4

我假设您进行了某种验证,以验证在from/subject中使用的变量不是垃圾?是的,JavaScript端使用正则表达式,服务器端使用PHP验证。
<form method="post" action="mail.php">

    <label>Name</label>
    <input name="name" placeholder="Type Here">

    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>

    <input id="submit" name="submit" type="submit" value="Submit">

</form>
mailHandlerURL:'mail.php',

...

// on submit when all validations are OK
$.ajax({
    type: "POST",
    url:_.mailHandlerURL,

    data:{
        name:_.getValFromLabel($('.name',_.form)),
        email:_.getValFromLabel($('.email',_.form)),
        phone:_.getValFromLabel($('.phone',_.form)),
        message:_.getValFromLabel($('.message',_.form)),
        stripHTML:_.stripHTML
    },
    success: function(response){
        console.log(response);
        _.showFu(); // to display to the user that the email has been sent
    }
})