MailApi.php 2.3 KB
<?php

namespace App\Http\Controllers;

use App\Models\Email;
use App\Models\Host;
use Helper\Mail\Imap;
use Helper\Mail\Mail;

/**
 * 提供邮件各项数据
 * @author:dc
 * @time 2023/2/4 11:18
 * Class MailApi
 * @package App\Http\Controllers
 */
class MailApi
{

    /**
     * 添加新的邮箱
     * @author:dc
     * @time 2023/2/4 15:37
     */
    public function login(){
//        $mail,$password,$imap,$smtp
        $formData = request()->only(['email','password','imap','smtp']);
        $validator = validator($formData,[
            'email'  =>  ['required','email'],
            'password'  =>  ['required','min:8','max:32'],
            'imap'  =>  ['required'],
            'smtp'  =>  ['required'],
        ],[

        ]);

        if($validator->fails()){
            return res()
                ->message($validator->errors()->first())
                ->status(400)
                ->toJson();
        }

        // host
        $model = Email::_first($formData['email']);
        if(!$model){
            $model = new Email();
            $model->email = $formData['email'];
        }

        $model->imap = $formData['imap'];
        $model->smtp = $formData['smtp'];
        $model->status = Email::STATUS_ACTIVE;
        $model->password = @base64_encode($formData['password']);

        try {
            Mail::login($model->email,$model->password,$model->imap);
        }catch (\Throwable $e){
            return res()
                ->message($e->getMessage())
                ->status(400)
                ->toJson();
        }

        // 登录成功了,密码验证字段通过
        $model->pwd_error = 0;
        // 保存好邮箱
        $model->save();

        // 设置上id,方便后面使用
        Mail::$client[$model->email]->setId($model->id);

        // 开始同步文件夹
//        $folder = Mail::syncFolder($model->email);

        return res()
            ->data([
                'token' => token_en($model->id.','.$model->email.','.time())
            ])
            ->toJson();
    }


    /**
     * 读取服务器上已记录的各个邮箱的服务器地址
     * @author:dc
     * @time 2023/2/4 15:12
     */
    public function host(){

        $host = Host::_all();

        res()->data($host)->throw();

    }



    public function lists(){

    }

}