从数据库特定列为每个用户发送多封电子邮件

时间:2016-10-04 09:23:39

标签: php mysql email

我正在使用PHPMailer。

我在users列的email表格中记录用户电子邮件。我的表格中还有category列。我想向这些用户发送电子邮件。例如,我想向tech列中categoryPHPMailer的用户发送电子邮件。我可以这样做,但是当我发送时,first user -> first column email second user -> first column email second column email third user -> first column email second column email third column email 发送我的电子邮件,如

user -> first column email

我只想先发送second user -> second column email<?php require_once("class.phpmailer.php"); if($_POST['message']){ $mail = new PHPMailer(); $mail->IsSMTP(); $mail->IsHTML(true); $mail->Host = "host.name.net"; $mail->Port = 587; $mail->SMTPAuth = true; $mail->CharSet ="utf-8"; $mail->Username = "mail@mymail.com"; $mail->Password = "pass"; $mail->SetFrom("hi@mymail.com", "my mail name"); $categorychoose = $_POST['categorychoose']; $query = (" SELECT email FROM users WHERE category LIKE '$categorychoose' "); $result = mysql_query($query); while( $data = mysql_fetch_assoc($result) ) { $mail->AddAddress($data["email"]); $mail->Subject = $_POST['subject']; $mail->Body = $_POST['message']; if(!$mail->Send()) { echo "<h4>not send</h4>"; } else { echo "<h4>send</h4>"; } } } ?>

我现在有这些代码。

.body_text_wrap {
    text-align: center;
}
.body_text {
    display: inline-block;
    text-align: left;
    font-size: 1.13em;
}

1 个答案:

答案 0 :(得分:0)

我使用您更新的代码编辑了我之前的代码。

require_once("class.phpmailer.php");

if($_POST['message']){
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->IsHTML(true);
    $mail->Host = "host.name.net"; 
    $mail->Port = 587; 
    $mail->SMTPAuth = true; 
    $mail->CharSet  ="utf-8";
    $mail->Username = "mail@mymail.com"; 
    $mail->Password = "pass"; 
    $mail->SetFrom("hi@mymail.com", "my mail name");
    $categorychoose = $_POST['categorychoose'];

    $query = (" SELECT email FROM users WHERE category LIKE '$categorychoose' ");

    $mail->Subject = $_POST['subject'];
    $mail->Body = $_POST['message'];

    $result = mysql_query($query);
    while( $data = mysql_fetch_assoc($result) ){
        $mail->AddAddress($data["email"]);            
    }

    if(!$mail->Send()){
        echo "<h4>not send</h4>";
    } else {
        echo "<h4>send</h4>";
    }
}

此代码应该适合您。

在循环播放时尝试使用此代码。

while( $data = mysql_fetch_assoc($result) ){
    #$mail->AddAddress($data["email"]); /*Common format*/
    #$mail->addCC($data["email"]); /*List emails to recipient*/
    $mail->addBCC($data["email"]); /*Never show List emails to recipient*/
}

注1: CC表示碳复制。所有其他收件人将看到的以逗号分隔的更多收件人列表。

注2: BCC表示盲碳复制。以逗号分隔的更多收件人列表,任何其他收件人都不会看到。