【问题标题】:PHP send mail for multiple users selected from databasePHP为从数据库中选择的多个用户发送邮件
【发布时间】:2017-02-18 13:09:01
【问题描述】:

所以我是这样使用 PHP 的:

if(isset($userID)) {    

$premium = $con->prepare("
SELECT Email
FROM tblName as d       
WHERE Rank = $rank and Type = $type
"); 
$premium->execute();

$premium->bind_result($email);


} else {
    echo "There is no User ID detected, try to refresh browser.";   
}

while ($premium->fetch()) { 

    # SUBJECT (Subscribe/Remove)
     $subject = "New Resume";

    # RESULT PAGE
    $location = "http://www.website.com";

    $sender = "info@website.com";

    # MAIL BODY
    $message = '<html><body>';    
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
    $message .= "</table>";
    $message .= "</body></html>";

    $cc = "ss@gmail.com";
    $headers = "From: " . $sender . "\r\n";
    $headers = "BCC: " . $cc . "\r\n";
    $headers .= "Reply-To: ". strip_tags($_POST['Email']) . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $to = $email;

    mail( $to, $subject, $message, $headers) or die ("Mail could not be sent.");
}
header("Location: http://website.com/");
die(); 

mysqli_close($link);

但这不会为每个选定的用户发送电子邮件。我如何正确地使用循环?我应该使用foreach 还是改进while 循环?在这种情况下,我该如何申请arrays?有人可以让我走正确的路吗?谢谢!

【问题讨论】:

  • 您通过在 sql 中直接嵌入变量而错过了 prepared statements 的要点 - 使用占位符并在 pdo 中的 mysqlibindParam 中使用 bind_param 分配变量。最初的headers$headers ="BCC: " . $cc . "\r\n"; 的第二行覆盖
  • 你的代码有很多错误
  • @mohade 谢谢你的回复,我想听听任何关于如何改进代码的建议。
  • $to = $email 从哪里获得 $email 以及它的内容...第二个 $headers 覆盖了它需要的第一个 (.)
  • @mohade $email 来自bind_results

标签: php mysql sql email mysqli


【解决方案1】:

我使用 foreach 循环实现了这一点。

在使用 foreach 循环之前,将所有电子邮件地址保存在一个数组中并遍历该数组以减少运行时间。

您可以通过迭代从数据库中获取的整个数据来反其道而行之。

您的选择。

【讨论】:

  • 感谢您的回答,但我不知道如何正确使用数组。
  • 使用$emails= $premium-&gt;fetch_assoc()
  • 这不会覆盖bind_result($email);?我还需要while ($premium-&gt;fetch()) { }吗?
  • 你不再需要所有这些了
【解决方案2】:

您必须在此期间使用fetch_assoc(),因为如果您使用它,它将一一获取所有记录,然后允许根据您需要的要求发送 MAIL。

你必须在这里连接第二行的标题,因为它会覆盖语句

替换:

$headers = "From: " . $sender . "\r\n";
$headers = "BCC: " . $cc . "\r\n";

与:

$headers = "From: " . $sender . "\r\n";
$headers .= "BCC: " . $cc . "\r\n";

【讨论】:

    【解决方案3】:

    当您使用密件抄送时,请勿使用循环发送邮件。 循环查询您的查询结果,收集所有收件人 ID 并一次调用发送邮件。

    密件抄送:密件抄送给收到邮件的第三位收件人。主要和次要收件人看不到第三个收件人。根据电子邮件软件的不同,第三收件人可能只能在密件抄送中看到他们自己的电子邮件地址,或者他们可能会看到所有主要和次要收件人的电子邮件地址。

    【讨论】:

    • 感谢您的回复,我不会在生产模式下使用密件抄送,我现在仅用于测试目的。
    【解决方案4】:

    准备好的语句应该利用占位符在执行前安全地分配变量

    正如评论中指出的那样 - 如果您在密件抄送字段中分配每个收件人,您可以在循环中收集电子邮件地址,但在循环之后发送电子邮件。这样每个收件人都会看到TO 地址,但其他收件人都看不到。

    if( isset( $userID, $rank, $type, $_POST['Email'] ) ) {  
    
        /* create sql with placholders and prepare */
        $sql='select email from tblname where rank=? and type=?';
        $premium = $con->prepare( $sql );
    
        /* Only proceed if the prepared statement succeeded */
        if( $premium ){
    
            /* bind the variables to the placeholders and execute */
            $premium->bind_param('ss',$rank,$type);
            $premium->execute();
            $premium->bind_result( $email );
    
    
    
            $to = "ss@gmail.com";
            $bcc= array( $to );
            $subject = "New Resume";
            $location = "http://www.website.com";
            $sender = "info@website.com";   
    
            /* collect all email addresses and add to BCC */
            while( $premium->fetch() ) {
                $bcc[]=$email;
            }
    
            /* close the prepared statement */
            $premium->close();
    
            /* Close the db connection */
            $link->close();
    
    
            $message = '
            <html>
                <body>
                    <table rules="all" style="border-color: #666;" cellpadding="10"></table>
                </body>
            </html>';
    
            $headers = "From: " . $sender . "\r\n";
            $headers .= "BCC: " . implode( ',', $bcc ) . "\r\n";
            $headers .= "Reply-To: ". strip_tags( $_POST['Email'] ) . "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    
    
    
            $status=@mail( $to, $subject, $message, $headers );
    
    
    
    
            die( header( "Location: " . ( $status ? 'http://website.com/?mailsent=true' : 'http://website.com/?mailsent=false' ) ) ); 
        } 
    } else {
        echo "There is no User ID detected, try to refresh browser.";   
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 2014-06-05
      相关资源
      最近更新 更多