【发布时间】:2015-12-07 05:20:17
【问题描述】:
我正在尝试向我网站上的所有 5 人组用户发送消息。但是,按照我构建此电子邮件表单的方式,我收到了我的 if 语句回复,说电子邮件地址未填写.
我正在尝试通过这样的 while 循环添加电子邮件地址..
while ($stmt->fetch()) {
$to = $user_email;
}
我通过 AJAX 通过我的表单发送主题和消息。我没有从中得到任何错误。这是我在页面上发回给我的部分..
else {
echo "Email Address was not filled out.";
}
说没有填写电子邮件地址,我做错了什么?此外,如果我有多个用户的电子邮件地址,我的工作方式是否可行,或者我是否需要以不同的方式构建 $to?
这里是完整的代码:
$subject = $_POST['subject'];
$message = $_POST['message'];
try {
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("SELECT id, email, username FROM users WHERE `group` IN (5)");
if ( !$stmt || $con->error ) {
// Check Errors for prepare
die('User/Player SELECT prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->execute()) {
die('User/Player SELECT execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
} catch (Exception $e ) {
die("User/Player SELECT execute() failed: " . $e->getMessage());
}
$stmt->bind_result($userid, $user_email, $username);
while ($stmt->fetch()) {
$to = $user_email;
}
$subject = 'Updated Status';
$message = '';
$from = "surveys@example.com";
$Bcc = "surveys_check@example.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);
//if(mail($to,$subject,$message,$headers)){
// echo "Success";
//} else {
// print_r(error_get_last());
// echo "Fail";
//}
if (!empty($email)) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//Should also do a check on the mail function
if (mail($to, $subject, $message, $headers)) {
echo "Your email was sent!"; // success message
} else {
echo "Mail could not be sent!"; // failed message
}
} else {
//Invalid email
echo "Invalid Email, please provide a valid email address.";
}
} else {
echo "Email Address was not filled out.";
}
【问题讨论】:
-
你从哪里得到
$user_email值? -
来自我的 bind_result
$stmt->bind_result($userid, $user_email, $username); -
似乎没有定义
$email。 -
@mattslone 成功了!!谢谢。
-
你应该可以只添加逗号,
$to .= $user_email . ','
标签: php html email loops while-loop