2017-02-24 4 views
-1

さまざまなテンプレートを使用してメールを送信するPHPクラスを作成しようとしています。たとえば、ユーザー名とパスワードでウェルカムメールを送信したいとします。私はsendwelcomemail()というメール機能を作成しようとしています。これは、クラスgetmailautho()からデフォルトのphpメーラー設定にアクセスしています。ここでさまざまなテンプレートを使用してメールを送信する電子メール機能を作成中にエラーが発生しました

がコードされています。私はこのスクリプトを実行すると、私は次のエラーを

<?php 
// Include the PHPMailer class 
include('PHPMailer/class.phpmailer.php'); 
require_once 'PHPMailer/PHPMailerAutoload.php'; 

class email { 


public function getmailauth(){ 

    // Setup PHPMailer 
$mail = new PHPMailer(); 
$mail->IsSMTP(); 

// This is the SMTP mail server 
$mail->Host = 'smtp.gmail.com'; 

// Remove these next 3 lines if you dont need SMTP authentication 
$mail->Port = 587; 
$mail->SMTPAuth = true; 
$mail->Username = '[email protected]'; 
$mail->Password = 'testing'; 
$mail->SMTPOptions = array(
    'ssl' => array(
     'verify_peer' => false, 
     'verify_peer_name' => false, 
     'allow_self_signed' => true 
    ) 
); 

// Set who the email is coming from 
$mail->SetFrom('[email protected]', 'test'); 


} 

function sendwelcomemail($username,$password,$to){ 
    $this->getmailauth(); 
    global $mail; 
    // Retrieve the email template required 
$message = file_get_contents('templates/welcome.html'); 

// Replace the % with the actual information 
$message = str_replace('%username%', $username, $message); 
$message = str_replace('%password%', $password, $message); 


// Set who the email is sending to 
$mail->AddAddress($to); 

// Set the subject 
$mail->Subject = 'Your account information'; 

//Set the message 
$mail->MsgHTML($message); 
$mail->AltBody = strip_tags($message); 

// Send the email 
if(!$mail->Send()) { 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 

} 
} 
?> 


<h3> testing </h3> 
<?php 
$email = new email; 
$email->sendwelcomemail("test","test","[email protected]"); 

?> 

を取得しています:

Fatal error: Call to a member function AddAddress() on null in C:\xampp\htdocs\db\mail_function.php on line 49

は、あなたたちは、このエラーで私を助けることはできますか?また、任意の提案は

+0

からグローバル$メールを削除し、追加します:)歓迎しますその中でメールを初期化し、$ this-> mailを使って他の関数にアクセスしてください。 – Naincy

答えて

1

__constructor()内のメールオブジェクトを定義するか、それを外部の関数を定義(phpmailerのを作るために他の機能にアクセス可能なオブジェクト)とコンストラクタを作成sendwelcomemail()

<?php 
// Include the PHPMailer class 
include('PHPMailer/class.phpmailer.php'); 
require_once 'PHPMailer/PHPMailerAutoload.php'; 

class email { 
    private $mail; 
function __constructor(){ 
// Setup PHPMailer 
$this->$mail = new PHPMailer(); 
} 

public function getmailauth(){ 

$this->$mail->IsSMTP(); 

// This is the SMTP mail server 
$this->$mail->Host = 'smtp.gmail.com'; 

// Remove these next 3 lines if you dont need SMTP authentication 
$this->$mail->Port = 587; 
$this->$mail->SMTPAuth = true; 
$this->$mail->Username = '[email protected]'; 
$this->$mail->Password = 'testing'; 
$this->$mail->SMTPOptions = array(
    'ssl' => array(
     'verify_peer' => false, 
     'verify_peer_name' => false, 
     'allow_self_signed' => true 
    ) 
); 

// Set who the email is coming from 
$this->$mail->SetFrom('[email protected]', 'test'); 


} 

function sendwelcomemail($username,$password,$to){ 
    $this->getmailauth(); 
    // Retrieve the email template required 
$message = file_get_contents('templates/welcome.html'); 

// Replace the % with the actual information 
$message = str_replace('%username%', $username, $message); 
$message = str_replace('%password%', $password, $message); 


// Set who the email is sending to 
$this->$mail->AddAddress($to); 

// Set the subject 
$this->$mail->Subject = 'Your account information'; 

//Set the message 
$this->$mail->MsgHTML($message); 
$this->$mail->AltBody = strip_tags($message); 

// Send the email 
if(!$this->$mail->Send()) { 
echo "Mailer Error: " . $this->$mail->ErrorInfo; 
} 

} 
} 
?> 


<h3> testing </h3> 
<?php 
$email = new email; 
$email->sendwelcomemail("test","test","[email protected]"); 

?> 
+0

は、関数のdint workより$ mailを定義しています。 "Parse error:構文エラー、予期しない '$ mail'(T_VARIABLE)、C:\ xampp \ htdocs \ db \ mail_function.phpの関数(T_FUNCTION) 8 "。 8行目は "$ mail = new PHPMailer();"です。 – Sohail

+0

プログラムを修正しました。チェックしてください –

関連する問題