Image_Us-메일 인증

codakcodak·2023년 2월 27일
0

ImageUs

목록 보기
9/17

문제상황

  • 회원가입시 이메일을 받고 있지만 추후 민감한 정보로 변경시 본인인증 수단으로 이메일을 인증해야 하기에 존재하지 않는 이메일로 회원가입을 한다면 인증이 불가

해결방법

  • smtplib패키지를 통해 랜덤한 인증코드를 생성하여 메일로 발급하고 사용자가 직접 메일로 확인 후 발급받은 인증코드로 인증해야 유효한 이메일로 저장하는 로직 구현

적용과정

1.스키마 생성

CREATE TABLE `email_auth` (
  `email` varchar(255) NOT NULL,
  `auth_password` int NOT NULL,
  `activated` int NOT NULL default 0,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`email`)
)

*auth_password에 인증코드를 저장

*acrivated에 활성화 여부를 저장

2.인증코드를 담을 html양식 생성

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Email_Authentification</title>

    <!-- 합쳐지고 최소화된 최신 CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
  </head>
  <body>
      <div class="panel panel-info">
        <div class="panel-heading">
          <h3 class="panel-title">AUTH-PASSWORD</h3>
        </div>
        <div class="panel-body">
          1425
        </div>
      </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

3.인증코드 생성 및 smtplib를 통해 메일로 인증코드 발급
(전체코드)

def send_email_auth_password(self,email,auth_password):
        
        sender=self.config['GOOGLE_MAIL_USER']
        password=self.config['GOOGLE_MAIL_PASSWORD']
        receiver=email

        smtp=smtplib.SMTP('smtp.gmail.com',587)
        smtp.ehlo()
        smtp.starttls()
        smtp.login(sender,password)

        msg = MIMEMultipart('alternative')
        with open('./templates/email_auth.html', 'r') as f:
           html = f.read()
        try:
            text=f"Hi,This is message for your email authentification of sign-up from ImageUs"
            html=html % (auth_password)
            msg['Subject']='test_email_send'
            msg['From']=sender
            msg['To']=receiver

            part1 = MIMEText(text, 'plain')
            part2 = MIMEText(html, 'html')

            msg.attach(part1)
            msg.attach(part2)

            smtp.sendmail(sender,receiver,msg.as_string())
        except Exception as e:
            print('error',e)
            return 0
        finally:
            if smtp is not None:
                smtp.quit()
            return 1
  • 환경변수에 저장된 google의 키값 호출
sender=self.config['GOOGLE_MAIL_USER']
password=self.config['GOOGLE_MAIL_PASSWORD']
receiver=email
  • smtp클래스에 메일의 양식 및 표기 데이터들 저장
		smtp=smtplib.SMTP('smtp.gmail.com',587)
        smtp.ehlo()
        smtp.starttls()
        smtp.login(sender,password)

        msg = MIMEMultipart('alternative')
        with open('./templates/email_auth.html', 'r') as f:
           html = f.read()
        try:
            text=f"Hi,This is message for your email authentification of sign-up from ImageUs"
            html=html % (auth_password)
            msg['Subject']='test_email_send'
            msg['From']=sender
            msg['To']=receiver

            part1 = MIMEText(text, 'plain')
            part2 = MIMEText(html, 'html')

            msg.attach(part1)
            msg.attach(part2)

            smtp.sendmail(sender,receiver,msg.as_string())
  • finally를 통해 stmp를 닫고 결과 반환
		except Exception as e:
            print('error',e)
            return 0
        finally:
            if smtp is not None:
                smtp.quit()
            return 1

결과

  • 인증가능한 메일을 사용하기 때문에 추후 개인의 정보를 변경 할 때 본인인증을 확인 가능
profile
숲을 보는 코더

0개의 댓글