注意:使用个人qq邮箱发送邮箱会被腾讯拦截发送失败
第一:163邮箱配置
1、登录163邮箱:https://email.163.com/
2、在邮箱的设置中开启SMTP服务(设置->POP3/SMTP/IMAP->开启服务),同时生成授权密码(发送邮件需要),如下图
第二:QQ企业邮箱配置
1、注册登录QQ企业邮箱:https://exmail.qq.com/onlinesell/intro
2、在邮箱的设置中开启SMTP服务(设置->收发信设置->POP3/SMTP/IMAP->开启服务),同时生成授权密码(发送邮件需要),如下图
3、在邮箱的设置->邮箱绑定中生成授权密码(发送邮件需要),如下图
第三:下载phpmailer的邮件代码压缩包
第四:编写邮件发送类,基于PHPMailer类
<?phpnamespace framework\ext;/*** 邮件发送类,基于PHPMailer类* Class Email* @package framework\ext*/
class Email{static public $config;//存储配置的静态变量//设定邮件参数static public function init($config = array()){//网易//self::$config['SMTP_HOST']=isset($config['SMTP_HOST'])?$config['SMTP_HOST']:'smtp.163.com';//smtp服务器地址//self::$config['SMTP_PORT']=isset($config['SMTP_PORT'])?$config['SMTP_PORT']:25;//smtp服务器端口//self::$config['SMTP_SSL']=isset($config['SMTP_SSL'])?$config['SMTP_SSL']:false;//是否启用SSL安全连接 ,gmail需要启用sll安全连接//self::$config['SMTP_USERNAME']=isset($config['SMTP_USERNAME'])?$config['SMTP_USERNAME']:'gzzzwhzxxx@163.com';//smtp服务器帐号,如:你的邮箱名称//self::$config['SMTP_PASSWORD']=isset($config['SMTP_PASSWORD'])?$config['SMTP_PASSWORD']:'QJTJDOPROZMYPCSM';//smtp服务器帐号授权码(授权密码)//self::$config['SMTP_AUTH']=isset($config['SMTP_AUTH'])?$config['SMTP_AUTH']:true;//启用SMTP验证功能,一般需要开启//self::$config['SMTP_CHARSET']=isset($config['SMTP_CHARSET'])?$config['SMTP_CHARSET']:'utf-8';//发送的邮件内容编码//self::$config['SMTP_FROM_TO']=isset($config['SMTP_FROM_TO'])?$config['SMTP_FROM_TO']:'gzzzwhzxxx@163.com';//发件人邮件地址//self::$config['SMTP_FROM_NAME']=isset($config['SMTP_FROM_NAME'])?$config['SMTP_FROM_NAME']:'某某人/公司';//发件人姓名//self::$config['SMTP_DEBUG']=isset($config['SMTP_DEBUG'])?$config['SMTP_DEBUG']:false;//是否显示调试信息//QQ企业邮箱self::$config['SMTP_HOST']=isset($config['SMTP_HOST'])?$config['SMTP_HOST']:'smtp.exmail.qq.com';//smtp服务器地址self::$config['SMTP_PORT']=isset($config['SMTP_PORT'])?$config['SMTP_PORT']:465;//smtp服务器端口self::$config['SMTP_SSL']=isset($config['SMTP_SSL'])?$config['SMTP_SSL']:true;//是否启用SSL安全连接 ,gmail需要启用sll安全连接self::$config['SMTP_USERNAME']=isset($config['SMTP_USERNAME'])?$config['SMTP_USERNAME']:'xxxx@hzmba.com';//smtp服务器(邮箱)帐号self::$config['SMTP_PASSWORD']=isset($config['SMTP_PASSWORD'])?$config['SMTP_PASSWORD']:'wk4AcGbWc5nqbMZ3';//smtp服务器(邮箱)帐号对应的客户端授权码self::$config['SMTP_AUTH']=isset($config['SMTP_AUTH'])?$config['SMTP_AUTH']:true;//启用SMTP验证功能,一般需要开启self::$config['SMTP_CHARSET']=isset($config['SMTP_CHARSET'])?$config['SMTP_CHARSET']:'utf-8';//发送的邮件内容编码self::$config['SMTP_FROM_TO']=isset($config['SMTP_FROM_TO'])?$config['SMTP_FROM_TO']:'xx@hzmba.com';//发件人邮件地址self::$config['SMTP_FROM_NAME']=isset($config['SMTP_FROM_NAME'])?$config['SMTP_FROM_NAME']:'某某公司';//发件人姓名self::$config['SMTP_DEBUG']=isset($config['SMTP_DEBUG'])?$config['SMTP_DEBUG']:false;//是否显示调试信息}//发送邮件static public function send($mail_to,$mail_subject,$mail_body,$mail_attach=NULL){@error_reporting(E_ERROR | E_WARNING | E_PARSE);//屏蔽出错信息require_once(dirname(__FILE__).'/phpmailer/class.phpmailer.php');$mail = new \PHPMailer();//没有调用配置方法,则调用一次config方法if(!isset(self::$config)||empty(self::$config)){self::config();}$mail->IsSMTP(); 使用SMTP方式发送$mail->Host = self::$config['SMTP_HOST']; //smtp服务器地址$mail->Port = self::$config['SMTP_PORT']; //smtp服务器端口$mail->Username = self::$config['SMTP_USERNAME']; //smtp服务器帐号,$mail->Password = self::$config['SMTP_PASSWORD']; // smtp服务器帐号密码$mail->SMTPAuth = self::$config['SMTP_AUTH'];//启用SMTP验证功能,一般需要开启$mail->CharSet = self::$config['SMTP_CHARSET'];//发送的邮件内容编码 $mail->SetFrom(self::$config['SMTP_FROM_TO'], self::$config['SMTP_FROM_NAME']); // 发件人的邮箱和姓名$mail->AddReplyTo(self::$config['SMTP_FROM_TO'],self::$config['SMTP_FROM_NAME']);// 回复时的邮箱和姓名,一般跟发件人一样//是否启用SSL安全连接 if(self::$config['SMTP_SSL']){$mail->SMTPSecure = "ssl"; //gmail需要启用sll安全连接}//开启调试信息if(self::$config['SMTP_DEBUG']){$mail->SMTPDebug = 1; }$mail->Subject = $mail_subject;//邮件标题$mail->MsgHTML($mail_body);//邮件内容,支持html代码//发送邮件if(is_array($mail_to)){//同时发送给多个人foreach($mail_to as $key=>$value){$mail->AddAddress($value,""); // 收件人邮箱和姓名}}else{ //只发送给一个人$mail->AddAddress($mail_to,""); // 收件人邮箱和姓名}//发送多个附件if(is_array($mail_attach)){foreach($mail_attach as $value){if(file_exists($value))//附件必须存在,才会发送{$mail->AddAttachment($value); // attachment}}}//发送一个附件if(!empty($mail_attach)&&is_string($mail_attach)){if(file_exists($mail_attach))//附件必须存在,才会发送{$mail->AddAttachment($mail_attach); //发送附件}}if(!$mail->Send()) {if(self::$config['SMTP_DEBUG']){echo "Mailer Error: " . $mail->ErrorInfo;}return false; } else {return true;}}}
第五:调用邮件发送类发送邮件
/*** 发送邮件* @param $mailto 收件的邮箱* @param $sendcontent* @return bool* @throws \phpmailerException*/public function sendmail($mailto,$sendcontent){//$sendcontent = [// "subject"=>"某某 xxx@qq.com,老师你好有来自 某某的留言",// "body" =>'留言时间: 2020-05-28 14:48:59;留言人电话:<a href="tel:$msg_tel"> 13566385387</a>;留言内容: ;留言链接: '//];\framework\ext\Email::init();var_dump($sendcontent);die();$rst = \framework\ext\Email::send( $mailto,$sendcontent['subject'],$sendcontent['body']);return $rst;//成功true,失败false}
第六:接收到邮箱内容