def update_email(self, new_email):
from server import app, mail
session = SessionManager.Session()
try:
user = session.query(User).filter(User.id == self.id).one()
if user.email is not None and user.email_confirmed:
# send notification mail
subject = '[{0}] Email Address Update Notification'.format(app.config['SITE_NAME'])
email_content = render_template('email-change-notification.html', info={
'title': subject,
'user_name': user.name,
'site_name': app.config['SITE_NAME']
})
msg = Message(subject, recipients=[self.email], html=email_content)
try:
mail.send(msg)
except SMTPAuthenticationError:
raise ServerError('SMTP authentication failed', 500)
# update
user.email = new_email
user.email_confirmed = False
self.email = new_email
self.email_confirmed = False
# send email
self.send_confirm_email()
session.commit()
return json_resp({'message': 'ok'})
except IntegrityError:
raise ClientError('duplicate email')
except NoResultFound:
raise ServerError('user not found')
finally:
SessionManager.Session.remove()
评论列表
文章目录