繁体   English   中英

通过while循环获取电子邮件地址并发送php电子邮件

[英]Getting email addresses via while loop and sending php email

我正在尝试向我的网站上的所有用户发送一条消息,其中包含5个组。但是,我将这个电子邮件表单的结构化方式,我收到我的if语句响应回来说电子邮件地址没有填写。

我试图通过像这样的while循环添加电子邮件地址..

while ($stmt->fetch()) {
            $to = $user_email;
}

我通过AJAX通过我的表单发送主题和消息。 我没有收到任何错误。 这是我被送回我的页面的部分..

 else {
    echo "Email Address was not filled out.";
}

如果说电子邮件地址没有填写,我做错了什么? 此外,如果我有多个用户的电子邮件地址,我可以使用它的方式,还是需要$to不同方式构建$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.";
}

看起来好像没有定义$ email。 您可以在以下位置检查$to而不是$email

if (!empty($email)) { 
    ...
}

所以:

if (!empty($to)) { 
    ...
}

对于多个收件人,请使用以下命令:

$to .= $user_email . ',';

在定义$to = ''; 在你的while循环之前

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM