mail = new PHPMailer(); $this->mail->CharSet = 'UTF-8'; $this->mail->isSMTP(); // Set mailer to use SMTP $this->mail->Host = $config['host']; // Specify main and backup SMTP servers $this->mail->SMTPAuth = $config['auth']; // Enable SMTP authentication $this->mail->Username = $config['user']; // SMTP username $this->mail->Password = $config['pwd']; // SMTP password $this->mail->SMTPSecure = $config['secure']; // Enable TLS encryption, `ssl` also accepted $this->mail->Port = $config['port']; // TCP port to connect to try { $this->mail->setFrom($config['user'], $config['from']); } catch (Exception $e) { } $this->mail->addCustomHeader('X-Priority', '1\r\n'); } public static function instance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } /** * @param array $receipt 收件人地址 * @param string $subject 邮件标题 * @param string $body 邮件内容 * @param array $attachment 附件 * @param bool $is_html 是否HTML格式 * @return bool * @throws Exception */ public function sendMail(array $receipt, string $subject, string $body, array $attachment = array(), bool $is_html = true) { foreach ($receipt as $list) { $this->mail->addAddress($list); } try { if ($attachment) { $this->mail->addAttachment($attachment['path'], $attachment['name']); } } catch (Exception $e) { } $this->mail->isHTML($is_html); $this->mail->Subject = $subject; $this->mail->Body = $body; try { if (!$this->mail->send()) { Logger::error($this->mail->ErrorInfo, 'mail_err'); return false; } } catch (Exception $e) { Logger::error($this->mail->ErrorInfo, 'mail_err'); } return true; } }