def send_invitation(invitation: InvitationParams, background_task: BackgroundTasks): """ This function sends the recipient an invitation to his email address in the format HTML. :param invitation: InvitationParams, invitation parameters :param background_task: BackgroundTasks :return: json response message, error message if the entered email address is incorrect, confirmation message if the invitation was successfully sent """ try: EmailStr.validate(invitation.recipient_mail) except EmailError: raise HTTPException( status_code=422, detail=INVALID_EMAIL_ADDRESS_ERROR_MESSAGE) if not send_email_invitation( sender_name=invitation.sender_name, recipient_name=invitation.recipient_name, recipient_mail=invitation.recipient_mail, background_tasks=background_task): raise HTTPException(status_code=422, detail="Couldn't send the email!") return RedirectResponse(invitation.send_to, status_code=303)
def confirm_mail(cls, email: str) -> Union[ValueError, str]: """Validating email is valid mail address.""" try: EmailStr.validate(email) return email except EmailError: raise ValueError("address is not valid")
def verify_email_pattern(email: str) -> bool: """ This function checks the correctness of the entered email address :param email: str, the entered email address :return: bool, True if the entered email address is correct, False if the entered email address is incorrect. """ try: EmailStr.validate(email) return True except EmailError: return False
def _create_subscription(email: str = Form(...)): try: EmailStr.validate(email) except (EmailError, EmailSyntaxError): return Response(content="Invalid email address", status_code=400) try: subscriptions.insert({ "created_at": str(datetime.datetime.now()), }, email) except Exception: return Response( content="You are already subscribed to our mailing list", status_code=304) return Response( content="Thanks for subscribing to the TroepTroep event mailing list", status_code=200)
def validate_email(self, email: str): """ Validate email address """ if EmailStr.validate(email): return True
async def on_post(self, base_url: Union[str, URL], session: models.Session, username: str, email: str, password: str, confirm_password: str = None, **kwargs) -> Union[HTMLResponse, RedirectResponse]: """ Handle POST requests """ base_url = str(base_url) email = EmailStr.validate(email) def _register() -> Union[HTMLResponse, RedirectResponse]: try: self.validate_password(password) except ValueError as ex: content = self.render_form(error=str(ex), **kwargs) return HTMLResponse(status_code=400, content=content) if confirm_password is not None: # The only way for confirm_password to be None is if a # standard form doesn't include it, otherwise the value is "" if password != confirm_password: content = self.render_form( error="The specified passwords do not match.", **kwargs) return HTMLResponse(status_code=400, content=content) user = self.user_cls.get_by_email(session, email) if user: if user.username != username: logger.info("Email '%s' already exists.", email) # Is this an information leak? content = self.render_form( error="That email address already exists.", **kwargs) return HTMLResponse(status_code=409, content=content) if user.confirmed: logger.info("User '%s' already confirmed.", username) else: # Unconfirmed, re-registration - update password user.password = password else: user = self.user_cls( username=username, password=password, email=email, ) session.add(user) if not self.email_confirmation_required: user.confirmed_at = tz.utcnow() try: session.commit() except sqlalchemy.exc.IntegrityError: content = self.render_form( error="That username already exists.", **kwargs) return HTMLResponse(status_code=409, content=content) if not self.email_confirmation_required: return RedirectResponse(url=self.location, status_code=303) self.send_email_confirmation(base_url, email, username=username, **kwargs) content = self.render(self.sent_template, username=username, email=email, **kwargs) return HTMLResponse(status_code=200, content=content) return await run_in_threadpool(_register)
def check_email(cls, v, values, **kwargs): return EmailStr.validate(v) if v else ""