【问题标题】:PHPMailer loop stops if incorrect email address如果电子邮件地址不正确,PHPMailer 循环将停止
【发布时间】:2023-01-20 03:40:51
【问题描述】:

这是我使用 PHPMailer 发送电子邮件的代码。

如果 mailto 地址正确,它会无缝地工作。

但是,如果 mailto 地址不正确或不存在,则循环停止并且不会传递数据库中的其余电子邮件。

我想如果电子邮件不正确,$mailer->send() 会发生错误,这会导致它跳转到catch 并且电子邮件的发送未在数据库中注册(不知道为什么它会跳转更新查询尽管)。然后,这将永远循环下去,什么也没有发生。

关于如何修复的任何想法?

如果 mailto 地址不存在,我只想跳过它并继续处理其余的电子邮件,或者,将其注册为 sent = 'no' 并继续处理其余的。

//SEND EMAIL
$x = 1;

while ($x > 0) {

    $result = $db->query("SELECT * FROM emails WHERE sent = '' AND mailto <> '' ORDER BY id ASC LIMIT 1");

    if (mysqli_num_rows($result)==0) {

        $x = 0;
    
    } else {    

        $row = $result->fetch_assoc();
        $id = $row["id"];

        //Load email data        
        $mailer->AddAddress($row["mailto"]);
        $mailer->Subject = $row["mailsubject"];
        $mailer->Body = $row["mailbody"];

        try {
            
            //Send email
            $mailer->send();
            
            //Register email sent on db
            $update = $db->query("UPDATE IGNORE emails SET sent='yes', date=now() WHERE id='$id'");

        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mailer->ErrorInfo}";
            $mailer->getSMTPInstance()->reset(); //Reset the connection to abort sending this message.
        }

        //Clear all addresses and attachments for the next iteration        
        $mailer->clearAddresses();
        $mailer->clearAttachments();

    }
    
    usleep(100000); //sleep for 0.1 seconds
    continue;

}

【问题讨论】:

  • 警告:您对SQL Injections 持开放态度,应该使用参数化准备好的陈述而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何类型的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!
  • 虽然这确实是最佳实践,但此脚本中没有 SQL 注入机会。查询中使用的唯一变量是$id,它来自内部,而不是来自任何用户提供的输入,甚至是间接的。它当然应该被验证并可能转换为 int 和/或转义,但这里没有漏洞。
  • 这是发送到列表的一种非常奇怪的方式。您正在获取整个列表,但随后仅使用第一条记录,将其标记为已发送,丢弃所有其余记录,然后重新开始。看看the mailing list example provided with PHPMailer

标签: try-catch phpmailer


【解决方案1】:

好的,我自己解决了问题。

如果它对某人有帮助,这里是优化的功能代码,如果 mailto 或 mailbcc 地址不正确或不存在,它不会停止。

$result = $db->query("SELECT * FROM emails WHERE sent = '' AND mailto <> ''");

foreach ($result as $row) {
    
    $id = $row["id"];
    
    try {
        
        $mailer->addAddress($row['mailto'], $row['mailtoname']);
        
        if (!empty($row["mailbcc"])) {
            $mailer->AddBCC($row["mailbcc"]);
        }
        
        $mailer->Subject = $row["mailsubject"];
        $mailer->Body = $row["mailbody"];
        
        if ($mailer->send()) {
              //Register email sent on db
            $update = $db->query("UPDATE IGNORE emails SET sent='yes', date=now() WHERE id='$id'");

        }
        
    } catch (Exception $e) {
        
        echo "Mailer Error: {$mailer->ErrorInfo}";
        
        //Register that email was not sent so that loop does not stop here
        $update = $db->query("UPDATE IGNORE emails SET sent='no' WHERE id='$id'");
        
        //Reset the connection to abort sending this message. The loop will continue trying to send to the rest of the list
        $mailer->getSMTPInstance()->reset(); 

    }
    
    //Clear all addresses and attachments for the next iteration
    $mailer->clearAddresses();
    $mailer->clearAttachments();

    usleep(100000); //sleep for 0.1 seconds
    continue;
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多