在ThinkPHP5中安装PHPMailer可以通过Composer进行。以下是安装和配置的步骤:
打开终端或命令行界面。
切换到你的ThinkPHP5项目的根目录。
运行以下命令来安装PHPMailer:
- composer require phpmailer/phpmailer
复制代码 安装完成后,在你的ThinkPHP5项目中,你可以这样使用PHPMailer:
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
-
- // 实例化PHPMailer对象
- $mail = new PHPMailer(true);
-
- try {
- //Server settings
- $mail->isSMTP(); // 使用SMTP
- $mail->Host = 'smtp.example.com'; // SMTP服务器地址
- $mail->SMTPAuth = true; // 启用SMTP认证
- $mail->Username = 'user@example.com'; // SMTP 用户名
- $mail->Password = 'secret'; // SMTP 密码
- $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 启用加密方法
- $mail->Port = 465; // SMTP端口
-
- //Recipients
- $mail->setFrom('from@example.com', 'Mailer');
- $mail->addAddress('to@example.com', 'Joe User'); // 添加接收者
-
- //Content
- $mail->isHTML(true); // 设置内容为HTML
- $mail->Subject = 'Subject Text';
- $mail->Body = '<b>Message body</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. Mailer Error: {$mail->ErrorInfo}";
- }
复制代码 |
|