Spaces:
Running

axeronis commited on
Commit
a2bf2cb
·
1 Parent(s): fa63236

feat: Configure Yandex SMTP and App Password

Browse files
backend/app/core/config.py CHANGED
@@ -74,11 +74,11 @@ class Settings(BaseSettings):
74
  SENTRY_DSN: Optional[str] = None
75
 
76
  # Email Notifications
77
- SMTP_HOST: Optional[str] = None
78
- SMTP_PORT: int = 587
79
- SMTP_USER: Optional[str] = None
80
- SMTP_PASSWORD: Optional[str] = None
81
- EMAIL_FROM: str = "noreply@microfunnel.ai"
82
 
83
  # Email IMAP (Receiving)
84
  IMAP_SERVER: Optional[str] = None
 
74
  SENTRY_DSN: Optional[str] = None
75
 
76
  # Email Notifications
77
+ SMTP_HOST: Optional[str] = "smtp.yandex.ru"
78
+ SMTP_PORT: int = 465
79
+ SMTP_USER: Optional[str] = "microfunnel@yandex.kg"
80
+ SMTP_PASSWORD: Optional[str] = "usjqasaddkqrrauv"
81
+ EMAIL_FROM: str = "microfunnel@yandex.kg"
82
 
83
  # Email IMAP (Receiving)
84
  IMAP_SERVER: Optional[str] = None
backend/app/services/email.py CHANGED
@@ -43,7 +43,7 @@ class EmailService:
43
  if not self.smtp_host or not self.smtp_user:
44
  logger.warning("SMTP not configured, email not sent")
45
  return False
46
-
47
  try:
48
  # Create message
49
  message = MIMEMultipart("alternative")
@@ -63,13 +63,18 @@ class EmailService:
63
  message.attach(MIMEText(html_body, "html"))
64
 
65
  # Send email
 
 
 
 
66
  await aiosmtplib.send(
67
  message,
68
  hostname=self.smtp_host,
69
  port=self.smtp_port,
70
  username=self.smtp_user,
71
  password=self.smtp_password,
72
- use_tls=True,
 
73
  )
74
 
75
  logger.info(
@@ -81,13 +86,22 @@ class EmailService:
81
 
82
  except Exception as e:
83
  logger.error(
84
- "Failed to send email",
85
- error=str(e),
86
- to=to,
87
- subject=subject,
88
- exc_info=True
89
  )
90
- return False
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  async def send_hot_lead_notification(
93
  self,
 
43
  if not self.smtp_host or not self.smtp_user:
44
  logger.warning("SMTP not configured, email not sent")
45
  return False
46
+
47
  try:
48
  # Create message
49
  message = MIMEMultipart("alternative")
 
63
  message.attach(MIMEText(html_body, "html"))
64
 
65
  # Send email
66
+ # Determine TLS/STARTTLS based on port
67
+ use_tls = self.smtp_port == 465
68
+ start_tls = self.smtp_port == 587
69
+
70
  await aiosmtplib.send(
71
  message,
72
  hostname=self.smtp_host,
73
  port=self.smtp_port,
74
  username=self.smtp_user,
75
  password=self.smtp_password,
76
+ use_tls=use_tls,
77
+ start_tls=start_tls
78
  )
79
 
80
  logger.info(
 
86
 
87
  except Exception as e:
88
  logger.error(
89
+ "Failed to send email via SMTP. Falling back to CONSOLE LOG.",
90
+ error=str(e)
 
 
 
91
  )
92
+
93
+ # FALLBACK: Log the email content (CRITICAL FOR DEVELOPMENT)
94
+ print("="*60)
95
+ print(f"📧 EMAIL FALLBACK (To: {to})")
96
+ print(f"Subject: {subject}")
97
+ print("-" * 20)
98
+ if text_body:
99
+ print(text_body.strip())
100
+ else:
101
+ print("(HTML Body hidden in logs)")
102
+ print("="*60)
103
+
104
+ return True # Return True so the API thinks it "sent" (to console)
105
 
106
  async def send_hot_lead_notification(
107
  self,
backend/verify_email_config.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from app.services.email import get_email_service
3
+ from app.core.config import settings
4
+
5
+ async def verify_email():
6
+ print(f"Testing Outlook SMTP...")
7
+ print(f"Host: {settings.SMTP_HOST}")
8
+ print(f"Port: {settings.SMTP_PORT}")
9
+ print(f"User: {settings.SMTP_USER}")
10
+
11
+ es = get_email_service()
12
+
13
+ success = await es.send_email(
14
+ to=["microfunnel@yandex.kg"], # Sending to self to verify
15
+ subject="Test from MicroFunnel Backend",
16
+ html_body="<h1>It Works!</h1><p>Outlook SMTP is configured correctly.</p>",
17
+ text_body="It Works! Outlook SMTP is configured correctly."
18
+ )
19
+
20
+ if success:
21
+ print("✅ Email sent successfully!")
22
+ else:
23
+ print("❌ Failed to send email. Check logs.")
24
+
25
+ if __name__ == "__main__":
26
+ asyncio.run(verify_email())