2016-07-26 31 views
-1

我想使用一段代碼來初始化一個電子郵件程序,但每次將代碼移動到腳本在發送電子郵件之前停止執行的函數。當我使用這個功能,它工作正常加載網頁和發送適當的電子郵件:將代碼移至導致腳本停止的函數

function SendAdminIntimationEmail(&$formvars) 
{ 
    if(empty($this->admin_email)) 
    { 
     return false; 
    } 
    $mailer = new PHPMailer(); 

    $mailer->CharSet = 'utf-8'; 

    //Tell PHPMailer to use SMTP 
    $mailer->isSMTP(); 

    //Set the hostname of the mail server 
    $mailer->Host = "test.com"; 

    //Set the SMTP port number - likely to be 25, 465 or 587 
    $mailer->Port = 25; 

    //Whether to use SMTP authentication 
    $mailer->SMTPAuth = false; 

    //Username to use for SMTP authentication 
    $mailer->Username = "[email protected]"; 

    //Password to use for SMTP authentication 
    $mailer->Password = "pword"; 

    //Set who the message is to be sent from 
    $mailer->setFrom('[email protected]', 'test'); 

    //Set an alternative reply-to address 
    $mailer->addReplyTo('[email protected]', 'First Last'); 

    $mailer->AddAddress($this->admin_email); 

    $mailer->Subject = "New registration: ".$formvars['name']; 

    $mailer->From = $this->GetFromAddress();   

    $mailer->Body ="A new user registered at ".$this->sitename."\r\n". 
    "Name: ".$formvars['name']."\r\n". 
    "Email address: ".$formvars['email']."\r\n". 
    "UserName: ".$formvars['username']; 

    if(!$mailer->Send()) 
    { 
     return false; 
    } 
    return true; 
} 

但是當我移動的代碼塊到一個單獨的功能,並嘗試通過該功能的$郵件對象的網頁加載一個空白頁面並且不發送電子郵件。非工作代碼:

function SendAdminIntimationEmail(&$formvars) 
{ 
    if(empty($this->admin_email)) 
    { 
     return false; 
    } 
    $mailer = new PHPMailer(); 

    $mailer->CharSet = 'utf-8'; 

    InitSMTPMailer($mailer); 

    $mailer->AddAddress($this->admin_email); 

    $mailer->Subject = "New registration: ".$formvars['name']; 

    $mailer->From = $this->GetFromAddress();   

    $mailer->Body ="A new user registered at ".$this->sitename."\r\n". 
    "Name: ".$formvars['name']."\r\n". 
    "Email address: ".$formvars['email']."\r\n". 
    "UserName: ".$formvars['username']; 

    if(!$mailer->Send()) 
    { 
     return false; 
    } 
    return true; 
} 

function InitSMTPMailer($mailer) 
{ 
    //Tell PHPMailer to use SMTP 
    $mailer->isSMTP(); 

    //Set the hostname of the mail server 
    $mailer->Host = "test.com"; 

    //Set the SMTP port number - likely to be 25, 465 or 587 
    $mailer->Port = 25; 

    //Whether to use SMTP authentication 
    $mailer->SMTPAuth = false; 

    //Username to use for SMTP authentication 
    $mailer->Username = "[email protected]"; 

    //Password to use for SMTP authentication 
    $mailer->Password = "pword"; 

    //Set who the message is to be sent from 
    $mailer->setFrom('[email protected]', 'test'); 

    //Set an alternative reply-to address 
    $mailer->addReplyTo('[email protected]', 'test'); 

    return $mailer; 
} 

如果它是有用的,該功能總是被這個函數調用:

function RegisterUser() 
{ 
    if(!isset($_POST['submitted'])) 
    { 
     return false; 
    } 

    $formvars = array(); 

    if(!$this->ValidateRegistrationSubmission()) 
    { 
     return false; 
    } 

    $this->CollectRegistrationSubmission($formvars); 

    if(!$this->SaveToDatabase($formvars)) 
    { 
     return false; 
    } 

    if(!$this->SendUserConfirmationEmail($formvars)) 
    { 
     return false; 
    } 

    $this->SendAdminIntimationEmail($formvars); 

    return true; 
} 

添加多數民衆贊成使腳本無法正常工作的功能後,似乎RegisterUser( )在$ this-> SendAdminIntimationEmail($ formvars)失敗;因爲SendUserConfirmationEmail($ formvars)函數仍然發送一封電子郵件。

我是新來的PHP很抱歉,如果這很簡單,但我做錯了傳遞一個對象的引用或值是制動的東西嗎?

+2

空白頁= PHP墜毀,你必須關閉所有的調試選項。打開它們,然後重試。他們不應該首先在開發/調試系統上關閉。它的編碼相當於把你的指尖塞進你的耳朵,並且「lalalalala聽不到你」 –

+0

這些功能是否屬於一個類? '$ this'用於一個類中。 –

+0

'InitSMTPMailer'返回郵件,但你沒有分配它,並且你沒有通過引用傳遞它。 – aynber

回答

0

問題是你撥打InitSMTPMailer錯誤的方法。

SendAdminIntimationEmailInitSMTPMailer是類方法,但您呼叫InitSMTPMailer作爲全局函數(或只是忘記$this->)。

試試這個:

function SendAdminIntimationEmail(&$formvars) 
{ 
    if(empty($this->admin_email)) 
    { 
     return false; 
    } 
    $mailer = new PHPMailer(); 

    $mailer->CharSet = 'utf-8'; 

    $this->InitSMTPMailer($mailer); 

    $mailer->AddAddress($this->admin_email); 

    $mailer->Subject = "New registration: ".$formvars['name']; 

    $mailer->From = $this->GetFromAddress();   

    $mailer->Body ="A new user registered at ".$this->sitename."\r\n". 
    "Name: ".$formvars['name']."\r\n". 
    "Email address: ".$formvars['email']."\r\n". 
    "UserName: ".$formvars['username']; 

    if(!$mailer->Send()) 
    { 
     return false; 
    } 
    return true; 
} 
+0

謝謝你這個工作!我沒有足夠的聲望來投票,但你的回答的推理正是我所期待的。我對使用$ this->或多或少感到困惑,似乎無法在任何地方找到明確的答案。再次感謝! – user3286166