def send_verify_email(): if request.method == 'GET': user_id = int(request.args['user_id']) form = EmailForm(user_id=user_id) user_name = User.get_name_by_id(user_id) return render_template('sign/send_verify_email.html', user_name=user_name, form=form) else: form = EmailForm(request.form) if form.validate(): # email t_addr = form.email.data # user info user_id = int(form.user_id.data) user_name = User.get_name_by_id(user_id) # add this email to user User.add_email(user_id, t_addr) # gene verify url verify_code = hashlib.sha1(user_name).hexdigest() verify_url = config.SITE_DOMAIN + "verify_email/douban/" + str( user_id) + "/" + verify_code # prepare email content msgText = '''<h3>点 <a href='%s'>这里</a>,激活你在西窗烛的帐号。</h3>''' % verify_url msg = MIMEText(msgText, 'html', 'utf-8') msg['From'] = "西窗烛 <" + config.SMTP_FROM + ">" msg['To'] = user_name + "<" + t_addr + ">" msg['Subject'] = "欢迎来到西窗烛!" # send email s = smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) s.login(config.SMTP_USER, config.SMTP_PASSWORD) s.sendmail(config.SMTP_FROM, t_addr, msg.as_string()) return redirect(url_for('verify_email_callback', state='send_succ')) else: user_id = int(form.user_id.data) user_name = User.get_name_by_id(user_id) return render_template('sign/send_verify_email.html', user_name=user_name, form=form)
def send_verify_email(): if request.method == 'GET': user_id = int(request.args['user_id']) form = EmailForm(user_id=user_id) user_name = User.get_name_by_id(user_id) return render_template('sign/send_verify_email.html', user_name=user_name, form=form) else: form = EmailForm(request.form) if form.validate(): # email t_addr = form.email.data # user info user_id = int(form.user_id.data) user_name = User.get_name_by_id(user_id) # add this email to user User.add_email(user_id, t_addr) # gene verify url verify_code = hashlib.sha1(user_name).hexdigest() verify_url = config.SITE_DOMAIN + "verify_email/douban/" + str(user_id) + "/" + verify_code # prepare email content msgText = '''<html> <h1>点击下面的链接,激活你在西窗烛的帐号:</h1> <a href='%s'>%s</a> </html>''' % (verify_url, verify_url) msg = MIMEText(msgText, 'html', 'utf-8') msg['From'] = "西窗烛 <" + config.SMTP_FROM + ">" msg['To'] = user_name + "<" + t_addr + ">" msg['Subject'] = "欢迎来到西窗烛!" # send email s = smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) s.login(config.SMTP_USER, config.SMTP_PASSWORD) s.sendmail(config.SMTP_FROM, t_addr, msg.as_string()) return redirect(url_for('verify_email_callback', state='send_succ')) else: user_id = int(form.user_id.data) user_name = User.get_name_by_id(user_id) return render_template('sign/send_verify_email.html', user_name=user_name, form=form)
def send_active_email(user_id): user = User.query.get_or_404(user_id) if request.method == 'GET': form = EmailForm(user_id=user_id) return render_template('sign/send_active_email.html', user=user, form=form) else: form = EmailForm(request.form) if form.validate(): to_addr = form.email.data # update user email user.email = to_addr db.session.add(user) db.session.commit() # gene active url active_code = hashlib.sha1(user.name).hexdigest() active_url = config.SITE_DOMAIN + "active_user/" + str(user_id) + "/" + active_code # prepare email content msgText = '''<h3>点 <a href='%s'>这里</a>,激活你在西窗烛的帐号。</h3>''' % active_url msg = MIMEText(msgText, 'html', 'utf-8') msg['From'] = "西窗烛 <" + config.SMTP_USER + ">" msg['To'] = user.name + "<" + to_addr + ">" msg['Subject'] = "欢迎来到西窗烛!" # send email s = smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) s.login(config.SMTP_USER, config.SMTP_PASSWORD) try: s.sendmail(config.SMTP_USER, to_addr, msg.as_string()) except: return redirect(url_for('active_state', state='send_failed')) else: return redirect(url_for('active_state', state='send_succ')) else: return render_template('sign/send_active_email.html', user=user, form=form)