2010-09-16 42 views
0

我正在創建一個函數來發送通知電子郵件給使用phpMailer lib的用戶。phpMailer創建類的多個實例時的空白頁

public function notify($address,$subject = '',$body = null,$mailer_options = array()) { 
     try { 
      $phpmailer = new PHPMailer($exceptions = true); 
      $phpmailer->CharSet = 'UTF-8'; 
      $phpmailer->IsSMTP(); 
      $phpmailer->SMTPAuth = true; 
      $phpmailer->SMTPKeepAlive = true; 
      //$phpmailer->SMTPDebug = true; 
      $phpmailer->IsHTML(true); 

      $phpmailer->Host = ... 
      $phpmailer->Username = ... 
      $phpmailer->Password = ... 
      $phpmailer->From = ... 
      $phpmailer->FromName =... 


      $phpmailer->AddAddress($address); 
      $phpmailer->Subject = $subject; 
      $phpmailer->Body = $body; 

      $phpmailer->Send(); 
      $phpmailer->ClearAllRecipients(); 
} 

它工作正常,如果我只是發送電子郵件或在課堂內發送多個電子郵件。 但是,如果這樣做

for($i=0;$i<3;$++) 
{ 
    $notification = new $Notification(); 
    $notification->notify(...); 
} 

它回顧一個空白頁。沒有錯誤,消息,什麼都沒有。 在你問我打開display_errors之前。

它可能是什麼?

它工作正常,如果我有這樣的PHPMailer的一個實例:

$phpmailer = new PHPMailer($exceptions = true); 
(...) 

     for($i=0;$i<3;$i++) 
     { 
      $phpmailer->AddAddress('address'); 
      $phpmailer->Subject = ""; 
      $phpmailer->Body = "sasa"; 

      $phpmailer->Send(); 
      $phpmailer->ClearAllRecipients(); 
     } 
+0

在這個例子中,它發送2封電子郵件,然後死亡。 – brpaz 2010-09-16 10:43:24

回答

1

取出$new Notification

for($i=0;$i<3;$++) 
{ 
    $notification = new Notification(); 
    $notification->notify(...); 
} 

new $Notification將從變量$Notification的值來創建新實例。 如果$Notification的確不包含「通知」如果你已經在你的PHP腳本打開display_errors上,但服務器在默認情況下禁用它這隻會工作(假設你的類被命名爲「通知」)

,錯誤贏得」如果腳本中存在語法錯誤,則會顯示。

+0

對不起,這是一個打字錯誤。我已經調整了我的代碼,將它放在這裏 – brpaz 2010-09-16 10:47:14

+0

我看到一個'try'塊。 「catch」在哪裏?你是如何啓用'display_errors'的?使用.htaccess/php.ini或在PHP腳本中?請參閱編輯的文章 – Lekensteyn 2010-09-16 10:50:03

+0

我在localhost工作。另外我在開發環境中使用Symfony框架,所以它默認顯示所有異常和錯誤。 – brpaz 2010-09-16 10:50:20