Changeset View
Changeset View
Standalone View
Standalone View
bid_main/email.py
| Show First 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | try: | ||||
| ) | ) | ||||
| except (smtplib.SMTPException, OSError): | except (smtplib.SMTPException, OSError): | ||||
| log.exception("error sending address verification mail to %s", email) | log.exception("error sending address verification mail to %s", email) | ||||
| return False | return False | ||||
| log.info("sent address verification mail to %s", email) | log.info("sent address verification mail to %s", email) | ||||
| return True | return True | ||||
| def send_deletion_request_received(user, scheme: str) -> bool: | |||||
| """Send out an email confirming tha an account deletion request was received. | |||||
| :param user: the user deletion was requested for | |||||
| :param scheme: either 'http' or 'https', for link generation. | |||||
| :returns: True when mail was sent successfully, False otherwise. | |||||
| The actual error (if any) is logged. | |||||
| """ | |||||
| email_body_html, email_body_txt, subject = construct_deletion_request_received( | |||||
| user, scheme | |||||
| ) | |||||
| email = user.email | |||||
| try: | |||||
| send_mail( | |||||
| subject, | |||||
| message=email_body_txt, | |||||
| html_message=email_body_html, | |||||
| from_email=None, # just use the configured default From-address. | |||||
| recipient_list=[email], | |||||
| fail_silently=False, | |||||
| ) | |||||
| except (smtplib.SMTPException, OSError): | |||||
| log.exception("error sending address verification mail to %s", email) | |||||
sybren: Not sure if we should be logging the email address of a user who chose to be forgotten.
| |||||
| return False | |||||
| log.info("sent address verification mail to %s", email) | |||||
| return True | |||||
| def _email_verification_hmac(payload: bytes) -> hmac.HMAC: | def _email_verification_hmac(payload: bytes) -> hmac.HMAC: | ||||
| return hmac.new(settings.SECRET_KEY.encode(), payload, hashlib.sha1) | return hmac.new(settings.SECRET_KEY.encode(), payload, hashlib.sha1) | ||||
| def construct_verify_address(user, scheme: str, extra: dict = None) -> (str, str, str): | def construct_verify_address(user, scheme: str, extra: dict = None) -> (str, str, str): | ||||
| """Construct the mail to verify an email address. | """Construct the mail to verify an email address. | ||||
| :param user: the user object whose email needs verification | :param user: the user object whose email needs verification | ||||
| Show All 35 Lines | def construct_verify_address(user, scheme: str, extra: dict = None) -> (str, str, str): | ||||
| ) | ) | ||||
| email_body_txt = loader.render_to_string( | email_body_txt = loader.render_to_string( | ||||
| "bid_main/emails/confirm_email.txt", context | "bid_main/emails/confirm_email.txt", context | ||||
| ) | ) | ||||
| return email_body_html, email_body_txt, context["subject"] | return email_body_html, email_body_txt, context["subject"] | ||||
| def construct_deletion_request_received(user, scheme: str) -> (str, str, str): | |||||
| """Construct the mail about account deletion request. | |||||
| :param user: the user object whose email needs verification | |||||
| :param scheme: either 'http' or 'https', for link generation. | |||||
| :returns: a tuple (html, text, subject) with the email contents. | |||||
| """ | |||||
| context = { | |||||
| "user": user, | |||||
Done Inline ActionsIt's probably better to start the subject with "Blender ID", like "Blender ID account deletion". That way the first words identify what it's about. sybren: It's probably better to start the subject with "Blender ID", like "Blender ID account deletion". | |||||
| "subject": "Deletion of your Blender ID account", | |||||
| } | |||||
| log.debug("Sending email about account deletion request to %s", user.email) | |||||
| email_body_html = loader.render_to_string( | |||||
| "bid_main/emails/deletion_request_received.html", context | |||||
| ) | |||||
| email_body_txt = loader.render_to_string( | |||||
| "bid_main/emails/deletion_request_received.txt", context | |||||
| ) | |||||
| return email_body_html, email_body_txt, context["subject"] | |||||
| class VerificationResult(enum.Enum): | class VerificationResult(enum.Enum): | ||||
| OK = 0 | OK = 0 | ||||
| EXPIRED = 1 | EXPIRED = 1 | ||||
| INVALID = 2 # invalid Base64, HMAC, JSON | INVALID = 2 # invalid Base64, HMAC, JSON | ||||
| OTHER_ACCOUNT = 3 # for another email address | OTHER_ACCOUNT = 3 # for another email address | ||||
| def check_verification_payload( | def check_verification_payload( | ||||
| ▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines | |||||
Not sure if we should be logging the email address of a user who chose to be forgotten.