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 :)
erstellen Konstruktor und fügen Mail initialisieren und dann in anderen Funktionen mit $ this-> mail darauf zugreifen. – Naincy