[英]Getting email addresses via while loop and sending php email
I am attempting to send a message to all users on my site with the group of 5. However, the way I have this email form structured, I am getting my if statement response come back saying the email address was not filled in. 我正在尝试向我的网站上的所有用户发送一条消息,其中包含5个组。但是,我将这个电子邮件表单的结构化方式,我收到我的if语句响应回来说电子邮件地址没有填写。
I'm trying to add the email address through a while loop like this.. 我试图通过像这样的while循环添加电子邮件地址..
while ($stmt->fetch()) {
$to = $user_email;
}
I am sending the subject and message through my form via AJAX. 我通过AJAX通过我的表单发送主题和消息。 I am not getting any errors from that.
我没有收到任何错误。 This is the part I get sent back to me onto my page..
这是我被送回我的页面的部分..
else {
echo "Email Address was not filled out.";
}
What am I doing wrong for this to say an email address was not filled in? 如果说电子邮件地址没有填写,我做错了什么? Also if I have more than one user's email address, would the way I have it work or would I need to structure the
$to
differently? 此外,如果我有多个用户的电子邮件地址,我可以使用它的方式,还是需要
$to
不同方式构建$to
?
Here is the full code: 这是完整的代码:
$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.";
}
It looks like $email is not being defined. 看起来好像没有定义$ email。 You could check for
$to
instead of $email
in : 您可以在以下位置检查
$to
而不是$email
:
if (!empty($email)) {
...
}
so: 所以:
if (!empty($to)) {
...
}
For multiple recipients, use this: 对于多个收件人,请使用以下命令:
$to .= $user_email . ',';
after defining $to = '';
在定义
$to = '';
before your while loop 在你的while循环之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.