2016-07-26 151 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

相关问题