2012-09-25 47 views
0

因此,我一直致力于一个项目,并且我有一个将电子邮件发送给选择雇主的员工的功能。现在这个工作除了邮件功能(我使用WinHost,我们需要包括Mail.php为了使邮件功能工作)它有时会发送3封电子邮件,而不是2,有时1封电子邮件,而不是2.在php中发送邮件的问题

代码:

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; 

} 

感谢您所有的时间!

+2

我修正了您的缩进。 – ceejayoz

+0

您是否检查过您暗示的异常情况下数据库中的数据? – Thomas

+0

你很好。 –

回答

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,以便您可以看到每次迭代代码之间的实时 - 当然,您需要在代码块之间添加更多日志记录。

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

HTH

+0

会让你发布谢谢!@ –

+0

好吧,所以我收到这个时,我呼应它 - [20120925-170523]循环结束 –

+0

所以最后一个语句(error_log一)执行,但没有在两者之间?它至少应该有第一条语句(启动循环)。你没有看到这些吗?另外,请检查您的$ memberuser变量是否正确填充。 –