类 class.send_mail_func.php
class phpmailer {
public function sendMail($message, $subject){
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once ('./PHPMailer/vendor/phpmailer/phpmailer/src/Exception.php');
require_once ('./PHPMailer/vendor/phpmailer/phpmailer/src/PHPMailer.php');
require_once ('./PHPMailer/vendor/phpmailer/phpmailer/src/SMTP.php');
//отправляем почту
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->setLanguage('ru', './PHPMailer/language/');
$mail->CharSet = 'utf-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'host'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mail@mailru'; // SMTP username
$mail->Password = '123'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('m@m.ru', 'Text');
$mail->addAddress('user@user.ru', 'User'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->msgHTML($message);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
我这样用
include './class.send_mail_func.php';
$email_send = new phpmailer();
$email_send->sendMail($message,$subject);
我收到以下错误:Parse error: syntax error, unexpected 'use' (T_USE) in /home/class.send_mail_func.php on line 7
如何在类中正确使用 use?
如果这是您从 packagist 或 github-e 获取的 PHPMailer 包,那么在同一个 github 上有一个很好的示例。我知道您正试图将这个类包装在您自己的类中?
你的电话是正确的