0

所以我一直在做一个项目,我有一个功能可以向被选中雇主的员工发送电子邮件。现在,除了邮件功能(我正在使用 winhost,我们需要包含 Mail.php 才能使邮件功能正常工作)之外,它正在工作。它有时会发送 3 封电子邮件而不是 2 封,有时会发送 1 封电子邮件而不是 2 封。

编码 :

if (isset($_POST['openemailmem'])){
    $memberuser = $_POST['openemailmemusername'];
    $sql = "SELECT email, username, password, status FROM csvdata WHERE memberview =:user ";
    $getinfo=$DBH->prepare($sql);
    $getinfo->execute(array(':user' => $memberuser));

    while ($row = $getinfo->fetch(PDO::FETCH_ASSOC)) {
        $check = $row;
        $newEmployeeEmail = $check['email'];
        $csvusername = $check['username'];
        $password = $check['password'];
        $status = $check['status'];

        if ($status == "Open"){
            echo "tesing";
            $from = "the email of where it is coming from is here but i removed";  
            $to = $newEmployeeEmail; 
            if (!empty($_POST['cc'])){
                $cc = $_POST['cc']; 
            }
            if (!empty($_POST['ccsend'])){
                $cc = $_POST['ccsend'];
                $to .= ", $cc";
            }
            $subject = "removed msg"; 
            $body = "removed msg"; 
            $host = "i removed"; 
            $username = "i removed"; 
            $password = "i removed"; 
            $headers = array ('From' => $from, 'To' => $to,'Cc' => $cc, 'Subject' => $subject); 
            $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); 
            $mail = $smtp->send($to, $headers, $body);
        }       
    }

    header("Location: I removed this.php?getmsg=12");
    exit;

}

谢谢你所有的时间!!!

4

1 回答 1

1

您的解决方案很可能在您的日志中。如果抛出异常,您可以在其中找到它们。

另外一个建议:

在循环之前和之后添加日志记录。所以你的例子变成:

if (isset($_POST['openemailmem']))
{
    $memberuser = $_POST['openemailmemusername'];
    $sql        = "SELECT email, username, password, status "
                . "FROM csvdata WHERE memberview =:user ";
    $getinfo    = $DBH->prepare($sql);
    $getinfo->execute(array(':user' => $memberuser));

    $log_file = "/home/my/transaction.log";
    $now      = "[" . date("Ymd-His") . "] ";

    $message = $now . "Starting loop...";
    error_log($message, 3, $log_file);

    while ($row = $getinfo->fetch(PDO::FETCH_ASSOC)) 
    {

        $check            = $row;
        $newEmployeeEmail = $check['email'];
        $csvusername      = $check['username'];
        $password         = $check['password'];
        $status           = $check['status'];

        if ($status == "Open")
        {
            echo "tesing";
            $from = "the email of where it is coming from is here but i removed";  
            $to   = $newEmployeeEmail; 
            if (!empty($_POST['cc']))
            {
                $cc = $_POST['cc']; 
            }
            if (!empty($_POST['ccsend']))
            {
                $cc = $_POST['ccsend'];
                $to .= ", $cc";
            }
            $subject  = "removed msg"; 
            $body     = "removed msg"; 
            $host     = "i removed"; 
            $username = "i removed"; 
            $password = "i removed"; 
            $headers = array(
                           'From'    => $from, 
                           'To'      => $to,
                           'Cc'      => $cc, 
                           'Subject' => $subject,
                       );

            $now     = "[" . date("Ymd-His") . "] ";
            $message = $now . "Before connecting to server - " . $to;
            error_log($message, 3, $log_file);

            $smtp = Mail::factory(
                'smtp', 
                 array(
                     'host'     => $host, 
                     'auth'     => true, 
                     'username' => $username, 
                     'password' => $password,
                 )
            ); 

            $now     = "[" . date("Ymd-His") . "] ";
            $message = $now . "After connecting to server - " . $to;
            error_log($message, 3, $log_file);

            $now     = "[" . date("Ymd-His") . "] ";
            $message = $now . "Before sending email - " . $to;
            error_log($message, 3, $log_file);

            $mail = $smtp->send($to, $headers, $body);

            $now     = "[" . date("Ymd-His") . "] ";
            $message = $now . "After sending email - " . $to;
            error_log($message, 3, $log_file);
        }       
    }

    $now     = "[" . date("Ymd-His") . "] ";
    $message = $now . "End of loop";
    error_log($message, 3, $log_file);

    header("Location: I removed this.php?getmsg=12");
   exit;

}

您可能会发现您的函数超时,具体取决于邮件程序将电子邮件发送到相关主机所需的时间。

从我看到的每个用户都有一个(可能)唯一的到邮件服务器的连接,然后发送电子邮件。如果由于某种原因在建立该连接时出现延迟,那么您的脚本很可能会超时。

您可以调整上面使用的错误日志记录方法,microtime以便您可以查看代码每次迭代之间的实时时间 - 当然,您需要在代码块之间添加更多日志记录。

如果您发现邮件服务器连接确实存在延迟等,您可以(如果您的要求允许)连接到一台服务器并从那里发送您的电子邮件。

高温高压

于 2012-09-25T21:52:29.603 回答