PacktPublishing / Django-4-by-example

Django 4 by example (4th Edition) published by Packt
https://djangobyexample.com/
MIT License
799 stars 461 forks source link

Error sending e-mail with smtp backend when using python 3.12 and django 4.1.x #41

Open pierryangelo opened 1 year ago

pierryangelo commented 1 year ago

In chapter 2, page 68, using the recommended version of Django by the book (exactly) I could't send e-mail via gmail account. The traceback error was this:

File "/home/pierry/django/my_env/lib/python3.12/site-packages/django/core/mail/message.py", line 298, in send
    return self.get_connection(fail_silently).send_messages([self])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pierry/django/my_env/lib/python3.12/site-packages/django/core/mail/backends/smtp.py", line 124, in send_messages
    new_conn_created = self.open()
                       ^^^^^^^^^^^
  File "/home/pierry/django/my_env/lib/python3.12/site-packages/django/core/mail/backends/smtp.py", line 87, in open
    self.connection.starttls(
TypeError: SMTP.starttls() got an unexpected keyword argument 'keyfile'
>>> send_mail('Django mail', 'This e-mail was sent with Django', 'your_email_here@gmail.com', ['you_email_here@gmail.com'], fail_silently=False)

After searching, I discovered that this was an issue https://github.com/django/django/commit/2848e5d0ce5cf3c31fe87525536093b21d570f69. (keyfile and certfile parameters were removed in Python 3.12) To solve this, you must install Django 4.2.x or copy the https://github.com/django/django/blob/main/django/core/mail/backends/smtp.py to your local django installation replacing the original 4.1.x file.

I did this and it worked, so I could send the e-mail following the book example.

roshnirajak commented 7 months ago

It worked for me too, I'm using django 4.1.13 (copying the file worked). Thanks.

LuizMateuss commented 3 months ago

I downgraded my python version and it worked too

martin-iturralde commented 2 months ago

I downgraded my python version and it worked too

From which version to what version?