11from typing import Dict , List , Union
2+ from .constants import MAILING_USERS_BATCH_SIZE
3+ from .models import MailingSchema
4+ from users .models import CustomUser
25
36import django .db .models
47from django .contrib .auth import get_user_model
912User = get_user_model ()
1013
1114
15+ def prepare_mail_data (post_data ) -> dict :
16+ users = post_data .getlist ("users[]" )
17+ schema_id = post_data ["schemas" ]
18+ subject = post_data ["subject" ]
19+ mail_schema = MailingSchema .objects .get (pk = schema_id )
20+ context = {}
21+ for variable_name in mail_schema .schema :
22+ key_in_post = "field-" + variable_name
23+ if key_in_post in post_data :
24+ context [variable_name ] = post_data [key_in_post ]
25+ users_to_send = CustomUser .objects .filter (pk__in = users )
26+ data_dict = {
27+ "users_to_send" : users_to_send ,
28+ "subject" : subject ,
29+ "mail_schema_template" : mail_schema .template ,
30+ "context" : context ,
31+ }
32+ return data_dict
33+
34+
35+ def create_message_groups (messages : list ) -> list [list ]:
36+ grouped_messages : list [list ] = [
37+ messages [message : message + MAILING_USERS_BATCH_SIZE ]
38+ for message in range (0 , len (messages ), MAILING_USERS_BATCH_SIZE )
39+ ]
40+ return grouped_messages
41+
42+
1243def send_mail (
13- user : User ,
14- subject : str ,
15- template_string : str ,
16- template_context : Union [
17- Dict ,
18- List ,
19- ] = None ,
20- connection = None ,
44+ user : User ,
45+ subject : str ,
46+ template_string : str ,
47+ template_context : Union [
48+ Dict ,
49+ List ,
50+ ] = None ,
51+ connection = None ,
2152):
2253 return send_mass_mail ([user ], subject , template_string , template_context , connection )
2354
2455
56+ def send_group_messages (messages : list ) -> int :
57+ connection = mail .get_connection ()
58+ num_sent = connection .send_messages (messages )
59+ connection .close ()
60+ return num_sent
61+
62+
2563def send_mass_mail (
26- users : django .db .models .QuerySet | List [User ],
27- subject : str ,
28- template_string : str ,
29- template_context : Union [
30- Dict ,
31- List ,
32- ] = None ,
33- connection = None ,
34- ) -> None :
64+ users : django .db .models .QuerySet | List [User ],
65+ subject : str ,
66+ template_string : str ,
67+ template_context : Union [
68+ Dict ,
69+ List ,
70+ ] = None ,
71+ connection = None ,
72+ ) -> int :
3573 """
3674 Begin mailing to specified users, sending rendered template with template_text arg.
3775 Throws an error if template render is unsuccessful.
@@ -45,16 +83,18 @@ def send_mass_mail(
4583 if template_context is None :
4684 template_context = {}
4785
48- connection = connection or mail .get_connection ()
4986 template = Template (template_string )
5087 messages = []
5188 for user in users :
5289 template_context ["user" ] = user
5390 html_msg = template .render (Context (template_context ))
5491 plain_msg = template .render (Context (template_context ))
55- msg = EmailMultiAlternatives (
56- subject , plain_msg , None , [user .email ], connection = connection
57- )
92+ msg = EmailMultiAlternatives (subject , plain_msg , None , [user .email ])
5893 msg .attach_alternative (html_msg , "text/html" )
5994 messages .append (msg )
60- return connection .send_messages (messages )
95+
96+ grouped_messages = create_message_groups (messages )
97+ num_sent : int = 0
98+ for group in grouped_messages :
99+ num_sent += send_group_messages (group )
100+ return num_sent
0 commit comments