1- import pathlib
2- from typing import Union , List , Dict
1+ from typing import Dict , List , Union
32
43import django .db .models
54from django .contrib .auth import get_user_model
65from django .core import mail
76from django .core .mail import EmailMultiAlternatives
8- from django .template . loader import render_to_string
7+ from django .template import Context , Template
98
109User = get_user_model ()
1110
1211
1312def send_mail (
1413 user : User ,
1514 subject : str ,
16- template_path : str ,
15+ template_string : str ,
1716 template_context : Union [
1817 Dict ,
1918 List ,
2019 ] = None ,
2120 connection = None ,
2221):
23- return send_mass_mail ([user ], subject , template_path , template_context , connection )
22+ return send_mass_mail ([user ], subject , template_string , template_context , connection )
2423
2524
2625def send_mass_mail (
2726 users : django .db .models .QuerySet | List [User ],
2827 subject : str ,
29- template_path : str ,
28+ template_string : str ,
3029 template_context : Union [
3130 Dict ,
3231 List ,
@@ -38,20 +37,21 @@ def send_mass_mail(
3837 Throws an error if template render is unsuccessful.
3938 Args:
4039 users: - The list of users who should receive the email.
41- template_path : str of template_path
40+ template_string : str of template_path
4241 subject: Subject of mail.
4342 template_context: Context for template render.
4443 connection: Connection to mail backend
4544 """
4645 if template_context is None :
4746 template_context = {}
48- template_path = pathlib . Path ( template_path ). absolute ()
47+
4948 connection = connection or mail .get_connection ()
49+ template = Template (template_string )
5050 messages = []
5151 for user in users :
5252 template_context ["user" ] = user
53- html_msg = render_to_string ( template_path , template_context )
54- plain_msg = render_to_string ( template_path , template_context )
53+ html_msg = template . render ( Context ( template_context ) )
54+ plain_msg = template . render ( Context ( template_context ) )
5555 msg = EmailMultiAlternatives (
5656 subject , plain_msg , None , [user .email ], connection = connection
5757 )
0 commit comments