【问题标题】:Getting email addresses via while loop and sending php email通过while循环获取电子邮件地址并发送php电子邮件
【发布时间】: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


【解决方案1】:

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

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

所以:

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

对于多个收件人,使用这个:

$to .= $user_email . ',';

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

【讨论】:

  • 完美!我这样做了if (!empty($user_email)) { 我应该改用$to 吗?有区别吗?
  • 没有,你应该没问题 :) 如果有帮助,请接受我的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2014-07-20
  • 1970-01-01
  • 2014-03-22
  • 2018-03-15
  • 1970-01-01
  • 2016-08-26
相关资源
最近更新 更多