插件窝 干货文章 thinkphp5使用phpmail发送qq邮件的实现过程

thinkphp5使用phpmail发送qq邮件的实现过程

mail email gt return 854    来源:    2024-10-29

1、使用composer 工具安装,在tp5根目录下执行

composer require phpmailer/phpmailer

安装成功后显示下面目录

2、封装发送邮件方法

function sendEmail($to, $title, $content){
 
    $email=[
         "send_email"=>"231323@qq.com",
        "host"=>"smtp.qq.com",
        "pwd"=>"vrgtobnx2232aogugaaf",
        "encry_type"=> "ssl",
        "port"=>465,
        "send_name"=>"测试",
        "title"=>"QQ邮箱"
    ];
 
    try {
        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        $mail->isSMTP();
        $mail->Host = $email['host'];  //qq邮箱的服务器地址
        $mail->SMTPAuth = true;
        $mail->Username = $email['send_email'];//授权的qq邮箱
        $mail->Password = $email['pwd'];//授权码,不是密码!!!
        $mail->SMTPSecure = $email['encry_type'];// 使用 ssl 加密方式登录boolean
        $mail->Port = $email['port'];//smtp 服务器的远程服务器端口号
        //Recipients
        $mail->setFrom($email['send_email'], $email['send_name']);//授权的qq邮箱(和上面一样),自己起的昵称
        $mail->addAddress($to);     // 传过来的收件人
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $title;//传过来的标题
        $mail->Body = $content;//传过来的内容
        if($mail->send()){
            return return_msg();
        }else{
            return return_msg(-1,"error");
        }
    } catch (\PHPMailer\PHPMailer\Exception $e) {
        return return_msg(-1,$mail->ErrorInfo);
    }
}
 
  function return_msg($status=1,$msg="ok",$data=[]){
    return [
        "status" => $status,
        "msg" => $msg,
        "data" => $data,
    ];
 
}

3、控制器中调用

function sendEmail($to, $title, $content){
 
    $email=[
         "send_email"=>"231323@qq.com",
        "host"=>"smtp.qq.com",
        "pwd"=>"vrgtobnx2232aogugaaf",
        "encry_type"=> "ssl",
        "port"=>465,
        "send_name"=>"测试",
        "title"=>"QQ邮箱"
    ];
 
    try {
        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        $mail->isSMTP();
        $mail->Host = $email['host'];  //qq邮箱的服务器地址
        $mail->SMTPAuth = true;
        $mail->Username = $email['send_email'];//授权的qq邮箱
        $mail->Password = $email['pwd'];//授权码,不是密码!!!
        $mail->SMTPSecure = $email['encry_type'];// 使用 ssl 加密方式登录boolean
        $mail->Port = $email['port'];//smtp 服务器的远程服务器端口号
        //Recipients
        $mail->setFrom($email['send_email'], $email['send_name']);//授权的qq邮箱(和上面一样),自己起的昵称
        $mail->addAddress($to);     // 传过来的收件人
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $title;//传过来的标题
        $mail->Body = $content;//传过来的内容
        if($mail->send()){
            return return_msg();
        }else{
            return return_msg(-1,"error");
        }
    } catch (\PHPMailer\PHPMailer\Exception $e) {
        return return_msg(-1,$mail->ErrorInfo);
    }
}
 
  function return_msg($status=1,$msg="ok",$data=[]){
    return [
        "status" => $status,
        "msg" => $msg,
        "data" => $data,
    ];
 
}

4、运行后结果

到此这篇关于thinkphp5使用phpmail发送qq邮件的实现过程的文章就介绍到这了,更多相关thinkphp5 phpmail发送qq邮件内容请搜索插件窝以前的文章或继续浏览下面的相关文章希望大家以后多多支持插件窝!