//下载PHPMailer并开启php_openssl、php_socket扩展 include_once 'PHPMailer.php'; include_once 'SMTP.php'; $mail = new PHPMailer(); //实例化 $mail->IsSMTP(); // 启用SMTP $mail->Host = "smtp.yeah.net"; //SMTP服务器 以163邮箱为例子 $mail->Port = 25; //邮件发送端口 $mail->SMTPAuth = true; //启用SMTP认证 $mail->CharSet = "UTF-8"; //字符集 $mail->Encoding = "base64"; //编码方式 $mail->Username = "phpmaster@yeah.net"; //你的邮箱 $mail->Password = "EFWWFULFELKCUMQY"; //网易邮箱客户端授权码 $mail->From = "phpmaster@yeah.net"; //发件人地址(也就是你的邮箱) $mail->FromName = "张某某"; //发件人姓名 $address = "137647337@qq.com";//收件人email $mail->AddAddress($address, "亲");//添加收件人(地址,昵称) //附件 /*$mail->AddAttachment("logo.jpg"); $mail->AddAttachment("1.zip"); $mail->AddAttachment('1.xls','我的附件.xls'); // 添加附件,并指定名称*/ $mail->IsHTML(true); //支持html格式内容 //$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片 $mail->Subject = "你好"; //邮件标题 $mail->Body = '这是HTML邮件正文粗体,<a href="http://www.zhangxuhui.com">激活</a>'; //邮件主体内容 //发送 if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "success";
场景:邮箱发送验证码类在php7.3+环境中报错:
filter_var(): explicit use of FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED is deprecated
原因:查资料得知,php7.3+弃用了FILTER_FLAG_SCHEME_REQUIRED函数
解决方式:
PHPMailer.php 3599行 if (filter_var('http://' . $host, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { //Is it a syntactically valid hostname? return true; } 更改为: if (preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is','http://' . $host)) { //Is it a syntactically valid hostname? return true; }
其实就是url地址地址的正则验证。