Ilyas KHIAT commited on
Commit
2f538b6
·
1 Parent(s): a88f8ef

emailing and prompt enhance

Browse files
Files changed (1) hide show
  1. main.py +21 -11
main.py CHANGED
@@ -13,6 +13,7 @@ from prompt import *
13
  from typing import Literal
14
  import time
15
  from fastapi.middleware.cors import CORSMiddleware
 
16
 
17
  import smtplib
18
  from email.mime.text import MIMEText
@@ -71,25 +72,34 @@ class ContactForm(BaseModel):
71
  email: EmailStr
72
  message: str
73
 
 
 
 
 
 
 
 
 
 
 
 
74
  # Function to send email
75
  def send_email(form_data: ContactForm):
76
- sender_email = os.getenv("SENDER_EMAIL")
77
- sender_password = os.getenv("SENDER_PASSWORD")
 
78
  receiver_email = os.getenv("RECEIVER_EMAIL") # Your email
79
 
80
  # Setup the message content
81
- message = MIMEText(f"Name: {form_data.name}\nEmail: {form_data.email}\nMessage: {form_data.message}")
82
- message['Subject'] = f"New contact form submission from {form_data.name}"
83
- message['From'] = sender_email
84
- message['To'] = receiver_email
85
 
 
86
  try:
87
- # Connect to the SMTP server and send the email
88
- with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
89
- server.login(sender_email, sender_password)
90
- server.sendmail(sender_email, receiver_email, message.as_string())
91
  except Exception as e:
92
- raise HTTPException(status_code=500, detail=str(e))
 
93
 
94
  # Endpoint to handle form submission
95
  @app.post("/send_email")
 
13
  from typing import Literal
14
  import time
15
  from fastapi.middleware.cors import CORSMiddleware
16
+ import requests
17
 
18
  import smtplib
19
  from email.mime.text import MIMEText
 
72
  email: EmailStr
73
  message: str
74
 
75
+ def send_simple_message(to,subject,text):
76
+ api_key = os.getenv("MAILGUN_API_KEY")
77
+
78
+ return requests.post(
79
+ "https://api.mailgun.net/v3/sandboxafc6970ffdab40ee9566a4e180b117fd.mailgun.org/messages",
80
+ auth=("api", api_key),
81
+ data={"from": "Excited User <mailgun@sandboxafc6970ffdab40ee9566a4e180b117fd.mailgun.org>",
82
+ "to": [to],
83
+ "subject": subject,
84
+ "text": text})
85
+
86
  # Function to send email
87
  def send_email(form_data: ContactForm):
88
+ # sender_email = os.getenv("SENDER_EMAIL")
89
+ # sender_password = os.getenv("SENDER_PASSWORD")
90
+
91
  receiver_email = os.getenv("RECEIVER_EMAIL") # Your email
92
 
93
  # Setup the message content
94
+ text = f"Name: {form_data.name}\nEmail: {form_data.email}\nMessage: {form_data.message}"
95
+ title = "New message from your website!"
 
 
96
 
97
+ # Send the email
98
  try:
99
+ send_simple_message(receiver_email,title,text)
 
 
 
100
  except Exception as e:
101
+ print(e)
102
+ return {"message": "Failed to send email."}
103
 
104
  # Endpoint to handle form submission
105
  @app.post("/send_email")