mirror of https://gitee.com/karson/fastadmin.git
parent
30c695ff61
commit
290bfc4f75
|
|
@ -27,7 +27,7 @@ class Email
|
||||||
*/
|
*/
|
||||||
public $options = [
|
public $options = [
|
||||||
'charset' => 'utf-8', //编码格式
|
'charset' => 'utf-8', //编码格式
|
||||||
'debug' => 0, //调式模式
|
'debug' => false, //调式模式
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -55,14 +55,17 @@ class Email
|
||||||
$this->options = array_merge($this->options, $config);
|
$this->options = array_merge($this->options, $config);
|
||||||
}
|
}
|
||||||
$this->options = array_merge($this->options, $options);
|
$this->options = array_merge($this->options, $options);
|
||||||
vendor('phpmailer.phpmailer.PHPMailerAutoload');
|
|
||||||
$securArr = [1 => 'tls', 2 => 'ssl'];
|
$securArr = [1 => 'tls', 2 => 'ssl'];
|
||||||
|
|
||||||
$this->mail = new \PHPMailer(true);
|
$this->mail = new \PHPMailer\PHPMailer\PHPMailer(true);
|
||||||
$this->mail->CharSet = $this->options['charset'];
|
$this->mail->CharSet = $this->options['charset'];
|
||||||
$this->mail->SMTPDebug = $this->options['debug'];
|
if ($this->options['mail_type'] == 1) {
|
||||||
$this->mail->isSMTP();
|
$this->mail->SMTPDebug = $this->options['debug'];
|
||||||
$this->mail->SMTPAuth = true;
|
$this->mail->isSMTP();
|
||||||
|
$this->mail->SMTPAuth = true;
|
||||||
|
} else {
|
||||||
|
$this->mail->isMail();
|
||||||
|
}
|
||||||
$this->mail->Host = $this->options['mail_smtp_host'];
|
$this->mail->Host = $this->options['mail_smtp_host'];
|
||||||
$this->mail->Username = $this->options['mail_from'];
|
$this->mail->Username = $this->options['mail_from'];
|
||||||
$this->mail->Password = $this->options['mail_smtp_pass'];
|
$this->mail->Password = $this->options['mail_smtp_pass'];
|
||||||
|
|
@ -75,54 +78,117 @@ class Email
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置邮件主题
|
* 设置邮件主题
|
||||||
* @param string $subject
|
* @param string $subject 邮件主题
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function subject($subject)
|
public function subject($subject)
|
||||||
{
|
{
|
||||||
$this->options['subject'] = $subject;
|
$this->mail->Subject = $subject;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置发件人
|
* 设置发件人
|
||||||
* @param string $email
|
* @param string $email 发件人邮箱
|
||||||
* @param string $name
|
* @param string $name 发件人名称
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function from($email, $name = '')
|
public function from($email, $name = '')
|
||||||
{
|
{
|
||||||
$this->options['from'] = $email;
|
$this->mail->setFrom($email, $name);
|
||||||
$this->options['from_name'] = $name;
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置收件人
|
* 设置收件人
|
||||||
* @param string $email
|
* @param mixed $email 收件人,多个收件人以,进行分隔
|
||||||
* @param string $name
|
* @param string $name 收件人名称
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function to($email, $name = '')
|
public function to($email, $name = '')
|
||||||
{
|
{
|
||||||
$this->options['to'] = $email;
|
$emailArr = $this->buildAddress($email);
|
||||||
$this->options['to_name'] = $name;
|
foreach ($emailArr as $address => $name) {
|
||||||
|
$this->mail->addAddress($address, $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置抄送
|
||||||
|
* @param mixed $email 收件人,多个收件人以,进行分隔
|
||||||
|
* @param string $name 收件人名称
|
||||||
|
* @return Email
|
||||||
|
*/
|
||||||
|
public function cc($email, $name = '')
|
||||||
|
{
|
||||||
|
$emailArr = $this->buildAddress($email);
|
||||||
|
foreach ($emailArr as $address => $name) {
|
||||||
|
$this->mail->addCC($address, $name);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置密送
|
||||||
|
* @param mixed $email 收件人,多个收件人以,进行分隔
|
||||||
|
* @param string $name 收件人名称
|
||||||
|
* @return Email
|
||||||
|
*/
|
||||||
|
public function bcc($email, $name = '')
|
||||||
|
{
|
||||||
|
$emailArr = $this->buildAddress($email);
|
||||||
|
foreach ($emailArr as $address => $name) {
|
||||||
|
$this->mail->addBCC($address, $name);
|
||||||
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置邮件正文
|
* 设置邮件正文
|
||||||
* @param string $body
|
* @param string $body 邮件下方
|
||||||
* @param boolean $ishtml
|
* @param boolean $ishtml 是否HTML格式
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function message($body, $ishtml = true)
|
public function message($body, $ishtml = true)
|
||||||
{
|
{
|
||||||
$this->options['body'] = $body;
|
if ($ishtml) {
|
||||||
$this->options['ishtml'] = $ishtml;
|
$this->mail->msgHTML($body);
|
||||||
|
} else {
|
||||||
|
$this->mail->Body = $body;
|
||||||
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加附件
|
||||||
|
* @param string $path 附件路径
|
||||||
|
* @param string $name 附件名称
|
||||||
|
* @return Email
|
||||||
|
*/
|
||||||
|
public function attachment($path, $name = '')
|
||||||
|
{
|
||||||
|
$this->mail->addAttachment($path, $name);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建Email地址
|
||||||
|
* @param mixed $emails Email数据
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function buildAddress($emails)
|
||||||
|
{
|
||||||
|
$emails = is_array($emails) ? $emails : explode(',', str_replace(";", ",", $emails));
|
||||||
|
$result = [];
|
||||||
|
foreach ($emails as $key => $value) {
|
||||||
|
$email = is_numeric($key) ? $value : $key;
|
||||||
|
$result[$email] = is_numeric($key) ? "" : $value;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取最后产生的错误
|
* 获取最后产生的错误
|
||||||
* @return string
|
* @return string
|
||||||
|
|
@ -148,38 +214,17 @@ class Email
|
||||||
public function send()
|
public function send()
|
||||||
{
|
{
|
||||||
$result = false;
|
$result = false;
|
||||||
switch ($this->options['mail_type']) {
|
if (in_array($this->options['mail_type'], [1, 2])) {
|
||||||
case 1:
|
try {
|
||||||
//使用phpmailer发送
|
$result = $this->mail->send();
|
||||||
$this->mail->setFrom($this->options['from'], $this->options['from_name']);
|
} catch (\PHPMailer\PHPMailer\Exception $e) {
|
||||||
$this->mail->addAddress($this->options['to'], $this->options['to_name']);
|
$this->setError($e->getMessage());
|
||||||
$this->mail->Subject = $this->options['subject'];
|
}
|
||||||
if ($this->options['ishtml']) {
|
|
||||||
$this->mail->msgHTML($this->options['body']);
|
|
||||||
} else {
|
|
||||||
$this->mail->Body = $this->options['body'];
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
$result = $this->mail->send();
|
|
||||||
} catch (\phpmailerException $e) {
|
|
||||||
$this->setError($e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setError($result ? '' : $this->mail->ErrorInfo);
|
$this->setError($result ? '' : $this->mail->ErrorInfo);
|
||||||
break;
|
} else {
|
||||||
case 2:
|
//邮件功能已关闭
|
||||||
//使用mail方法发送邮件
|
$this->setError(__('Mail already closed'));
|
||||||
$headers = 'MIME-Version: 1.0' . "\r\n";
|
|
||||||
$headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
|
|
||||||
$headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
|
|
||||||
$headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
|
|
||||||
$result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
|
|
||||||
$this->setError($result ? '' : error_get_last()['message']);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
//邮件功能已关闭
|
|
||||||
$this->setError(__('Mail already closed'));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
"endroid/qr-code": "^1.9",
|
"endroid/qr-code": "^1.9",
|
||||||
"topthink/think-captcha": "^1.0",
|
"topthink/think-captcha": "^1.0",
|
||||||
"mtdowling/cron-expression": "^1.2",
|
"mtdowling/cron-expression": "^1.2",
|
||||||
"phpmailer/phpmailer": "^5.2",
|
"phpmailer/phpmailer": "~6.0.6",
|
||||||
"karsonzhang/fastadmin-addons": "~1.1.9",
|
"karsonzhang/fastadmin-addons": "~1.1.9",
|
||||||
"overtrue/pinyin": "~3.0",
|
"overtrue/pinyin": "~3.0",
|
||||||
"phpoffice/phpspreadsheet": "^1.2"
|
"phpoffice/phpspreadsheet": "^1.2"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue