1+ # -*- coding: utf-8 -*-
2+ """Email utility functions."""
3+ import logging
4+ from typing import List , Optional , Union
5+ from flask import current_app
6+ from flask_mail import Message
7+ from mind_matter_api .extensions import mail
8+
9+ logger = logging .getLogger (__name__ )
10+
11+ def send_email (
12+ to : Union [str , List [str ]],
13+ subject : str ,
14+ html : str ,
15+ text : Optional [str ] = None ,
16+ cc : Optional [Union [str , List [str ]]] = None ,
17+ bcc : Optional [Union [str , List [str ]]] = None ,
18+ attachments : Optional [List [tuple ]] = None
19+ ) -> bool :
20+ """Send an email using Flask-Mail.
21+
22+ Args:
23+ to: Email address(es) of the recipient(s)
24+ subject: Subject of the email
25+ html: HTML content of the email
26+ text: Plain text content of the email (optional)
27+ cc: Email address(es) to CC (optional)
28+ bcc: Email address(es) to BCC (optional)
29+ attachments: List of attachments as (filename, content_type, data) tuples (optional)
30+
31+ Returns:
32+ bool: True if email was sent successfully, False otherwise
33+ """
34+ try :
35+ msg = Message (
36+ subject = subject ,
37+ recipients = [to ] if isinstance (to , str ) else to ,
38+ html = html ,
39+ body = text ,
40+ cc = [cc ] if isinstance (cc , str ) else cc ,
41+ bcc = [bcc ] if isinstance (bcc , str ) else bcc ,
42+ attachments = attachments ,
43+ sender = current_app .config ['MAIL_DEFAULT_SENDER' ]
44+ )
45+
46+ mail .send (msg )
47+ logger .info (f"Email sent successfully to { to } " )
48+ return True
49+
50+ except Exception as e :
51+ logger .error (f"Failed to send email to { to } : { str (e )} " )
52+ return False
53+
54+ def send_password_reset_email (to : str , reset_token : str ) -> bool :
55+ """Send a password reset email.
56+
57+ Args:
58+ to: Email address of the recipient
59+ reset_token: Password reset token
60+
61+ Returns:
62+ bool: True if email was sent successfully, False otherwise
63+ """
64+ reset_url = f"{ current_app .config .get ('FRONTEND_URL' , '' )} /reset-password?token={ reset_token } "
65+
66+ html = f"""
67+ <h1>Password Reset Request</h1>
68+ <p>You have requested to reset your password. Click the link below to proceed:</p>
69+ <p><a href="{ reset_url } ">Reset Password</a></p>
70+ <p>If you did not request this, please ignore this email.</p>
71+ <p>This link will expire in 1 hour.</p>
72+ """
73+
74+ text = f"""
75+ Password Reset Request
76+
77+ You have requested to reset your password. Visit the link below to proceed:
78+ { reset_url }
79+
80+ If you did not request this, please ignore this email.
81+ This link will expire in 1 hour.
82+ """
83+
84+ return send_email (
85+ to = to ,
86+ subject = "Password Reset Request" ,
87+ html = html ,
88+ text = text
89+ )
90+
91+ def send_welcome_email (to : str , name : str ) -> bool :
92+ """Send a welcome email to new users.
93+
94+ Args:
95+ to: Email address of the recipient
96+ name: Name of the recipient
97+
98+ Returns:
99+ bool: True if email was sent successfully, False otherwise
100+ """
101+ html = f"""
102+ <h1>Welcome to Mind Matter!</h1>
103+ <p>Hello { name } ,</p>
104+ <p>Thank you for joining Mind Matter. We're excited to have you on board!</p>
105+ <p>You can now log in to your account and start exploring our features.</p>
106+ """
107+
108+ text = f"""
109+ Welcome to Mind Matter!
110+
111+ Hello { name } ,
112+
113+ Thank you for joining Mind Matter. We're excited to have you on board!
114+ You can now log in to your account and start exploring our features.
115+ """
116+
117+ return send_email (
118+ to = to ,
119+ subject = "Welcome to Mind Matter!" ,
120+ html = html ,
121+ text = text
122+ )
0 commit comments