<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: sai <1430792918@qq.com>
// +----------------------------------------------------------------------
namespace plugin\saiadmin\service;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use plugin\saiadmin\app\logic\system\SystemConfigLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Arr;

/**
 * 邮件服务类
 */
class EmailService
{
    /**
     * 读取配置
     * @return array
     */
    public static function getConfig(): array
    {
        $logic = new SystemConfigLogic();
        $config = $logic->getGroup('email_config');
        if (!$config) {
            throw new ApiException('Mail config not set');
        }
        return $config;
    }

    /**
     * Get Mailer
     * @return PHPMailer
     */
    public static function getMailer(): PHPMailer
    {
        if (!class_exists(PHPMailer::class)) {
            throw new ApiException('Please run composer require phpmailer/phpmailer and restart');
        }
        $config = static::getConfig();
        $mailer = new PHPMailer();
        $mailer->SMTPDebug = intval(Arr::getConfigValue($config,'SMTPDebug'));
        $mailer->isSMTP();
        $mailer->Host = Arr::getConfigValue($config,'Host');
        $mailer->SMTPAuth = true;
        $mailer->CharSet = Arr::getConfigValue($config,'CharSet');
        $mailer->Username = Arr::getConfigValue($config,'Username');
        $mailer->Password = Arr::getConfigValue($config,'Password');
        $mailer->SMTPSecure = Arr::getConfigValue($config,'SMTPSecure');
        $mailer->Port = Arr::getConfigValue($config,'Port');
        return $mailer;
    }

    /**
     * 发送邮件
     * @param $from
     * @param $to
     * @param $subject
     * @param $content
     * @return string
     * @throws Exception
     */
    public static function send($from, $to, $subject, $content): string
    {
        $mailer = static::getMailer();
        call_user_func_array([$mailer, 'setFrom'], (array)$from);
        call_user_func_array([$mailer, 'addAddress'], (array)$to);
        $mailer->Subject = $subject;
        $mailer->isHTML(true);
        $mailer->Body = $content;
        $mailer->send();
        return $mailer->ErrorInfo;
    }

    /**
     * 按照模版发送
     * @param string|array $to
     * @param $subject
     * @param $content
     * @param array $templateData
     * @return string
     * @throws Exception
     */
    public static function sendByTemplate($to, $subject, $content, array $templateData = []): string
    {
        if ($templateData) {
            $search = [];
            foreach ($templateData as $key => $value) {
                $search[] = '{' . $key . '}';
            }
            $content = str_replace($search, array_values($templateData), $content);
        }
        $config = static::getConfig();
        return static::send([Arr::getConfigValue($config,'From'), Arr::getConfigValue($config,'FromName')], $to, $subject, $content);
    }
    
}