さまざまなテンプレートを使用してメールを送信する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
は、あなたたちは、このエラーで私を助けることはできますか?また、任意の提案は
からグローバル$メールを削除し、追加します:)歓迎しますその中でメールを初期化し、$ this-> mailを使って他の関数にアクセスしてください。 – Naincy