<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\SMTP; require 'path/to/PHPMailer/src/Exception.php'; require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; function sendMail($to, $toname, $subject, $body) { // 文字エンコードを指定 mb_language('uni'); mb_internal_encoding('UTF-8'); $mail = new PHPMailer(true); try { //Enable SMTP debugging //SMTP::DEBUG_OFF = off (for production use) //SMTP::DEBUG_CLIENT = client messages //SMTP::DEBUG_SERVER = client and server messages $mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->isSMTP(); $mail->Host = SMTP_SERVER; $mail->SMTPAuth = true; $mail->Username = SMTP_USER; $mail->Password = SMTP_PASS; $mail->SMTPSecure = 'tls'; $mail->Port = SMTP_PORT; $mail->CharSet = "utf-8"; $mail->setFrom(FROM_MAIL, SITE_NAME); $mail->addAddress($to, $toname); //$mail->addReplyTo('info@example.com', 'Information'); //$mail->addCC('cc@example.com'); //$mail->addBCC('bcc@example.com'); //Attachments //$mail->addAttachment('/var/tmp/file.tar.gz'); //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $body; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } } ?>
Fatal error: Uncaught Error: Class "SMTP" not found
use PHPMailer\PHPMailer\SMTP;
SMTPプロトコルを使用してメールを送信することができ、メッセージの配信可能性が向上します。また、SMTPクラスを使用することで、エラーハンドリングやデバッグ機能も利用できるようになり、問題が発生した際の原因特定が容易になります。