// +---------------------------------------------------------------------- namespace App\Services; use app\Lib\smtpMail\smtp as smtpService; use PHPMailer\PHPMailer\PHPMailer; /** * 邮件管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ConfigService * @package App\Services */ class EmailService extends BaseService { /** * 配置参数 * @var array */ protected $config = []; /** * 初始化配置 * @return bool */ public function __construct() { $config = ConfigService::make()->getConfigByGroup(3); $mail = config('mail'); $mailKey = md5(json_encode($mail)); $mail['default'] = isset($config['mail_mailer']['value']) && $config['mail_mailer']['value'] ? $config['mail_mailer']['value'] : $mail['default']; $mail['markdown']['paths'] = [resource_path('views/vendor/mail')]; $smtp = isset($mail['mailers']['smtp']) ? $mail['mailers']['smtp'] : []; $smtp['host'] = isset($config['mail_host']['value']) && $config['mail_host']['value'] ? $config['mail_host']['value'] : $smtp['host']; $smtp['port'] = isset($config['mail_port']['value']) && $config['mail_port']['value'] ? $config['mail_port']['value'] : $smtp['port']; $smtp['username'] = isset($config['mail_username']['value']) && $config['mail_username']['value'] ? $config['mail_username']['value'] : $smtp['host']; $smtp['password'] = isset($config['mail_password']['value']) && $config['mail_password']['value'] ? $config['mail_password']['value'] : $smtp['password']; $smtp['timeout'] = isset($config['mail_timeout']['value']) && $config['mail_timeout']['value'] ? $config['mail_timeout']['value'] : $smtp['timeout']; if ($mail['default'] != 'smtp') { return false; } $mail['mailers']['smtp'] = $smtp; $mail['from']['address'] = $smtp['username']; $mail['from']['name'] = 'UTC交易平台'; $this->config = array_merge($config, $smtp); if ($mailKey != md5(json_encode($mail))) { file_put_contents(base_path() . '/config/mail.php', ""); } return true; } /** * 发送邮件验证码 * @param $email * @param string $scene 场景:默认reg-注册 * @return bool */ public function sendCode($email, $scene = 'reg') { try { $cacheKey = "stores:codes:email_{$scene}:" . md5($email); // 发送频繁 if (RedisService::get($cacheKey . '_lock')) { $this->error = '1014'; // return false; } $title = isset($this->config['mail_title_' . $scene]['value']) && $this->config['mail_title_' . $scene]['value'] ? $this->config['mail_title_' . $scene]['value'] : "UTC网站注册邮箱验证码"; $template = isset($this->config['mail_template_' . $scene]['value']) && $this->config['mail_template_' . $scene]['value'] ? $this->config['mail_template_' . $scene]['value'] : "您的验证码是:{code},5分钟有效期!!!"; // 生成验证码 $code = rand(100000, 999999); $template = str_replace('{code}', $code, $template); // 邮件服务 $mail = new PHPMailer(true); $mail->CharSet ="UTF-8"; //设定邮件编码 $mail->SMTPDebug = 0; //调试模式输出:0 不输出,2 输出 $mail->isSMTP(); //使用SMTP $mail->Host = $this->config['host']; // SMTP服务器:以QQ为例 $mail->SMTPAuth = true; // 允许 SMTP 认证 $mail->Username = $this->config['username']; // SMTP用户名: 即发送方的邮箱 $mail->Password = $this->config['password']; // SMTP授权码: 即发送方的邮箱授权码 $mail->SMTPSecure = 'tls'; // 允许 TLS 或者ssl协议 $mail->Port = 25; // 服务器端口: 25 或者465 或者587 具体要看邮箱服务器支持 // 上锁 RedisService::set($cacheKey.'_lock', ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], rand(5, 10)); // 设置发送信息参数 $mail->setFrom($this->config['username'], "UTC交易平台"); //发件人:邮箱与用户名 $mail->addAddress($email, 'Mailer'); //收件人 $mail->isHTML(true); //是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容 $mail->Subject = $title; //邮件标题 $mail->Body = $template; //邮件内容 $mail->AltBody = "您的验证码是:{$code}"; //邮件内容:如果邮件客户端不支持HTML则显示此内容//*/ $mail->send(); return true; } catch (\Exception $exception){ $this->error = $exception->getMessage(); return false; } } /** * 验证邮件验证码 * @param $email * @param $code * @param string $scene 场景:默认reg-注册 * @return bool */ public function check($email, $code, $scene = 'reg') { return true; } }