[PR #4794] feat: opportunistic tls flag for smtp #24644

Closed
opened 2026-02-21 20:23:24 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/4794

State: closed
Merged: Yes


Description

This pull request introduces an opportunistic TLS flag for the SMTP configuration in Dify and changes the default SMTP port from 587 to 465.

Currently, the existing TLS flag in Dify does not support port 465 (It will hang and then timeout), which is more commonly used nowadays due to its higher security. Port 465 is used for SMTP connections secured with SSL/TLS encryption from the start, establishing a secure connection before any SMTP commands are sent. SSL/TLS encryption is enabled by default on this port, providing a higher level of security compared to port 587.

Port 587, on the other hand, is used for SMTP connections with STARTTLS, where the connection starts as a plain-text SMTP connection and can be upgraded to a secure connection using the STARTTLS command. It allows the client and server to negotiate the use of encryption during the SMTP conversation.

To address the limitation of the current TLS flag and enhance the security of email communication, this pull request introduces an opportunistic TLS flag. This flag provides the option to enable opportunistic TLS encryption for SMTP connections, allowing the flexibility to use either port 587 with STARTTLS or port 465 with SSL/TLS, depending on the email server's requirements and support.

Furthermore, the pull request changes the default SMTP port in the example configs from 587 to 465. This ensures that the SMTP connection is encrypted from the beginning by default, providing a higher level of security out of the box.

The changes include updates to the configuration files, environment variables, and the SMTPClient class to support this new feature and the default port change.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Verified that the SMTP configuration updates are reflected in the api/.env.example and docker/docker-compose.yaml files.
  • Tested the SMTPClient class with different combinations of use_tls and opportunistic_tls flags to ensure the correct behavior.
  • Validated that email sending functionality works as expected with the new opportunistic TLS option enabled with port 587.
  • Validated that email sending functionality works as expected with the new opportunistic TLS option disabled with port 465.

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/4794 **State:** closed **Merged:** Yes --- # Description This pull request introduces an opportunistic TLS flag for the SMTP configuration in Dify and changes the default SMTP port from 587 to 465. Currently, the existing TLS flag in Dify does not support port 465 (It will hang and then timeout), which is more commonly used nowadays due to its higher security. Port 465 is used for SMTP connections secured with SSL/TLS encryption from the start, establishing a secure connection before any SMTP commands are sent. SSL/TLS encryption is enabled by default on this port, providing a higher level of security compared to port 587. Port 587, on the other hand, is used for SMTP connections with STARTTLS, where the connection starts as a plain-text SMTP connection and can be upgraded to a secure connection using the STARTTLS command. It allows the client and server to negotiate the use of encryption during the SMTP conversation. To address the limitation of the current TLS flag and enhance the security of email communication, this pull request introduces an opportunistic TLS flag. This flag provides the option to enable opportunistic TLS encryption for SMTP connections, allowing the flexibility to use either port 587 with STARTTLS or port 465 with SSL/TLS, depending on the email server's requirements and support. Furthermore, the pull request changes the default SMTP port in the example configs from 587 to 465. This ensures that the SMTP connection is encrypted from the beginning by default, providing a higher level of security out of the box. The changes include updates to the configuration files, environment variables, and the SMTPClient class to support this new feature and the default port change. ## Type of Change - [x] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? - [x] Verified that the SMTP configuration updates are reflected in the `api/.env.example` and `docker/docker-compose.yaml` files. - [x] Tested the SMTPClient class with different combinations of `use_tls` and `opportunistic_tls` flags to ensure the correct behavior. - [x] Validated that email sending functionality works as expected with the new opportunistic TLS option enabled with port 587. - [x] Validated that email sending functionality works as expected with the new opportunistic TLS option disabled with port 465. # Suggested Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] My changes generate no new warnings - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods - [ ] `optional` I have made corresponding changes to the documentation - [ ] `optional` I have added tests that prove my fix is effective or that my feature works - [ ] `optional` New and existing unit tests pass locally with my changes
yindo added the pull-request label 2026-02-21 20:23:24 -05:00
yindo closed this issue 2026-02-21 20:23:24 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#24644