Opened 20 months ago
Closed 20 months ago
#34414 closed Bug (fixed)
Connection to SMTP server not closed after exception
Reported by: | chamalsl | Owned by: | nobody |
---|---|---|---|
Component: | Core (Mail) | Version: | 4.1 |
Severity: | Normal | Keywords: | |
Cc: | chamalsl | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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="chamals@test.com", to=["chamaltest@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
- Create a Django app.
Add this method to a view.
def email(request): email_msg = EmailMessage(subject="Topic\n", body="Body", from_email="chamals@test.com", to=["chamaltest@test.com"]) email_msg.send() return HttpResponse("Sent")
- Note: There is a new line character in the email subject.
- Install aiosmtpd
python3 -m pip install aiosmtpd
- Run smtpd server
This will start a SMTP server on port 8025
python3 -m aiosmtpd -n
- Add these lines to settings.py of your Django app.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'localhost' EMAIL_PORT = 8025
- Open attached send_emails.py
Change URL in this line to match your application's URL.
requests.get('http://localhost:8080/file/email')
- Run send_emails.py
This script will make 10 requests to send email.
python3 send_emails.py
- Check connections to port 8025 (SMTP server)
netstat -at | grep 8025
You can see some connections are in ESTABLISHED state.
- 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.
Change History (3)
comment:1 by , 20 months ago
comment:2 by , 20 months ago
Cc: | added |
---|
comment:3 by , 20 months ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Triage Stage: | Unreviewed → Accepted |
Fixed in 8a844e761d098d4005725f991a5e120a1f17cb70.
Note:
See TracTickets
for help on using tickets.
I am unable to attach send_emails.py, because it is detected as spam.
This is the code for send_email.py.