Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP邮件发送重复项_Php_Sql_Email - Fatal编程技术网

PHP邮件发送重复项

PHP邮件发送重复项,php,sql,email,Php,Sql,Email,我有一个问题,我当前的php使用phpMail发送重复的电子邮件。php文件将由cronjob运行,但是对于我们来说,我们只是手动运行它 我尝试了$mail->ClearAddresses();但这似乎没有帮助 当我们丢弃$mail时;但看起来它只发送了一次,而我们的“消息已发送”消息只打印了一次数据库中的pr电子邮件地址 我们还尝试了另一个线程中建议的selectdistinct,但似乎没有效果 我们还尝试在脚本的不同位置添加计数器,但它显示了正确的迭代次数 <?php ini_

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

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

当我们丢弃$mail时;但看起来它只发送了一次,而我们的“消息已发送”消息只打印了一次数据库中的pr电子邮件地址

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

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

<?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();
    ?>

您正在调用
$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++;
}
$mail->send();

如果(!$mail->send()){//db请求的结果(vardump)是什么?最好添加输出和提到的转储。这很有效!非常感谢!为我们节省了很多时间!
// 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++;
}