找回密码
 免费注册

[ThinkPHP] thinkphp5 安装phpmailer

[复制链接]
admin 发表于 2024-4-14 19:53:25 | 显示全部楼层 |阅读模式
在ThinkPHP5中安装PHPMailer可以通过Composer进行。以下是安装和配置的步骤:
打开终端或命令行界面。
切换到你的ThinkPHP5项目的根目录。
运行以下命令来安装PHPMailer:
  1. composer require phpmailer/phpmailer
复制代码
安装完成后,在你的ThinkPHP5项目中,你可以这样使用PHPMailer:
  1. use PHPMailer\PHPMailer\PHPMailer;
  2. use PHPMailer\PHPMailer\Exception;

  3. // 实例化PHPMailer对象
  4. $mail = new PHPMailer(true);

  5. try {
  6.     //Server settings
  7.     $mail->isSMTP();                                          // 使用SMTP
  8.     $mail->Host       = 'smtp.example.com';                    // SMTP服务器地址
  9.     $mail->SMTPAuth   = true;                                 // 启用SMTP认证
  10.     $mail->Username   = 'user@example.com';                    // SMTP 用户名
  11.     $mail->Password   = 'secret';                             // SMTP 密码
  12.     $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;          // 启用加密方法
  13.     $mail->Port       = 465;                                  // SMTP端口

  14.     //Recipients
  15.     $mail->setFrom('from@example.com', 'Mailer');
  16.     $mail->addAddress('to@example.com', 'Joe User');          // 添加接收者

  17.     //Content
  18.     $mail->isHTML(true);                                      // 设置内容为HTML
  19.     $mail->Subject = 'Subject Text';
  20.     $mail->Body    = '<b>Message body</b>';
  21.     $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

  22.     $mail->send();
  23.     echo 'Message has been sent';
  24. } catch (Exception $e) {
  25.     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  26. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|Archiver|手机版|小黑屋|信息共享网

GMT+8, 2024-5-14 06:50 , Processed in 0.078477 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表