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; //用于存放phpmailer对象
- 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->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
- $this->mail->isSMTP(); //Send using SMTP
- $this->mail->CharSet = 'UTF-8';
- $this->mail->Host = $smtp_config['host']; //Set the SMTP server to send through
- $this->mail->SMTPAuth = true; //Enable SMTP authentication
- $this->mail->Username = $this->send_mail; //SMTP username
- $this->mail->Password = $smtp_config['password']; //SMTP password
- $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
- $this->mail->Port = $smtp_config['port'];
- }
- /**
- * 邮件发送
- * @param $toAddress 接受邮件的邮箱地址
- * @param $subject 邮件标题
- * @param $body 邮件内容
- * @param $isHtml 邮件内容是否是html,默认为否
- * @return Array 返回参数
- */
- public function send($toAddress,$subject,$body,$isHtml=false){
- try {
- //Recipients
- $this->mail->setFrom($this->send_mail);
- $this->mail->addAddress($toAddress); //Add a recipient
- //Content
- $this->mail->isHTML($isHtml); //Set email format to HTML
- $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;
- }
- }
- }
|