PHP邮件程序发送重复项

时间:2017-04-28 08:29:54

标签: php sql email

我有一个问题,我当前的PHP使用PHPMailer发送重复的电子邮件。 php文件将由cronjob运行,但我们只是手动运行它。

我试过了$ mail-> ClearAddresses();但这似乎没有帮助。

当我们vardedump $ mail;但它看起来只发送一次,我们的消息已被发送"只有在我们的数据库中发送电子邮件地址时才会打印邮件。

我们还尝试了另一个线程中建议的select distinct,但似乎没有效果。

我们还尝试将计数器添加到脚本的不同位置,但它显示了正确的迭代次数。

<?php
    ini_set("allow_url_fopen", 1);
    include '/home/actiorwd/include/dbinfo.php';

    $counter =0;

    $servername = "localhost";
    $dbname = "actiorwd_websec";

    // Create connection
    $conn = new mysqli($servername, $user, $passw, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT DISTINCT * FROM users";
    $result = $conn->query($sql);
    $ch = curl_init();



    if ($result->num_rows > 0) {
        // output data of each row

        while($row = $result->fetch_assoc()) {
            $url = 'https://haveibeenpwned.com/api/v2/breachedaccount/';
            $url.= $row["email"];


            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
            curl_setopt($ch, CURLOPT_URL, $url);
            $results = curl_exec($ch);

            $obj = json_decode($results, TRUE);

            $newbreaches = count($obj);

            //run if new breaches are found
            if($row["breaches"]!==$newbreaches){

              require_once('/home/actiorwd/public_html/PHPMailer/PHPMailerAutoload.php');
              $mail = new PHPMailer;
              $mail->isSMTP();                                      // Set mailer to use SMTP
              $mail->Host = 'cpanel40.proisp.no';  // Specify main and backup SMTP servers
              $mail->SMTPAuth = true;                               // Enable SMTP authentication
              $mail->Username = 'breached@actionscript.no';                 // SMTP username
              $mail->Password = 'PASSWORDHERE';                           // SMTP password
              $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
              $mail->Port = 465;                                    // TCP port to connect to
              $mail->setFrom('breached@actionscript.no', 'WebSec');
              $mail->ClearAddresses();
              $mail->addAddress($row["email"]);     // Add a recipient
              $mail->addReplyTo('breached@actionscript.no', 'WebSec');
              $mail->isHTML(true);                                  // Set email format to HTML
              $mail->Subject = 'Breached!!!!';
              $mail->Body    = 'Someone hacked your account, there are: '.$newbreaches."breaches";
              $mail->AltBody = 'Someone hacked your account in plain text';
              $mail->send();

              if(!$mail->send()) {
                  echo 'Message could not be sent.<br />';
                  echo 'Mailer Error: ' . $mail->ErrorInfo . '<br />';
              } else {
                  echo 'Message has been sent <br />';
                  $counter++;
              }

              $updatebreach = "INSERT INTO users (`email`, `breaches`) VALUES ('$usermail', '$newbreaches') ON DUPLICATE KEY UPDATE `breaches` = '$newbreaches' ";
              //echo $updatebreach;

              if ($conn->query($updatebreach) === TRUE) {
                  echo "New record created successfully <br />";
              } else {
                  echo "Error: " . $updatebreach . "<br>" . $conn->error;
              }

            }

            sleep(2);
        }
    } else {
        echo "0 results";
    }
    curl_close($ch);
    echo $counter;


    $conn->close();
    ?>

1 个答案:

答案 0 :(得分:2)

您正在拨打$mail->send()两次:

$mail->send();

if(!$mail->send()) { // <--- Here you're actually sending it again.
  echo 'Message could not be sent.<br />';
  echo 'Mailer Error: ' . $mail->ErrorInfo . '<br />';
} else {
  echo 'Message has been sent <br />';
  $counter++;
}

你应该存储第一个发送的结果,如下所示:

// Store the status in a variable instead
$sent = $mail->send();

if(!$sent) { // Check the result of the variable instead of sending it again
  echo 'Message could not be sent.<br />';
  echo 'Mailer Error: ' . $mail->ErrorInfo . '<br />';
} else {
  echo 'Message has been sent <br />';
  $counter++;
}
相关问题