发送电子邮件php mysql时防止重复

时间:2018-04-26 05:02:03

标签: php sql loops for-loop phpmailer

我有这样的案例,

我想运行localhost:88 / run_email.php并使用 phpmailer

将每行的数据发送到行中提到的电子邮件地址
id | tools | model | sn | expiration_date | user_email | desc | date_modified

-----------------------------------------------------------------------------
12 | a1 | b | c | 2018-04-23 | email_1st@yahoo.com | status | 2018-04-25 17:53:48

22 | b2 | f | r | 2018-04-24 | email_2nd@gmail.com | contact221 | 2018-04-25 17:53:50

32 | d3 | e | lll | 2018-04-24 | email_3rd@gmail.com | 12 | 2018-04-25 17:53:52

42 | f4 | l | h | 2018-04-24 | email_4th@gmail.com | \N | 2018-04-25 17:53:56

52 | 9JJ5 | QW | NA | 2018-04-25 | email_5th@company.com | \N | 2018-04-25 17:53:58

例如,如果我发送到 email_1st@yahoo.com

它会收到 id = 12 | tools = a1 | model = b | sn = c | expiration_date = 2018-04-23

我目前的代码 -

    <?php
    require_once "vendor/autoload.php";
    require("vendor/phpmailer/phpmailer/src/PHPMailer.php");
    require("vendor/phpmailer/phpmailer/src/SMTP.php");

    //PHPMailer Object
    $mail = new PHPMailer\PHPMailer\PHPMailer();

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'ssl://smtp.googlemail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'email@email.com';                 // SMTP username
    $mail->Password = 'pwd';  
    $mail->Port = 465;                         // SMTP password
    $mail->SMTPSecure = 'tls';   

    $DB_host     = 'localhost';
    $DB_user     = 'root';
    $DB_password = '';
    $DB_name     = 'sent_email';

    $link = mysql_connect($DB_host, $DB_user, $DB_password);
    if (!$link) {
        die ('Failed to connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db($DB_name, $link);
    if (!$db_selected) {
        die ("Db errorr: " . mysql_error());
    }
        // sending query
        $result = mysql_query("SELECT id,tools,model,sn,expiration_date,user_email FROM `table` ");
        if (!$result) {
            die("Query to show fields from table failed");
        }
        $num_rows = mysql_num_rows($result);


    $mail->From = "email@email.com";//from dari Username
    $mail->FromName = "For Looping";
    $mail->addReplyTo("no-N-reply@email.com", "...loop ");
    $mail->isHTML(true);
    $mail->Subject = "loop";


                for($i=1;$i<=$num_rows;$i++){
                    $row = mysql_fetch_array($result);
                    $line[$i][5] = $row['user_email'];
                    echo "No. ".$i."is ".$line[$i][5]."<br>";
                                    $mail->addAddress($line[$i][5]);

                                    $mail->Body = "<i>".$line[$i][5]."</i>";


                                    if(!$mail->send()) 
                                    {
                                        echo "Mailer Error: " . $mail->ErrorInfo;
                                    } 
                                    else 
                                    {
                                        echo "Message has been sent successfully <br>";
                                    }
                }

结果如下     for循环结果:

sent to 1 email_1st@yahoo.com
    There is 5 inbox to email_1st@yahoo.com like below
    email ke 1- email_1st@yahoo.com
    email ke 2- email_1st@yahoo.com, email_2nd@gmail.com
    email ke 3- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com
    email ke 4- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com
    email ke 5- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com,email_5th@company.com

sent to 2 email_2nd@gmail.com
    There is 4 inbox to email_2nd@gmail.com like below
    email ke 1- email_1st@yahoo.com, email_2nd@gmail.com
    email ke 2- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com
    email ke 3- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com
    email ke 4- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com,email_5th@company.com

sent to 3 email_3rd@gmail.com
    There is 3 inbox to email_3rd@gmail.com like below
    email ke 1- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com
    email ke 2- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com
    email ke 3- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com,email_5th@company.com

sent to 4 email_4th@gmail.com
    There is 2 inbox to email_4th@gmail.com like below
    email ke 1- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com
    email ke 2- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com,email_5th@company.com

sent to 5 email_5th@company.com
    There is 1 inbox to email_4th@gmail.com like below
    email ke 1- email_1st@yahoo.com, email_2nd@gmail.com,email_3rd@gmail.com,email_4th@gmail.com,email_5th@company.com

问题是如何防止这些情况,如何检查我的逻辑循环,我试图通过使用echo显示结果来调试它。

问候。

1 个答案:

答案 0 :(得分:0)

我知道了,在这个街区

else 
{
echo "Message has been sent successfully <br>";
}

添加了命令$ mail-&gt; ClearAddresses(); $ MAIL-&GT; ClearAllRecipients(); 等,

else 
{
echo "Message has been sent successfully <br>";
$mail->ClearAddresses(); 
$mail->ClearAllRecipients();
}

我们可以讨论你是否还有任何问题取决于它