2013-02-05 60 views
0

我正在研究其他開發人員開發的腳本。有它發送電子郵件警報系統,並且每個模塊有自己的警示文件和警報文件require_once PHPMailer的文件的多個require_once

<?php 
    // assume this is 1.php 
    //.......... some code above // 
    $mail_title = 'Message Title'; 
    $mail_body = 'Message body with complete details what has been done here';  
    require_once 'module_alert.php'; // <- PHP Mailer is requir_once in this file 
    //........... ajax and some response // 
?> 

只要我require_once它生成的電子郵件和派遣這一點。

現在我必須更新模塊中的PHP頁面。該頁面必須將數據發佈到3個不同的php頁面模塊。在該頁面僅向一個PHP頁面VIA ajax發送數據之前,其他兩個頁面使用First First的自動遞增ID。

由於複雜性,我想包括其他兩個文件以及文件1

<?php 
    // assume this is 1.php 
    //.......... some code above // 
    $mail_title = 'Message Title'; 
    $mail_body = 'Message body with complete details what has been done here';  
    require_once 'module_alert.php'; // <- PHP Mailer is requir_once in this file 
    //........... ajax and some response // 
    if($condition){ 
     include '2.php'; // <- this file again require_once 'module_alert.php' 
     include '3.php'; // <- this file again require_once 'module_alert.php' 
    } 
?> 

的問題是,我想對所有3個步驟發送警報,並且還我不想改變其他文件中的大量代碼可能會破壞腳本。

在這種情況下我有什麼選擇可以存活下來?

編輯

module_alerts.php有東西數量。郵件佈局,表格圖像,條件接收者,誰應該獲得這個模塊的警報等。$ mail-> send()函數,以便發送電子郵件給特定的接收者。

+2

我想建議將require_once替換爲require ...但wtf是在module_alert.php中嗎? – ajreal

+0

我已經更新了你的問題。 :) –

回答

2

發送電子郵件的邏輯應該包裝在一個函數中,然後您可以在程序中的任意位置調用相應的參數。例如:

function sendMyEmail($recipient, $subject, $message) 
{ ... } 

你當然可以構建一個類,一系列的功能,真是任何類型的可重複使用的合法設施,這在現在和將來會緩解這個問題。

HTH

+0

它是一個很好的建議,但現在考慮項目的規模,我必須說它至少需要4到5天來更新模塊的郵件功能。而且有100多個模塊 –

+2

我會堅持我的回答,因爲它可以節省時間和長期的理智。 – jstephenson

+0

+1這個答案。如果您包含包含功能代碼但未包含在函數中的PHP文件,那麼您的系統寫得很差。抱歉。 – SDC