Skip to content

Commit

Permalink
add email templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dqian3 committed Jan 23, 2022
1 parent 261b766 commit c555f0a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
31 changes: 15 additions & 16 deletions apis/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,26 @@
user_api.register_blueprint(google_bp, url_prefix="/api/users/auth")


def send_reset_email(id, email, hashed, url_root):
def send_reset_email(id, email, username, hashed, url_root):
token = create_reset_token(id, hashed)
link = f"{url_root}reset/{id}/{token}"

msg = Message("Reset Your Password - wikispeedruns.com",
recipients=[email])

msg.body = 'Hello,\n\nYou or someone else has requested that a new password '\
'be generated for your account. If you made this request, then '\
'please follow this link: ' + link
# msg.html = render_template('email_reset.html', link=link) #TODO
msg.body = f"Hello {username},\n\n You are receiving this email because we received a request to reset your password. If this wasn't you, you can ignore this email. Otherwise, please follow this link: {link}"
msg.html = render_template('emails/reset.html', user=username, link=link)
mail.send(msg)


def send_confirmation_email(id, email, url_root):
def send_confirmation_email(id, email, username, url_root, on_signup=False):
token = create_confirm_token(id)
link = url_root + "confirm/" + token

msg = Message("Confirm your Email - Wikispeedruns.com", recipients=[email])

msg.body = 'Hello,\n\nClick the following link to confirm your email ' + link
# msg.html = render_template('email_confirmation.html', link=link) #TODO
msg.body = f"Hello {username},\n\nClick the following link to confirm your email: {link}"
msg.html = render_template('emails/confirm.html', link=link, user=username, on_signup=on_signup)

mail.send(msg)

Expand Down Expand Up @@ -164,7 +162,7 @@ def create_user():
cursor.execute(get_id_query)
(id,) = cursor.fetchone()

send_confirmation_email(id, email, request.url_root)
send_confirmation_email(id, email, username, request.url_root, on_signup=True)

db.commit()

Expand Down Expand Up @@ -303,15 +301,15 @@ def confirm_email_request():
Request another email token be sent in as a logged in user, i.e. from profile page
'''
id = session["user_id"]
query = "SELECT `email` FROM `users` WHERE `user_id`=%s"
query = "SELECT `email`, `username` FROM `users` WHERE `user_id`=%s"

db = get_db()
with db.cursor() as cursor:
result = cursor.execute(query, (id, ))
(email,) = cursor.fetchone()
cursor.execute(query, (id, ))
(email, username) = cursor.fetchone()
# TODO throw error?

send_confirmation_email(id, email, request.url_root)
send_confirmation_email(id, email, username, request.url_root)

return "New confirmation email sent", 200

Expand All @@ -334,6 +332,7 @@ def confirm_email():

return "Email Confirmed"


@user_api.post("/reset_password_request")
def reset_password_request():
'''
Expand All @@ -343,15 +342,15 @@ def reset_password_request():

email = request.json['email']

query = "SELECT `user_id`, `hash` FROM `users` WHERE `email`=%s"
query = "SELECT `user_id`, `username`, `hash` FROM `users` WHERE `email`=%s"

db = get_db()
with db.cursor() as cursor:
res = cursor.execute(query, (email, ))
(id, hash) = cursor.fetchone()
(id, username, hash) = cursor.fetchone()

if (res != 0):
send_reset_email(id, email, hash, request.url_root)
send_reset_email(id, email, username, hash, request.url_root)

return f"If the account for {email} exists, an email has been sent with a reset link", 200

Expand Down
17 changes: 17 additions & 0 deletions templates/emails/confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<p>Hello {{user}},</p>

<p>Welcome to Wikispeedruns! Please click <a href="{{link}}"> here</a> or copy and paste this link in your browser to confirm your email:</p>

<p>{{link}}</p>


{% if on_signup %}
<p>
We value your interest in the wonderful game of Wikispeedruns! You can learn more about how to play and try the
prompt of the day on the <a href="https://wikispeedruns.com">WikiSpeedruns Hompage</a>.
</p>
{% endif %}

<p>Sincerely,</p>

<p>The Wikispeedruns Team</p>
13 changes: 13 additions & 0 deletions templates/emails/reset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

<p>Hello {{user}},</p>

<p>
You are receiving this email because we received a request to reset your password. If this wasn't you, you can ignore this email.
Otherwise, please click <a href="{{link}}">here</a> or copy and paste the following link in your browser to reset your password:
</p>
<p>{{link}}</p>


<p>Sincerely,</p>

<p>The Wikispeedruns Team</p>

0 comments on commit c555f0a

Please sign in to comment.