2014-10-17 64 views
0

我們有一個腳本來監視數據庫。在數據庫中存儲JSON數據和fromName和fromEmail。如果句柄爲空,那麼電子郵件仍然必須發送。所以我們創建了一個監控handlesAt列的cronjob。如果爲空,則應發送電子郵件。SWiFT + TWIG循環不同的發件人

我們的腳本工作正常,但它有點慢。因此我想知道我們是否以正確的方式進行。

搶發送

foreach($ResultSelectEmails AS $key => $value) 
{ 
     require_once '../library/Twig/Autoloader.php'; 

     Twig_Autoloader::register(); 
     $loader = new Twig_Loader_String(); 

     $twig = new Twig_Environment($loader, array(
       'cache' => '../tmp/cache/' 
     )); 

     $Contents = $twig->render(stripslashes($Templ['templateHTML']), $EData); 
     $Subject = $twig->render(stripslashes($Templ['templateSubject']), $EData); 

     $WriteFile = fopen('logs/'.$PDFLogFile.'.html','w'); 
     fwrite($WriteFile, $Contents); 
     fclose($WriteFile); 

     // Create the Transport 
     $transport = Swift_SmtpTransport::newInstance('mail.example.com', 25) 
     ->setUsername('[email protected]') 
     ->setPassword('examplePassword') 
     ; 

     // Create the Mailer using your created Transport 
     $mailer = Swift_Mailer::newInstance($transport); 

     $message = Swift_Message::newInstance($Subject) 
     ->setFrom(array($ResultSelectEmails[$key]['fromEmail'] => $ResultSelectEmails[$key]['fromName'])) 
     ->setTo(array($ResultSelectEmails[$key]['toEmail'])) 
     ->setBody($Contents, 'text/html', 'UTF-8') 
     ; 

     if(isset($EmailAttachements)) 
     { 
      foreach($EmailAttachements AS $key => $value) 
       $message->attach(Swift_Attachment::fromPath($value)); 
     } 

     $result = $mailer->send($message); 
} 

需要所有電子郵件在腳本中,我們爲每個電子郵件單獨運輸。有時郵件服務器會引發錯誤。主要是它不能登錄。

我們可以改善腳本性能嗎?

期待您的想法....

回答

0

我看到幾個電話,你不應該在foreach循環做。我不知道你想發送多少郵件,但這不是最佳的。由於您不是在並行處理循環,所以可以移動幾行以防止一次又一次創建一些實例。我不知道你的代碼的確切目的,沒有任何細節,很難說如果這是一個明顯的改進,但你可以試試看:

require_once '../library/Twig/Autoloader.php'; 

Twig_Autoloader::register(); 
$loader = new Twig_Loader_String(); 

$twig = new Twig_Environment($loader, array(
    'cache' => '../tmp/cache/' 
)); 

// Create the Transport 
$transport = Swift_SmtpTransport::newInstance('mail.example.com', 25) 
     ->setUsername('[email protected]') 
     ->setPassword('examplePassword') 
; 

// Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

foreach ($ResultSelectEmails AS $key => $value) { 
    $Contents = $twig->render(stripslashes($Templ['templateHTML']), $EData); 
    $Subject = $twig->render(stripslashes($Templ['templateSubject']), $EData); 

    $WriteFile = fopen('logs/' . $PDFLogFile . '.html', 'w'); 
    fwrite($WriteFile, $Contents); 
    fclose($WriteFile); 

    $message = Swift_Message::newInstance($Subject) 
      ->setFrom(array($ResultSelectEmails[$key]['fromEmail'] => $ResultSelectEmails[$key]['fromName'])) 
      ->setTo(array($ResultSelectEmails[$key]['toEmail'])) 
      ->setBody($Contents, 'text/html', 'UTF-8') 
    ; 

    if (isset($EmailAttachements)) { 
     foreach ($EmailAttachements AS $key => $value) 
      $message->attach(Swift_Attachment::fromPath($value)); 
    } 

    $result = $mailer->send($message); 
}