Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
71e5828d
Unverified
Commit
71e5828d
authored
Feb 07, 2024
by
JonahCui
Committed by
GitHub
Feb 07, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add support for smtp when send email (#2409)
parent
65a02f7d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
4 deletions
+61
-4
.env.example
api/.env.example
+8
-2
config.py
api/config.py
+6
-0
ext_mail.py
api/extensions/ext_mail.py
+15
-1
smtp.py
api/libs/smtp.py
+26
-0
docker-compose.yaml
docker/docker-compose.yaml
+6
-1
No files found.
api/.env.example
View file @
71e5828d
...
@@ -81,11 +81,17 @@ UPLOAD_IMAGE_FILE_SIZE_LIMIT=10
...
@@ -81,11 +81,17 @@ UPLOAD_IMAGE_FILE_SIZE_LIMIT=10
# Model Configuration
# Model Configuration
MULTIMODAL_SEND_IMAGE_FORMAT=base64
MULTIMODAL_SEND_IMAGE_FORMAT=base64
# Mail configuration, support: resend
# Mail configuration, support: resend
, smtp
MAIL_TYPE=
MAIL_TYPE=
resend
MAIL_DEFAULT_SEND_FROM=no-reply <no-reply@dify.ai>
MAIL_DEFAULT_SEND_FROM=no-reply <no-reply@dify.ai>
RESEND_API_KEY=
RESEND_API_KEY=
RESEND_API_URL=https://api.resend.com
RESEND_API_URL=https://api.resend.com
# smtp configuration
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=123
SMTP_PASSWORD=abc
SMTP_USE_TLS=false
# Sentry configuration
# Sentry configuration
SENTRY_DSN=
SENTRY_DSN=
...
...
api/config.py
View file @
71e5828d
...
@@ -209,6 +209,12 @@ class Config:
...
@@ -209,6 +209,12 @@ class Config:
self
.
MAIL_DEFAULT_SEND_FROM
=
get_env
(
'MAIL_DEFAULT_SEND_FROM'
)
self
.
MAIL_DEFAULT_SEND_FROM
=
get_env
(
'MAIL_DEFAULT_SEND_FROM'
)
self
.
RESEND_API_KEY
=
get_env
(
'RESEND_API_KEY'
)
self
.
RESEND_API_KEY
=
get_env
(
'RESEND_API_KEY'
)
self
.
RESEND_API_URL
=
get_env
(
'RESEND_API_URL'
)
self
.
RESEND_API_URL
=
get_env
(
'RESEND_API_URL'
)
# SMTP settings
self
.
SMTP_SERVER
=
get_env
(
'SMTP_SERVER'
)
self
.
SMTP_PORT
=
get_env
(
'SMTP_PORT'
)
self
.
SMTP_USERNAME
=
get_env
(
'SMTP_USERNAME'
)
self
.
SMTP_PASSWORD
=
get_env
(
'SMTP_PASSWORD'
)
self
.
SMTP_USE_TLS
=
get_bool_env
(
'SMTP_USE_TLS'
)
# ------------------------
# ------------------------
# Workpace Configurations.
# Workpace Configurations.
...
...
api/extensions/ext_mail.py
View file @
71e5828d
...
@@ -21,13 +21,27 @@ class Mail:
...
@@ -21,13 +21,27 @@ class Mail:
api_key
=
app
.
config
.
get
(
'RESEND_API_KEY'
)
api_key
=
app
.
config
.
get
(
'RESEND_API_KEY'
)
if
not
api_key
:
if
not
api_key
:
raise
ValueError
(
'RESEND_API_KEY is not set'
)
raise
ValueError
(
'RESEND_API_KEY is not set'
)
api_url
=
app
.
config
.
get
(
'RESEND_API_URL'
)
api_url
=
app
.
config
.
get
(
'RESEND_API_URL'
)
if
api_url
:
if
api_url
:
resend
.
api_url
=
api_url
resend
.
api_url
=
api_url
resend
.
api_key
=
api_key
resend
.
api_key
=
api_key
self
.
_client
=
resend
.
Emails
self
.
_client
=
resend
.
Emails
elif
app
.
config
.
get
(
'MAIL_TYPE'
)
==
'smtp'
:
from
libs.smtp
import
SMTPClient
if
not
app
.
config
.
get
(
'SMTP_SERVER'
)
or
not
app
.
config
.
get
(
'SMTP_PORT'
):
raise
ValueError
(
'SMTP_SERVER and SMTP_PORT are required for smtp mail type'
)
if
not
app
.
config
.
get
(
'SMTP_USERNAME'
)
or
not
app
.
config
.
get
(
'SMTP_PASSWORD'
):
raise
ValueError
(
'SMTP_USERNAME and SMTP_PASSWORD are required for smtp mail type'
)
self
.
_client
=
SMTPClient
(
server
=
app
.
config
.
get
(
'SMTP_SERVER'
),
port
=
app
.
config
.
get
(
'SMTP_PORT'
),
username
=
app
.
config
.
get
(
'SMTP_USERNAME'
),
password
=
app
.
config
.
get
(
'SMTP_PASSWORD'
),
_from
=
app
.
config
.
get
(
'MAIL_DEFAULT_SEND_FROM'
),
use_tls
=
app
.
config
.
get
(
'SMTP_USE_TLS'
)
)
else
:
else
:
raise
ValueError
(
'Unsupported mail type {}'
.
format
(
app
.
config
.
get
(
'MAIL_TYPE'
)))
raise
ValueError
(
'Unsupported mail type {}'
.
format
(
app
.
config
.
get
(
'MAIL_TYPE'
)))
...
...
api/libs/smtp.py
0 → 100644
View file @
71e5828d
import
smtplib
from
email.mime.multipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
class
SMTPClient
:
def
__init__
(
self
,
server
:
str
,
port
:
int
,
username
:
str
,
password
:
str
,
_from
:
str
,
use_tls
=
False
):
self
.
server
=
server
self
.
port
=
port
self
.
_from
=
_from
self
.
username
=
username
self
.
password
=
password
self
.
_use_tls
=
use_tls
def
send
(
self
,
mail
:
dict
):
smtp
=
smtplib
.
SMTP
(
self
.
server
,
self
.
port
)
if
self
.
_use_tls
:
smtp
.
starttls
()
smtp
.
login
(
self
.
username
,
self
.
password
)
msg
=
MIMEMultipart
()
msg
[
'Subject'
]
=
mail
[
'subject'
]
msg
[
'From'
]
=
self
.
_from
msg
[
'To'
]
=
mail
[
'to'
]
msg
.
attach
(
MIMEText
(
mail
[
'html'
],
'html'
))
smtp
.
sendmail
(
self
.
username
,
mail
[
'to'
],
msg
.
as_string
())
smtp
.
quit
()
docker/docker-compose.yaml
View file @
71e5828d
...
@@ -104,10 +104,15 @@ services:
...
@@ -104,10 +104,15 @@ services:
MILVUS_PASSWORD
:
Milvus
MILVUS_PASSWORD
:
Milvus
# The milvus tls switch.
# The milvus tls switch.
MILVUS_SECURE
:
'
false'
MILVUS_SECURE
:
'
false'
# Mail configuration, support: resend
# Mail configuration, support: resend
, smtp
MAIL_TYPE
:
'
'
MAIL_TYPE
:
'
'
# default send from email address, if not specified
# default send from email address, if not specified
MAIL_DEFAULT_SEND_FROM
:
'
YOUR
EMAIL
FROM
(eg:
no-reply
<no-reply@dify.ai>)'
MAIL_DEFAULT_SEND_FROM
:
'
YOUR
EMAIL
FROM
(eg:
no-reply
<no-reply@dify.ai>)'
SMTP_HOST
:
'
'
SMTP_PORT
:
587
SMTP_USERNAME
:
'
'
SMTP_PASSWORD
:
'
'
SMTP_USE_TLS
:
'
true'
# the api-key for resend (https://resend.com)
# the api-key for resend (https://resend.com)
RESEND_API_KEY
:
'
'
RESEND_API_KEY
:
'
'
RESEND_API_URL
:
https://api.resend.com
RESEND_API_URL
:
https://api.resend.com
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment