문제상황
해결방법
적용과정
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
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
결과