<?php namespace Lib\Mail; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; /** * 函数 * @time 2022/8/1 16:02 * Class MailFun * @package App\Mail\lib */ class MailFun { /** * json encode * @param $data * @param int $option * @return false|string * @time 2022/8/2 15:57 */ public static function json_en($data,$option=\JSON_UNESCAPED_UNICODE){ return \json_encode($data,$option); } /** * 解码 * @param $string * @param string $charset * @return string * @time 2022/8/15 9:31 */ public static function decodeMimeStr($string, $charset = 'utf-8') { $newString = ''; $elements = imap_mime_header_decode($string); // print_r($elements); for($i = 0; $i < count($elements); $i++) { if($elements[$i]->charset == 'default') { $elements[$i]->charset = 'iso-8859-1'; } $newString .= self::convertStringEncoding($elements[$i]->text, $elements[$i]->charset, $charset); } return $newString; } public static function convertStringEncoding($string, $fromEncoding, $toEncoding) { $convertedString = null; if($string && $fromEncoding != $toEncoding) { $convertedString = @iconv($fromEncoding, $toEncoding . '//IGNORE', $string); if(!$convertedString && extension_loaded('mbstring')) { $convertedString = @mb_convert_encoding($string, $toEncoding, $fromEncoding); } } return $convertedString ?: $string; } /** * 验证是否有附件 BODYSTRUCTURE值 * @param array $BODYSTRUCTURE * @return int * @author:dc * @time 2022/11/1 10:57 */ public static function isFile(array $BODYSTRUCTURE):int { // foreach ($BODYSTRUCTURE as $item){ // if($item[0] === 'APPLICATION'){ // return 1; // } // } // return 0; $json = json_encode($BODYSTRUCTURE); return strpos($json,'"attachment"')!==false; } /** * 邮件收件人/发件人 * @param $str * @return array * @author:dc * @time 2022/11/8 9:36 */ public static function toOrFrom($str){ $strs = explode(',',$str); foreach ($strs as $k=>$s){ preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',$s,$email); if(empty($email[0])){ $s = [ 'email' => '', 'name' => $s ]; }else{ $s = str_replace([$email[0],'"','<','>','>','<'],'',$s); $s = trim($s); $s = [ 'email' => $email[0], 'name' => $s ]; } if(empty($s['name'])){ $s['name'] = explode('@',$s['email'])[0]??''; } if(!empty($s['email'])){ $strs[$k] = $s; }else{ unset($strs[$k]); } } return $strs; } /** * @param string $smtp smtp服务器地址 * @param string $username 发件人 * @param string $password 发件人密码 * @param string $nickname 昵称 * @param string|array $to_email 收件人,邮件或['email'=>'','name'=>''] * @param string $subject 标题,主题 * @param string $body 文本内容 * @param array $files 文件 ['origin_name'=>'','path'=>''] * @param false $receipt 是否回执 * @param int $priority 是否紧急 1紧急 3正常 5慢 * @return bool * @throws \PHPMailer\PHPMailer\Exception * @author:dc * @time 2022/11/11 14:26 */ public static function sendEmail(string $smtp,string $username,string $password,string $nickname,$to_email,string $subject,string $body,$files=[],$receipt=false,$priority=3){ // 邮件对象 $mail = new PHPMailer(true); //Server settings $mail->SMTPDebug = SMTP::DEBUG_CLIENT;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = $smtp; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = $username; //SMTP username $mail->Password = $password; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` $mail->CharSet = 'utf-8'; $mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE; //Recipients,设置发件人 $mail->setFrom($username, $nickname);// 显示邮件来自谁 // //Add a recipient,设置收件人 这里必须是一对一发送 if(is_array($to_email)){ $mail->addAddress($to_email['email'], $to_email['name']); }else{ $mail->addAddress($to_email, ''); } // //回复到那个邮件 // $mail->addAddress($reply_to['email'], $reply_to['name']); //Add a recipient // // 抄送 // $mail->addCC($cc['email'],$cc['name']);// // // 密送 // $mail->addBCC($bcc['email'],$bcc['name']); //Attachments 附件 if($files){ foreach ($files as $file){ // 添加到邮箱中 $mail->addAttachment($file['path'], $file['origin_name']); //Add attachments } } // 回执,阅读后收回执的邮箱 if($receipt){ $mail->ConfirmReadingTo = $receipt; } // 是否紧急邮件 // Options: null (default), 1 = High, 3 = Normal, 5 = low. $mail->Priority = $priority; //Content 主题,标题 $mail->Subject = $subject; $mail->isHTML(true); //Set email format to HTML $mail->Body = $body;// html格式的内容 // 发送 if($mail->send()){ return true; } throw new \Exception($mail->ErrorInfo,500); } }