代码中使用的utf-8编码格式,邮件内容在outlook中乱码,其他邮件上正常。
解决方法:使用mb_convert_encoding — 转换字符的编码
$mailsubject = mb_convert_encoding($mailsubject, "GBK", "UTF-8");//邮件内容
$mailbody = mb_convert_encoding($content, "GBK", "UTF-8");//邮件内容
发送邮件并校验方法:
/**
* 9、发送邮件验证码
*/
public function sendMail ()
{
$email = input('post.email'); //邮箱
$result = $this->send_mail($email);
return $result;
}
public function send_mail($email)
{
$userId = session('userId');
if ($email == ''){
return array("status" => '1', 'error' => ['请输入邮箱']);
}
$checkmail = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';//是否邮箱
if(!preg_match($checkmail,$email)){
return array("status" => '1', 'error' => ['邮箱格式不正确']);
}
$redis = Cache::connect(config::get('redis_options'));
$redisCount = $redis->get('app_count' . $email);
if (!$redisCount) {
$redisCount = 0;
}
if ($redisCount>20){
return array("status" => '1', 'error' => ['每天最多发送20条验证码']);
}
$verify_code = strval(rand(100000, 999999));
$content = '<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf8">
</head>
<body>
<h3>亲爱的用户,您好:</h3>
您正在**平台设置邮箱,验证码:'. $verify_code .',有效期为10分钟,如有问题请联系管理员!
</body>
</html>';
# 发送邮件
$smtpserver = 'smtp.***tl.net'; //SMTP服务器地址
$smtpserverport = 80; //SMTP端口
$smtpusermail = 'service@***tl.net'; //用户邮箱
$smtpemailto = $email;
$smtpuser = 'service@***tl.net'; //SMTP服务器的用户帐号
$smtppass = 'f***@2015'; //SMTP服务器的用户密码
$mailsubject = '邀请函';//邮件主题
$mailsubject = mb_convert_encoding($mailsubject, "GBK", "UTF-8");//邮件内容
$mailbody = mb_convert_encoding($content, "GBK", "UTF-8");//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new \Smtp\Smtp;
$smtp->smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//true是表示使用身份验证,否则不使用
$smtp->debug = TRUE;//是否显示发送的调试信息
$re = $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
if ($re) {
$redis->set('app_'.$userId . $smtpemailto, $verify_code, 600);
$redisCount++;
$seconds = strtotime(date("Y-m-d",strtotime("+1 day"))) - time(); //今天还剩多少秒
$redis->set('app_count' . $smtpemailto, $redisCount,$seconds);
$data['email'] = hidtel($smtpemailto);
return array("status" => '0', 'data' => $data);
} else {
return array("status" => '1', 'error' => ['邮件发送失败']);
}
}
/**
* 校验邮箱验证码
*/
public function checkEmail()
{
$email = input('post.email'); //邮箱
$verificationCode = input('post.verificationCode'); //验证码
$result = $this->check_email($email,$verificationCode);
return $result;
}
public function check_email($email,$verificationCode)
{
$userId = session('userId');
if ($email == '' or $verificationCode == '') {
return array("status" => '1', 'error' => ['请输入邮箱和验证码']);
}
$checkmail = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';//是否邮箱
if(!preg_match($checkmail,$email)){
return array("status" => '1', 'error' => ['邮箱格式不正确']);
}
$redis = Cache::connect(config::get('redis_options'));
$redisCheck = $redis->get('app_'.$userId . $email);
if ($verificationCode != $redisCheck) {
return array("status" => '1', 'error' => ['验证码错误']);
}else{
return array("status" => '0', 'data' => ['验证码正确']);
}
}