【问题标题】:php mailer wont send mail to rest of email addresses after it got any bad email in betweenphp mailer在收到任何不良电子邮件后不会将邮件发送到其他电子邮件地址
【发布时间】:2018-06-17 15:35:52
【问题描述】:

我编写了一个 PHP 邮件程序代码来向多个收件人发送电子邮件。但不幸的是,我在脚本中发现了一个缺点,我正在从数据库中获取电子邮件,让我们看看它是否获取 5 封电子邮件并尝试向所有这些电子邮件发送电子邮件,并且第 3 封电子邮件不是有效地址(任何原因)然后我的脚本只是通过向 1-2 发送邮件并显示错误无效电子邮件 3 和“退出”(不再继续)来完全“存在”该功能,我想要的是“退出”它应该跳过该迭代语句(电子邮件)的功能然后像“继续”语句一样继续下一个。同样在代码中,消息和主题是从另一个由表单组成的页面发送的。 这是代码:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_SESSION['userIs']))
{
$message = $_POST['message'];
$subject = $_POST['subject'];
//Load composer's autoloader
require './PHPMailer/vendor/autoload.php';
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 1;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'trying.test.00';                 // SMTP username
    $mail->Password = 'trying.test.00';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    //Recipients
    $mail->setFrom('trying.test.00@gmail.com', 'Tester');
include('./create-connection.php');
$get_list = "select name,email from signup";
$result = $conn->query($get_list) ;
if($result->num_rows>0)
{
    while($row_list = $result->fetch_assoc())
    {
        $to = $row_list['email'];
        $name = $row_list['name'];
        $mail->addAddress($to,$name);     // Add a recipient    
    }
}
    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = strip_tags($message);
    if($mail->send())
    {
    echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-check"></i></div>';
    }
} catch (Exception $e) {
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-times"></i></div>';   
}
$conn->close();
}
?>

【问题讨论】:

    标签: php html


    【解决方案1】:

    您可以在发送前验证电子邮件地址。

    或者更好:

    您可以在插入数据库之前验证电子邮件地址,这样您就知道数据库只包含有效数据。

    【讨论】:

    • C.以上所有
    • 兄弟我已经编写了一个脚本,在插入数据库之前验证人们的电子邮件,让我们假设如果人们删除了它的电子邮件,那么它仍然会在数据库中,但是 php 邮件程序会给出无效地址的错误。
    • 您能告诉我如何在发送前验证电子邮件地址吗?
    【解决方案2】:

    只需要添加一个验证来检查电子邮件是否有效,而无需在while循环中发送电子邮件,然后如果电子邮件有效,则处理邮件,否则“继续”当前迭代(电子邮件)并跳转到下一封电子邮件。 因此在更改代码后 ->

      <?php
        // Import PHPMailer classes into the global namespace
        // These must be at the top of your script, not inside a function
        use PHPMailer\PHPMailer\PHPMailer;
        use PHPMailer\PHPMailer\Exception;
        if(isset($_SESSION['userIs']))
        {
        $message = $_POST['message'];
        $subject = $_POST['subject'];
        //Load composer's autoloader
        require './PHPMailer/vendor/autoload.php';
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->SMTPDebug = 1;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'trying.test.00';                 // SMTP username
            $mail->Password = 'trying.test.00';                           // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom('trying.test.00@gmail.com', 'Tester');
        include('./create-connection.php');
        $get_list = "select name,email from signup";
        $result = $conn->query($get_list) ;
        if($result->num_rows>0)
        {
            while($row_list = $result->fetch_assoc())
            {
        //add a validation here 
    
        if(!filter_var($row_list['email'], FILTER_VALIDATE_EMAIL))
                {
                    echo "invalid email".$row_list['email'];
                continue;   
                }
           else
           {
               $to = $row_list['email'];
                $name = $row_list['name'];
    
                $mail->addAddress($to,$name);     // Add a recipient
            }
            }
        }
            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $message;
            $mail->AltBody = strip_tags($message);
            if($mail->send())
            {
            echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-check"></i></div>';
            }
        } catch (Exception $e) {
        echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-times"></i></div>';   
        }
        $conn->close();
        }
        ?>
    

    【讨论】:

    • 您自己对问题的回答确实是一个可行的解决方案。 :)
    • 这是不可能的,因为我是在阅读您的回答后才知道这个想法以在发送前验证电子邮件。谢谢@maart
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多