2017-02-24 2 views
-1

Ich versuche, eine PHP-Klasse zu erstellen, um E-Mails mit verschiedenen Vorlagen zu senden. Zum Beispiel möchte ich eine Willkommensmail mit Benutzername und Passwort senden. Ich versuche, eine E-Mail-Funktion namens sendwelcomemail(), die Standard-PHP-Mailer-Konfiguration aus der Klasse getmailautho() zugreifen. HierFehler beim Erstellen einer E-Mail-Funktion zum Senden von E-Mails mit verschiedenen Vorlagen

ist der Code:

<?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]"); 

?> 

Wenn ich dieses Skript ausführen, erhalte ich folgende Fehlermeldung:

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

Könnt ihr mir helfen, mit diesem Fehler aus? Auch Willkommen Anregungen :)

+0

erstellen Konstruktor und fügen Mail initialisieren und dann in anderen Funktionen mit $ this-> mail darauf zugreifen. – Naincy

Antwort

1

Definieren Sie Ihre E-Mail-Objekt in einem __constructor() oder außerhalb Funktionen definieren (um die PHPMailer zu machen Objekt anderen Funktionen) und global $ Mail entfernen 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

Definition $ Mail über Funktionen dint Arbeit .. Seine geben Fehler: "Parse Fehler: Syntax Fehler, unerwartete 'Mail' (T_VARIABLE), Funktion (T_FUNCTION) erwartet in C: \ xampp \ htdocs \ db \ mail_function.php online 8 ". Zeile 8 ist "$ mail = new PHPMailer();" – Sohail

+0

Habe das Programm korrigiert. Bitte prüfe –

Verwandte Themen