| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- /**
- * Created by PhpStorm.
- * User: chendeben
- * Date: 2018/8/10
- * Time: 上午11:22
- */
- namespace Heanup\Library;
- use Heanup\App\Api\Config\GlobalConfig;
- use Heanup\Frame\Logger;
- use PHPMailer\PHPMailer\Exception;
- use PHPMailer\PHPMailer\PHPMailer;
- class Email
- {
- private static $instance;
- private $mail;
- public function __construct()
- {
- $config = GlobalConfig::getData();
- $this->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;
- }
- }
|