Coverage for functions \ flipdare \ mailer \ user_mailer.py: 84%
31 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
1#!/usr/bin/env python
2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved.
3#
4# This file is part of Flipdare's proprietary software and contains
5# confidential and copyrighted material. Unauthorised copying,
6# modification, distribution, or use of this file is strictly
7# prohibited without prior written permission from Flipdare Pty Ltd.
8#
9# This software includes third-party components licensed under MIT,
10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details.
11#
13from __future__ import annotations
15import smtplib
16from typing import Any, override
18from flipdare.app_globals import is_valid_email
19from flipdare.app_log import LOG
20from flipdare.constants import IS_DEBUG
21from flipdare.mailer._jinja_email_template import JinjaEmailTemplate
22from flipdare.mailer._mailer import Mailer, MailerOptions
23from flipdare.wrapper import UserWrapper
25__all__ = ["UserMailer"]
28class UserMailer(Mailer):
29 def __init__(
30 self,
31 options: MailerOptions | None = None,
32 smtp_client: smtplib.SMTP_SSL | None = None,
33 should_minify: bool = True,
34 ) -> None:
35 super().__init__(options=options, smtp_client=smtp_client)
36 self._should_minify = should_minify
38 @override
39 def send(
40 self,
41 user: UserWrapper,
42 email_template: JinjaEmailTemplate[Any],
43 notif_check: bool = True,
44 no_reply: bool = True,
45 ) -> bool:
46 """Send an email using the mailer."""
47 from flipdare.services import get_app_logger
49 email = user.email
50 email_result = is_valid_email(email)
51 if email_result.error is not None:
52 msg = f"Invalid email for {user.doc_id}, cannot send email : {email_result.error}"
53 LOG().error(msg)
55 app_logger = get_app_logger()
56 app_logger.invalid_user_email(user)
57 return False
59 email = email_result.normalized
60 if notif_check and not user.email_notifs_enabled:
61 if IS_DEBUG:
62 LOG().debug(
63 f"User {user.doc_id} has email notifications disabled; "
64 f"skipping email to {email}.",
65 )
66 return True
68 return self._send_template(
69 email,
70 email_template,
71 should_minify=self._should_minify,
72 no_reply=no_reply,
73 )