[Django] #34414: Connection to SMTP server not closed after exception

8 views
Skip to first unread message

Django

unread,
Mar 14, 2023, 9:44:23 AM3/14/23
to django-...@googlegroups.com
#34414: Connection to SMTP server not closed after exception
---------------------------------------+------------------------
Reporter: chamalsl | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+------------------------
Django EmailMessage class does not close connection to SMTP server, when
an exception is raised.


{{{
email_msg = EmailMessage(subject="Topic\n", body="Body",
from_email="cha...@test.com",
to=["chama...@test.com"])
email_msg.send()
}}}


Above Django/python code sends an email. But the email's subject contains
a new line.
So Django will raise a BadHeaderError. But the connection to the SMTP
server is not closed.


== Steps

1. Create a Django app.
Add this method to a view.
{{{
def email(request):
email_msg = EmailMessage(subject="Topic\n", body="Body",
from_email="cha...@test.com",
to=["chama...@test.com"])
email_msg.send()
return HttpResponse("Sent")
}}}

* Note: There is a new line character in the email subject.

2. Install aiosmtpd
{{{
python3 -m pip install aiosmtpd
}}}

3. Run smtpd server
This will start a SMTP server on port 8025
{{{
python3 -m aiosmtpd -n
}}}

4. Add these lines to settings.py of your Django app.
{{{
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 8025
}}}

5. Open attached send_emails.py
Change URL in this line to match your application's URL.
{{{
requests.get('http://localhost:8080/file/email')
}}}

6. Run send_emails.py
This script will make 10 requests to send email.
{{{
python3 send_emails.py
}}}

7. Check connections to port 8025 (SMTP server)
{{{
netstat -at | grep 8025
}}}
You can see some connections are in ESTABLISHED state.

8. Remove the new line character inside the "def email" method.
Run send_emails.py.
Then run
{{{
netstat -at | grep 8025.
}}}
You will see all connections are in TIME_WAIT state.

--
Ticket URL: <https://code.djangoproject.com/ticket/34414>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 14, 2023, 9:48:10 AM3/14/23
to django-...@googlegroups.com
#34414: Connection to SMTP server not closed after exception
-----------------------------+--------------------------------------

Reporter: chamalsl | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: 4.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by chamalsl):

I am unable to attach send_emails.py, because it is detected as spam.
This is the code for send_email.py.

{{{
import requests
i = 0
times = 10
while i < times:

i = i+1
print('Completed')
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34414#comment:1>

Django

unread,
Mar 14, 2023, 9:50:03 AM3/14/23
to django-...@googlegroups.com
#34414: Connection to SMTP server not closed after exception
-----------------------------+--------------------------------------
Reporter: chamalsl | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by chamalsl):

* cc: chamalsl (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34414#comment:2>

Django

unread,
Mar 14, 2023, 11:22:56 AM3/14/23
to django-...@googlegroups.com
#34414: Connection to SMTP server not closed after exception
-----------------------------+------------------------------------
Reporter: chamalsl | Owner: nobody
Type: Bug | Status: closed

Component: Core (Mail) | Version: 4.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => fixed
* stage: Unreviewed => Accepted


Comment:

Fixed in 8a844e761d098d4005725f991a5e120a1f17cb70.

--
Ticket URL: <https://code.djangoproject.com/ticket/34414#comment:3>

Reply all
Reply to author
Forward
0 new messages