Skip to content

Commit

Permalink
feat(smtp&share): implemented smtp support and fixed share brain (#3049)
Browse files Browse the repository at this point in the history
completes ENT-53 & fixes ENT-52
  • Loading branch information
StanGirard authored Aug 22, 2024
1 parent 6a6b962 commit e2df8b3
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 263 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ [email protected]
RESEND_CONTACT_SALES_FROM=[email protected]
RESEND_CONTACT_SALES_TO=<change-me>

# SMTP
QUIVR_SMTP_SERVER=smtp.example.com
QUIVR_SMTP_PORT=587
QUIVR_SMTP_USERNAME=username
QUIVR_SMTP_PASSWORD=password

CRAWL_DEPTH=1

PREMIUM_MAX_BRAIN_NUMBER=30
Expand Down
4 changes: 4 additions & 0 deletions backend/api/quivr_api/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class BrainSettings(BaseSettings):
class ResendSettings(BaseSettings):
model_config = SettingsConfigDict(validate_default=False)
resend_api_key: str = "null"
quivr_smtp_server: str = ""
quivr_smtp_port: int = 587
quivr_smtp_username: str = ""
quivr_smtp_password: str = ""


# Global variables to store the Supabase client and database instances
Expand Down
34 changes: 32 additions & 2 deletions backend/api/quivr_api/packages/emails/send_email.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
from typing import Dict

import resend
import smtplib
from quivr_api.models.settings import ResendSettings


def send_email(params: Dict):
settings = ResendSettings()
resend.api_key = settings.resend_api_key
return resend.Emails.send(params)
if settings.resend_api_key != "null":
resend.api_key = settings.resend_api_key
return resend.Emails.send(params)
else:
# Use SMTP to send the email
smtp_server = settings.quivr_smtp_server
smtp_port = settings.quivr_smtp_port
smtp_username = settings.quivr_smtp_username
smtp_password = settings.quivr_smtp_password

try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)

from_address = params.get('from', '[email protected]')
to_addresses = params.get('to', [])
subject = params.get('subject', '')
html_content = params.get('html', '')

message = f"From: {from_address}\n"
message += f"To: {', '.join(to_addresses)}\n"
message += f"Subject: {subject}\n"
message += "Content-Type: text/html\n\n"
message += html_content

server.sendmail(from_address, to_addresses, message)

return {"message": "Email sent successfully"}
except Exception as e:
raise Exception(f"Failed to send email: {str(e)}")
10 changes: 1 addition & 9 deletions backend/api/quivr_api/routes/subscription_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,7 @@ async def accept_invitation(
is_default_brain=False,
)
shared_brain = brain_service.get_brain_by_id(brain_id)
integration_brain = integration_brains_repository.get_integration_brain(
brain_id=brain_id,
)
integration_brains_repository.add_integration_brain(
brain_id=brain_id,
user_id=current_user.id,
integration_id=integration_brain.integration_id,
settings=integration_brain.settings,
)

except Exception as e:
logger.error(f"Error adding user to brain: {e}")
raise HTTPException(status_code=400, detail=f"Error adding user to brain: {e}")
Expand Down
Loading

0 comments on commit e2df8b3

Please sign in to comment.