电子邮件不发送。 ajax发送的值

时间:2015-07-20 13:48:56

标签: javascript php jquery ajax email

我正在通过AJAX调用获取值并发送隐藏字段值,然后执行查询以更新数据库中的内容。第一个初始查询查找id和其他$_POST信息,效果很好。但是我的电子邮件没有发送。我的警报错误消息未显示任何内容,但正在显示(这是一个空白警报)。

有没有人看到我试图发送到我的电子邮件的方式有什么问题,或者我如何在我的电子邮件中提取它不会发送的电子邮件中的值?

隐藏的输入字段..

<input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
    <input type="hidden" value="<?php echo $approved_firstname; ?>" id="approved_firstname" name="firstname" />
    <input type="hidden" value="<?php echo $approved_lastname; ?>" id="approved_lastname" name="lastname" />
    <input type="hidden" value="<?php echo $approved_username; ?>" id="approved_username" name="username" />
    <input type="hidden" value="<?php echo $approved_email; ?>" id="approved_email" name="email" />

ajax call ..

$(document).ready(function () {

    $('#update_group').on('submit', function (event) {
    event.preventDefault();
        $.ajax({
            url: 'user_group_update.php',
            type: 'POST',
            data: {
            id: $("#approved_id").val(), //id
            firstname: $("#approved_firstname").val(), //firstname
            lastname: $("#approved_lastname").val(), //lastname
            username: $("#approved_username").val(), //username
            email: $("#approved_email").val(), //email
           // update_group: $("#group_id").val() //group level
          update_group: $(this).find( "#group_id option:selected" ).val()
        },
            success: function (data) {
                //do something with the data that got returned
                $(".group_success").fadeIn();
                $(".group_success").show();
                $('.group_success').html('User Permission Level Changed!');
                $('.group_success').delay(5000).fadeOut(400);
                alert(data);
            },
             error: function(jqXHR, textStatus,errorThrown )
            {
              // alert on an http error 
              alert( textStatus +  errorThrown );
            }
        });
        return false;
    });
});

user_group_update.php

<?php
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
error_reporting(E_ALL);
require_once 'core/init.php';

$approved_id = $_POST['id'];
//test - delete if it doesn't work
$approved_firstname = $_POST['firstname'];
$approved_lastname = $_POST['lastname'];
$approved_username = $_POST['username'];
$approved_email = $_POST['email'];
$change_group = $_POST['update_group'];

$con = mysqli_connect("localhost","","","");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("UPDATE users,user_requests SET users.group=?, user_requests.group=? WHERE users.id=? AND user_requests.user_id=?");
    if ( !$stmt || $con->error ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('iiii', $change_group, $change_group, $approved_id, $approved_id)) {
    // Check errors for binding parameters
        die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$stmt->execute()) {
        die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
    }

    //test
    $email_stmt = $con->prepare("SELECT * FROM users WHERE id=?");
    if ( !$email_stmt || $con->error ) {
     // Check Errors for prepare
        die('User email prepare() failed: ' . htmlspecialchars($con->error));
    }
    /*if(!$email_stmt->bind_param('ii', $change_group, $approved_id)) {
    // Check errors for binding parameters
        die('User email bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$email_stmt->execute()) {
        die('User email execute() failed: ' . htmlspecialchars($stmt->error));*/
        /*
            $row = mysqli_fetch_assoc($$email_stmt);
            $pending_id = $_POST['id'];
            $group_firstname = $row['firstname'];
            $group_lastname = $row['lastname'];
            $group_username = $row['username'];
            $group_email = $row['email'];
            $group_email = $row['group'];*/

            $to = $approved_email;
            $subject = 'There is a new user request to join the Sunday Funday League';
            $message = '
            <html>
            <head>
                <title>New SFL User Request</title>
            </head>
            <body>
                <p>Hi '.$approved_firstname.',</p><br>
                <p>Your Sunday Funday League Account has been accepted. You have been added to the group. To sign in, click this link
                http://sundayfundayleague.com . </p><br>
                <p>Thank you,</p>
                <p>Administration</p>
            </body>
            </html>
            ';

            $from = "user-requests@sundayfundayleague.com";
            $Bcc = "user-requests-confirm@sundayfundayleague.com";

            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            //$headers .= 'To: ' .$to;//. "\r\n";
            $headers .= 'From: ' .$from; "\r\n";
            $headers .= 'Bcc: '.$Bcc; "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);

1 个答案:

答案 0 :(得分:1)

PHP中的mail()函数将返回truefalse,因此您无法在客户端中看到错误。

然而,当error_get_last()因答案here而失败时,您可以使用mail()

由于mail()函数返回truefalse,您将能够创建一个简单的if语句来检查它是否成功:

if(mail($to,$subject,$message,$headers)){
     // success
} else { 
     print_r(error_get_last());
}

尝试此操作以获取有关后台发生情况的更多信息。