Coder19/interview_system
0
1import os2import smtplib3from email.mime.multipart import MIMEMultipart4from email.mime.text import MIMEText5from email.mime.image import MIMEImage6from email.utils import formatdate7from dotenv import load_dotenv8 9# Load environment variables10load_dotenv()11 12def send_cheating_email(image_path, confidence=0.99, detection_type="suspicious activity", timestamp=None):13 """14 Send an email with the screenshot of the cheating incident.15 16 Args:17 image_path: Path to the screenshot image18 confidence: Confidence level of the detection (0-1)19 detection_type: Type of cheating detected20 timestamp: When the incident was detected21 """22 # Email configuration23 sender_email = os.getenv('EMAIL_USER')24 receiver_email = os.getenv('EMAIL_RECIPIENT')25 password = os.getenv('EMAIL_PASS')26 27 if not all([sender_email, receiver_email, password]):28 print("Email configuration is incomplete. Please check your .env file.")29 return30 31 # Format the subject with incident details32 subject = f"🚨 Cheating Alert: {detection_type.capitalize()} Detected"33 34 # Format the email body with incident details35 body = f"""36 <html>37 <body>38 <h2>🚨 Cheating Incident Detected</h2>39 <p><strong>Type:</strong> {detection_type.capitalize()}</p>40 <p><strong>Confidence Level:</strong> {int(confidence * 100)}%</p>41 <p><strong>Time of Detection:</strong> {timestamp or 'N/A'}</p>42 <p><strong>Image Path:</strong> {os.path.basename(image_path)}</p>43 <hr>44 <p>Please find the attached screenshot of the incident.</p>45 <p>This is an automated message. Please investigate immediately.</p>46 <br>47 <p>Best regards,<br>Exam Security System</p>48 </body>49 </html>50 """51 52 # Create message container53 msg = MIMEMultipart()54 msg['From'] = sender_email55 msg['To'] = receiver_email56 msg['Subject'] = subject57 msg['Date'] = formatdate(localtime=True)58 59 # Attach the HTML body60 msg.attach(MIMEText(body, 'html'))61 62 try:63 # Attach the image64 with open(image_path, 'rb') as img_file:65 img = MIMEImage(img_file.read())66 img.add_header('Content-Disposition', 'attachment', filename=os.path.basename(image_path))67 msg.attach(img)68 69 # Create secure connection with server and send email70 with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:71 server.login(sender_email, password)72 server.send_message(msg)73 74 print(f"Email alert sent to {receiver_email}")75 76 except Exception as e:77 print(f"Failed to send email: {str(e)}")78 # Log the error for debugging79 with open('email_errors.log', 'a') as f:80 f.write(f"{timestamp or 'No timestamp'}: {str(e)}\n")81 