これまでMVCを使用してメールを送信したことがなく、少し行き詰まっています。
私のアプリフォルダーには、Controller.php、Core.php、Database.phpを含むライブラリフォルダーがあり、Email.phpを作成しました
Email.phpには、次のクラスがあります。
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
class Email {
public function sendMail()
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]'); // Add a recipient // Name is optional
$mail->addReplyTo('[email protected]');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
メールビューにアクセスしたときに、メールの送信をトリガーしようとしています。しかし、コントローラーに何を入れるべきかわかりません。以下のコードは私にエラーを与えます。
public function email()
{
$this->sendMail();
$this->view('pages/email');
}
致命的なエラー:キャッチされていないエラー:未定義のメソッドPages :: sendMail()の呼び出し
クラスEmailのインスタンスを作成する必要があります。
$email = new Email();
$email->sendMail();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加