PHP MYSQL在循环时向AddAddress添加多个电子邮件地址

时间:2014-09-15 21:59:32

标签: php mysql email

有人请指出我正确的方向。我在这里看了很多关于这个主题的帖子,我似乎找到了许多不同的拼图,但没有一个在一起。我想在广告插入数据库后向用户发送电子邮件。我没有收到任何错误,但也没有收到电子邮件。如果我在AddAddress行中输入硬编码和电子邮件地址,我会收到电子邮件,但数组不会合在一起。任何帮助表示赞赏。

public function create($category_id, $title, $description, $city, $price)
{
    // clean the input to prevent for example javascript within the notes.
    $category_id = \strip_tags($category_id);
    $title = \strip_tags($title);
    $description = \strip_tags($description);
    $city = \strip_tags($city);
    $price = \strip_tags($price);


    $sql = "INSERT INTO ads (category_id, user_id, title, description, city, price) VALUES (:category_id, :user_id, :title, :description, :city, :price)";
    $query = $this->db->prepare($sql);
    $query->execute(array(':category_id' => $category_id, ':user_id' => $_SESSION['user_id'], ':title' => $title, ':description' => $description, ':city' => $city, ':price' => $price));

    $count =  $query->rowCount();
    if ($count == 1) {
        $user_email = '';
        $sql = "SELECT user_email FROM users";
        $query = $this->db->prepare($sql);
        $query->execute(array(':user_email' => $user_email));
        return $query->fetchAll(); 

while ($row = mysql_fetch_array($query)){
  $to = $row['user_email'];

        // create PHPMailer object here. This is easily possible as we auto-load the according class(es) via composer
    $mail = new PHPMailer;

    // please look into the config/config.php for much more info on how to use this!
    if (EMAIL_USE_SMTP) {
        // Set mailer to use SMTP
        $mail->IsSMTP();
        //useful for debugging, shows full SMTP errors, config this in config/config.php
        $mail->SMTPDebug = PHPMAILER_DEBUG_MODE;
        // Enable SMTP authentication
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        // Enable encryption, usually SSL/TLS
        if (defined('EMAIL_SMTP_ENCRYPTION')) {
            $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
        }
        // Specify host server
        $mail->Host = EMAIL_SMTP_HOST;
        $mail->Username = EMAIL_SMTP_USERNAME;
        $mail->Password = EMAIL_SMTP_PASSWORD;
        $mail->Port = EMAIL_SMTP_PORT;
    } else {
        $mail->IsMail();
    }

    // build the email
    $mail->From = EMAIL_AD_NOTIFICATION_FROM_EMAIL;
    $mail->FromName = EMAIL_AD_NOTIFICATION_FROM_NAME;
    $mail->AddAddress($to);
    $mail->Subject = EMAIL_AD_NOTIFICATION_SUBJECT;
    $mail->Body = EMAIL_AD_NOTIFICATION_CONTENT;

    // send the mail
    if($mail->Send()) {
       $_SESSION["feedback_positive"][] = FEEDBACK_PASSWORD_RESET_MAIL_SENDING_SUCCESSFUL;
        return true;
    } else {
       $_SESSION["feedback_negative"][] = FEEDBACK_PASSWORD_RESET_MAIL_SENDING_ERROR . $mail->ErrorInfo;
        return false;
    }
}

    } else {
        $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED;
    }
    // default return
    return false;

}

2 个答案:

答案 0 :(得分:0)

  $sql = "SELECT user_email FROM users";
        $query = $this->db->prepare($sql);
        $query->execute(array(':user_email' => $user_email));
        return $query->fetchAll(); 

这有点不对,我想你想要更像......

$sql = $this->db->prepare("SELECT user_email FROM users WHERE user_email=?");
$sql->execute(array($user_email));


    while($row = $sql->fetch(PDO::FETCH_ASSOC)) {

答案 1 :(得分:0)

我想出了如何使它工作,但我不能让它与PDO一起工作。

public function create($category_id, $title, $description, $city, $price) {
    // clean the input to prevent for example javascript within the notes.
    $category_id = \strip_tags($category_id);
    $title = \strip_tags($title);
    $description = \strip_tags($description);
    $city = \strip_tags($city);
    $price = \strip_tags($price);


    $sql = "INSERT INTO ads (category_id, user_id, title, description, city, price) VALUES (:category_id, :user_id, :title, :description, :city, :price)";
    $query = $this->db->prepare($sql);
    $query->execute(array(':category_id' => $category_id, ':user_id' => $_SESSION['user_id'], ':title' => $title, ':description' => $description, ':city' => $city, ':price' => $price));

    $count = $query->rowCount();
    if ($count == 1) {


        // create PHPMailer object here. This is easily possible as we auto-load the according class(es) via composer
        $mail = new PHPMailer;

        // please look into the config/config.php for much more info on how to use this!
        if (EMAIL_USE_SMTP) {
            // Set mailer to use SMTP
            $mail->IsSMTP();
            //useful for debugging, shows full SMTP errors, config this in config/config.php
            $mail->SMTPDebug = PHPMAILER_DEBUG_MODE;
            // Enable SMTP authentication
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            // Enable encryption, usually SSL/TLS
            if (defined('EMAIL_SMTP_ENCRYPTION')) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            }
            // Specify host server
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
        } else {
            $mail->IsMail();
        }

        // build the email
        $mail->From = EMAIL_AD_NOTIFICATION_FROM_EMAIL;
        $mail->FromName = EMAIL_AD_NOTIFICATION_FROM_NAME;
        $mail->Subject = EMAIL_AD_NOTIFICATION_SUBJECT;
        $mail->Body = EMAIL_AD_NOTIFICATION_CONTENT;


        $mysql = mysql_connect('localhost', 'root', 'root');
        mysql_select_db('login', $mysql);
        $result = mysql_query("SELECT user_email FROM users where ad_email = 1", $mysql);


        while ($row = mysql_fetch_array($result)) {
            $mail->addAddress($row['user_email']);

            if (!$mail->send()) {
                echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
                break; //Abandon sending
            } else {
                echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';

            }
            // Clear all addresses and attachments for next loop
            $mail->clearAddresses();
            $mail->clearAttachments();
        }



    } else {
        $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED;
    }
    // default return
    return false;
}