电子邮件未发送。 ajax发送的值[重复]
Posted
技术标签:
【中文标题】电子邮件未发送。 ajax发送的值[重复]【英文标题】:Email not sending. Values sent by ajax [duplicate] 【发布时间】:2015-10-09 16:17:48 【问题描述】:我正在通过 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 调用..
$(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】:PHP 中的mail()
函数将返回true
或false
,因此您将无法在客户端中看到错误。
但是,当 mail()
回答 here 失败时,您可以使用 error_get_last()
。
由于 mail() 函数返回 true
或 false
,您将能够创建一个简单的 if 语句来检查它是否成功:
if(mail($to,$subject,$message,$headers))
// success
else
print_r(error_get_last());
试试这个以获得更多关于后台发生的事情的信息。
【讨论】:
仍然出现空白警报屏幕。 您能否尝试在 if 中输入echo "success"
,在 else 中输入 echo "fail"
,以确保它进入其中之一?
我在 else 中无法显示在警报中。
我刚刚又读了一遍你的代码,发现$headers .= 'From: ' .$from; "\r\n";
和$headers .= 'Bcc: '.$Bcc; "\r\n";
应该是:$headers .= 'From: ' .$from. "\r\n";
和$headers .= 'Bcc: '.$Bcc. "\r\n";
你可以试试这个吗?
它现在回显成功,但仍然不发送电子邮件。以上是关于电子邮件未发送。 ajax发送的值[重复]的主要内容,如果未能解决你的问题,请参考以下文章