>  기사  >  백엔드 개발  >  phpmailer 바인딩 메일함을 구현하는 방법

phpmailer 바인딩 메일함을 구현하는 방법

高洛峰
高洛峰원래의
2016-12-30 10:24:301451검색

이 기사의 예에서는 phpmailer를 사용하여 메일함을 바인딩하는 구현 방법을 설명합니다. 참고하실 수 있도록 모두와 공유하세요. 자세한 내용은 다음과 같습니다.

효과는 다음과 같습니다.

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分钟^_^",
);

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;);;
    }
}

3. 인증코드를 생성하고 세션에 저장한 후 보내기

// 发送邮箱激活码
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;));
    }
}

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();
    }
}

요약:

1. 휴대폰 인증번호를 보내는 것과 비슷한 아이디어입니다.
2. 차이점은 하나는 문자 메시지 전송용이고 다른 하나는 이메일 전송용입니다.
3. 둘, 보내는 제목 하나는 알리바바다유, 또 하나는 회사에서 적용한 이메일 주소입니다.
4. 문자 발송은 유료이며, 이메일 발송은 무료입니다.

이 글이 PHP 프로그래밍에 종사하는 모든 분들께 도움이 되기를 바랍니다.

phpmailer 바인딩 메일함 구현 방법과 관련된 더 많은 기사를 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.