1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Com;
- use EasySwoole\EasySwoole\Config;
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\SMTP;
- use PHPMailer\PHPMailer\Exception;
- use EasySwoole\EasySwoole\Logger;
- class SmtpSend
- {
- protected $mail;
- protected $send_mail;
- function __construct()
- {
- $smtp_config = Config::getInstance()->getConf('SMTP_SET');
- $this->send_mail = $smtp_config['username'];
- $this->mail = new PHPMailer(true);
-
- $this->mail->isSMTP();
- $this->mail->CharSet = 'UTF-8';
- $this->mail->Host = $smtp_config['host'];
- $this->mail->SMTPAuth = true;
- $this->mail->Username = $this->send_mail;
- $this->mail->Password = $smtp_config['password'];
- $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
- $this->mail->Port = $smtp_config['port'];
- }
-
- public function send($toAddress,$subject,$body,$isHtml=false){
- try {
-
- $this->mail->setFrom($this->send_mail);
- $this->mail->addAddress($toAddress);
-
- $this->mail->isHTML($isHtml);
- $this->mail->Subject = $subject;
- $this->mail->Body = $body;
- $this->mail->AltBody = '您的邮箱不支持HTML格式,请联系邮箱管理员';
- $this->mail->send();
- $notice = "smtp邮件发送成功,内容:".json_encode(['toAddress'=>$toAddress,'subject'=>$subject,'body'=>$body])."\n";
- Logger::getInstance()->notice($notice);
- return true;
- } catch (Exception $e) {
- $notice = "smtp邮件发送失败. 错误信息: {$this->mail->ErrorInfo},内容:".json_encode(['toAddress'=>$toAddress,'subject'=>$subject,'body'=>$body])."\n";
- Logger::getInstance()->notice($notice);
- return false;
- }
- }
- }
|