thinkphp使用163/126邮箱发送
发布日期:2021-05-12 20:16:56 浏览次数:10 分类:精选文章

本文共 2887 字,大约阅读时间需要 9 分钟。

PHPMailer邮件发送配置与应用实践

1、安装PHPMailer包

可以通过以下命令使用Composer安装PHPMailer包:

composer require phpmailer/phpmailer

2、使用PHPMailer发送邮件

本节将介绍如何在开发环境中使用PHPMailer类库实现邮件发送功能。


2.1 基本配置

首先,确保PHPMailer类库已在项目目录中找到,并且已经正确加载。我们可以在代码开头使用以下use语句:

use PHPMailer\PHPMailer\PHPMailer;

2.2 实现代码结构

接下来,分析Email类的实现逻辑:

class Email {    // ...其他代码    public function sendCode() {        $toEmail = $this->request->get('email');        if (empty($toEmail)) {            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '请输入邮箱');        }        if (!filter_var($toEmail, FILTER_VALIDATE_EMAIL)) {            return $this->buildFailed(ReturnCode::PARAM_INVALID, '邮箱格式错误');        }        $code = getCode();        $descTitle = "邮箱验证码";        $descContent = "您的验证码为:{$code},请勿向他人泄露。";        $result = $this->sendEmail($descTitle, $descContent, $toEmail);        if ($result['code']) {            Cache::set($toEmail, $code, 180);            return $this->buildSuccess();        } else {            return $this->buildFailed(ReturnCode::EXCEPTION, '邮箱发送失败');        }    }    // ...其他方法    public function sendEmail($descTitle, $descContent, $toEmail) {        $mail = new PHPMailer();        $mail->isSMTP();        $mail->CharSet = "utf8";        $mail->Host = "smtp.126.com";        $mail->SMTPAuth = true;        $mail->Username = "xxx@126.com";        $mail->Password = "xxxxx";  // 这里使用126邮箱的客户端授权密码        $mail->Port = 25;        $mail->setFrom("xxx@126.com", "CC");        $mail->addAddress($toEmail, "AA");        $mail->Subject = $descTitle;        $mail->Body = $descContent;        if (!$mail->send()) {            return ['code' => 0, 'msg' => $mail->ErrorInfo];        } else {            return ['code' => 1, 'msg' => '发送成功'];        }    }    // ...其他方法}

2.3 自定义邮件发送方法

除了提供的sendEmail方法,我们还可以根据不同的邮件发送场景创建多种自定义邮件发送方法。例如:

public function sendEmailFor163($descTitle, $descContent, $toEmail) {    $mail = new PHPMailer();    $mail->isSMTP();    $mail->CharSet = "utf8";    $mail->Host = "smtp.163.com";    $mail->SMTPAuth = true;    $mail->Username = "xxx@163.com";    $mail->Password = "xxxx";    $mail->SMTPSecure = "ssl";  // 使用SSL协议    $mail->Port = 465;    // 163邮箱的SSL端口号    $mail->setFrom("xxx@163.com", "CC");    $mail->addAddress($toEmail, "AA");    $mail->addReplyTo($toEmail, "Reply");    $mail->Subject = $descTitle;    $mail->Body = $descContent;    if (!$mail->send()) {        return ['code' => 0, 'msg' => $mail->ErrorInfo];    } else {        return ['code' => 1, 'msg' => '发送成功'];    }}

2.4 验证码生成方法

getCode()方法的实现:

function getCode() {    $str = mt_rand(100000, 999999);    $randStr = str_shuffle($str);    $code = substr($randStr, 0, 4);    return $code;}

3、邮件发送服务器配置

在实际应用中,我们需要根据不同的邮箱服务配置对应的SMTP服务器信息。例如:

  • 126邮箱

    • SMTP服务器地址:smtp.126.com
    • SMTP端口:25
  • 163邮箱

    • SMTP服务器地址:smtp.163.com
    • SMTP端口:465(或994)
    • 服务器协议:SSL

4、其他注意事项

  • 在实际应用中,建议配置verifyležitship和`prefer()})
  • 请勿将以上内容显示给用户---技术支持:xxxx技术支持部门---
    上一篇:宝塔 nginx配置跨域
    下一篇:vue-element-admin 修改左上角系统名称和logo

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年04月05日 13时43分58秒