2016-07-26 13 views
-1

メールブロックを初期化するコードブロックを使用したいが、コードを関数に移動するたびにスクリプトが実行を停止してから電子メールを送信する。私はそれが正常に動作します。この機能を使用してWebページをロードし、適切な電子メール送信する場合:スクリプトを停止させる関数にコードを移動する

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; 
} 

しかし、私は別の関数にコードのブロックを移動し、その機能を渡すしようとすると、$メーラーがWebページオブジェクトを空白のページが読み込まれ、電子メールは送信されません。非稼働コード:

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)関数は依然として電子メールを送信するからです。

これはシンプルなのですが、申し訳ありませんが、参考にしたり値を渡したりすることで何か間違ったことをしていますか?

+2

空白のページ= PHPがクラッシュして、あなたは無効になってすべてのデバッグオプションがあります。

これを試してみてください。それらをオンにして、もう一度やり直してください。最初はdevel/debugシステムではOFFにしてはいけません。あなたの耳にあなたの指を詰め込み、 "ララルララはあなたの声を聞くことができません"というコードに相当します。 –

+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

関連する問題