phpmailer实现绑定邮箱的方法

墨辰丷
Freigeben: 2023-03-28 12:18:02
Original
1395 Leute haben es durchsucht

这篇文章主要介绍了phpmailer绑定邮箱的实现方法,结合实例形式较为详细的分析了phpmailer绑定邮箱的配置、功能实现与相关操作技巧,需要的朋友可以参考下

效果如下:

1.配置

<?php
return array (
 &#39;email_host&#39; => &#39;smtp.aliyun.com&#39;,
 &#39;email_port&#39; => &#39;25&#39;,
 &#39;email_username&#39; => &#39;diandodo@aliyun.com&#39;,
 &#39;email_password&#39; => &#39;xxxxxx&#39;,
 &#39;email_from&#39; => &#39;diandodo@aliyun.com&#39;,
 &#39;email_fromname&#39; => &#39;点多多&#39;,
 &#39;email_subject&#39; => &#39;助店宝商户激活邮箱&#39;,
 &#39;email_body&#39; => "尊敬的用户{$username}您好:
    您的激活码为<font color=&#39;red&#39;>{$code}</font>,请将激活码输入进行验证! 激活码有效期为6分钟^_^",
);
Nach dem Login kopieren

2.发送函数

// 发送邮件
private function _sendEmail($email,$code,$username = &#39;&#39;) {
    import(&#39;@.ORG.phpmailer&#39;);
    $mail = new PHPMailer(); //建立邮件发送类,类名不一定与引入的文件名相同
    $mail->CharSet = "UTF-8";
    $mail->IsSMTP(); // 使用SMTP方式发送
    $mail->Host = C(&#39;email_host&#39;); // 您的企业邮局域名
    $mail->SMTPAuth = true; // 启用SMTP验证功能
    $mail->Username = C(&#39;email_username&#39;); // 邮局用户名(请填写完整的email地址)
    $mail->Password = C(&#39;email_password&#39;); // 邮局密码
    $mail->Port=C(&#39;email_port&#39;);
    $mail->From = C(&#39;email_from&#39;); //邮件发送者email地址
    $mail->FromName = C(&#39;email_fromname&#39;);
    $mail->AddAddress("$email", "$username");
    $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
    $mail->Subject = C(&#39;email_subject&#39;); //邮件标题
    $email_body = "尊敬的用户<strong>{$username}</strong>您好:
    您的激活码为<font color=&#39;red&#39;>{$code}</font>,请将激活码输入进行验证! 激活码有效期为6分钟^_^";
    $mail->Body = $email_body; //邮件内容,上面设置HTML,则可以是HTML
    if(!$mail->Send())
    {
      return array(&#39;status&#39;=>2,&#39;info&#39;=>$mail->ErrorInfo);
    } else {
      return array(&#39;status&#39;=>1,&#39;info&#39;=>&#39;发送成功&#39;);;
    }
}
Nach dem Login kopieren

3.生成验证码保存到session中,并发送

// 发送邮箱激活码
public function sendActivationcode() {
    session($this->activationtime, null);
    $activationtime = session($this->activationtime);
    $email = $this->_post(&#39;email&#39;, &#39;trim&#39;);
    if (IS_AJAX && (!$activationtime || time() > $activationtime)) {
      $activationcode = rand(1000, 9999);
      $res = $this->_sendEmail($email,$activationcode,$this->user[&#39;username&#39;]);
      if($res[&#39;status&#39;] == 1) {
        //设置发送限制时间
        session($this->activationtime, time() + 50);
        session($this->activationcode, array(&#39;code&#39; => $activationcode, &#39;time&#39; => time() + 600));
        $this->ajaxReturn(array(&#39;result&#39; => true));
      } else {
        //发送失败写入日志文件
        $log = date(&#39;Y-m-d H:i:s&#39;) . " 发送失败:{$res[&#39;info&#39;]}" . PHP_EOL;
        file_put_contents(RUNTIME_PATH . &#39;Log/activationcode.log&#39;, $log, FILE_APPEND);
        $this->ajaxReturn(array(&#39;result&#39; => false, &#39;error&#39; => $res[&#39;info&#39;]));
      }
    } else {
      $this->ajaxReturn(array(&#39;result&#39; => false, &#39;error&#39; => &#39;错误的请求&#39;));
    }
}
Nach dem Login kopieren

4.验证并绑定

// 绑定邮箱
public function bind_email() {
    if (IS_POST) {
      // 获取验证码
      $activationcode = $this->_post(&#39;activationcode&#39;,&#39;trim&#39;);
      $email = $this->_post(&#39;email&#39;,&#39;trim&#39;);
      $session_activationcode = session($this->activationcode);
      if (time() > $session_activationcode[&#39;time&#39;] || $activationcode != $session_activationcode[&#39;code&#39;]) {
        $this->error(&#39;验证码有误&#39;);
      } else {
        M(&#39;User&#39;)->where(array(&#39;id&#39;=>$this->user[&#39;id&#39;]))->save(array(&#39;email&#39;=>$email));
        $this->success(&#39;绑定成功&#39;,U(&#39;Account/my&#39;));
      }
    } else {
      $this->display();
    }
}
Nach dem Login kopieren

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php实现正则判断是否为合法身份证号的方法

php通过eval实现字符串格式的计算公式

PHP实现随机获取未被微信屏蔽的域名

Das obige ist der detaillierte Inhalt vonphpmailer实现绑定邮箱的方法. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!