I’m fairly new to django and was trying to send an email containing Persian letters using django.core.mail.EmailMessage
. here is my code:
from django.core.mail import EmailMessage
from django.conf import settings
def custom_sender(subject: str, body: str, recipient_list: list[str], attachments: list[str] = (), content_type_html: bool = False):
try:
email = EmailMessage(
subject,
body,
settings.DEFAULT_FROM_EMAIL,
recipient_list,
)
if content_type_html:
email.content_subtype = "html"
for attachment in attachments:
email.attach_file(attachment)
email.send()
except Exception as e:
print(f'nnFailed to send email: {e}nn')
and this is how I tried to use it in my view :
custom_sender(subject='ایمیل تستی',
body='این یک ایمیل تستی است.',
recipient_list=[user.email])
but I encounter this error :
'charmap' codec can't encode character 'u06cc' in position 1: character maps to <undefined>
I have noticed that I can’t even print Persian stuff out in the console in my django project and I keep encountering this same error (this doesn’t happen outside of my django project, and I CAN print Persian letters out in the console). can someone please tell me what this error is about and how I can fix it?
I asked some AIs and tried a bunch of solutions like encoding the subject and body using .encode('utf-8')
and using django’s smart_str
, or using python’s unicode strings like
u'این یک پیام تستی است'
I even tried messing around with django’s setting.py
like trying to change the DEFAULT_CHARSET
(although it already is set to 'utf-8'
by default) or changing the default language to Persian to see if it helps, but none of those work and the error remains.
Amir J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.