nonzeroexit commited on
Commit
750c75a
·
verified ·
1 Parent(s): 88c6b7a

Create email_sender_api.py

Browse files
Files changed (1) hide show
  1. email_sender_api.py +98 -0
email_sender_api.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ import smtplib
4
+ from email.mime.multipart import MIMEMultipart
5
+ from email.mime.text import MIMEText
6
+ from email.mime.base import MIMEBase
7
+ from email import encoders
8
+ import base64
9
+ import os
10
+
11
+ app = Flask(__name__) # Make sure your Flask app instance is named 'app'
12
+ CORS(app)
13
+
14
+ # Environment variables will be set as Secrets in Hugging Face Space settings
15
+ SMTP_SERVER_HOST = os.environ.get("SMTP_SERVER_HOST")
16
+ SMTP_SERVER_PORT = os.environ.get("SMTP_SERVER_PORT")
17
+ SMTP_SENDER_EMAIL = os.environ.get("SMTP_SENDER_EMAIL")
18
+ SMTP_SENDER_PASSWORD = os.environ.get("SMTP_SENDER_PASSWORD")
19
+
20
+ @app.route('/send-report-via-email', methods=['POST'])
21
+ def handle_send_email():
22
+ print("EMAIL_SENDER_API (HF): Received request at /send-report-via-email")
23
+ if not all([SMTP_SERVER_HOST, SMTP_SERVER_PORT, SMTP_SENDER_EMAIL, SMTP_SENDER_PASSWORD]):
24
+ error_msg = "EMAIL_SENDER_API (HF): ERROR - SMTP environment variables (Secrets) not fully set."
25
+ print(error_msg)
26
+ return jsonify({"status": "error", "message": "Email server configuration incomplete on the API."}), 500
27
+
28
+ try:
29
+ SMTP_PORT_INT = int(SMTP_SERVER_PORT)
30
+ except ValueError:
31
+ error_msg = "EMAIL_SENDER_API (HF): ERROR - Invalid SMTP_SERVER_PORT configured."
32
+ print(error_msg)
33
+ return jsonify({"status": "error", "message": "Invalid email server port configured on the API."}), 500
34
+
35
+ data = request.json
36
+ if not data:
37
+ print("EMAIL_SENDER_API (HF): No JSON data received.")
38
+ return jsonify({"status": "error", "message": "No data received by email API."}), 400
39
+
40
+ recipient_email = data.get('recipient_email')
41
+ pdf_base64_data = data.get('pdf_base64_data')
42
+ pdf_filename = data.get('pdf_filename')
43
+ print(f"EMAIL_SENDER_API (HF): Attempting to send to: {recipient_email}, Filename: {pdf_filename}")
44
+
45
+ if not all([recipient_email, pdf_base64_data, pdf_filename]):
46
+ print("EMAIL_SENDER_API (HF): Missing required data in JSON payload.")
47
+ return jsonify({"status": "error", "message": "Missing required data: recipient_email, pdf_base64_data, or pdf_filename."}), 400
48
+
49
+ try:
50
+ msg = MIMEMultipart()
51
+ msg['From'] = SMTP_SENDER_EMAIL
52
+ msg['To'] = recipient_email
53
+ msg['Subject'] = f"EPIC-AMP Analysis Report: {pdf_filename}"
54
+ body = f"""Dear User,\n\nPlease find your EPIC-AMP analysis report attached.\n\nFilename: {pdf_filename}\n\nThis report includes details on your sequence analysis.\n\nThank you for using EPIC-AMP!\n\nBest regards,\nThe EPIC-AMP Team\nBioinformatics and Computational Biology Unit, Zewail City\nFor inquiries: [email protected]"""
55
+ msg.attach(MIMEText(body, 'plain'))
56
+
57
+ pdf_data_bytes = base64.b64decode(pdf_base64_data)
58
+ part = MIMEBase('application', 'octet-stream')
59
+ part.set_payload(pdf_data_bytes)
60
+ encoders.encode_base64(part)
61
+ part.add_header('Content-Disposition', f"attachment; filename=\"{pdf_filename}\"")
62
+ msg.attach(part)
63
+
64
+ print(f"EMAIL_SENDER_API (HF): Connecting to SMTP server {SMTP_SERVER_HOST}:{SMTP_PORT_INT}")
65
+ with smtplib.SMTP(SMTP_SERVER_HOST, SMTP_PORT_INT) as server:
66
+ server.set_debuglevel(1)
67
+ server.ehlo()
68
+ if SMTP_PORT_INT == 587:
69
+ print("EMAIL_SENDER_API (HF): Starting TLS...")
70
+ server.starttls()
71
+ server.ehlo()
72
+ print("EMAIL_SENDER_API (HF): Logging in...")
73
+ server.login(SMTP_SENDER_EMAIL, SMTP_SENDER_PASSWORD)
74
+ print("EMAIL_SENDER_API (HF): Sending mail...")
75
+ server.sendmail(SMTP_SENDER_EMAIL, recipient_email, msg.as_string())
76
+ print("EMAIL_SENDER_API (HF): Mail sent.")
77
+
78
+ success_msg = f"Report successfully sent to {recipient_email}."
79
+ print(f"EMAIL_SENDER_API (HF): {success_msg}")
80
+ return jsonify({"status": "success", "message": success_msg}), 200
81
+
82
+ except smtplib.SMTPAuthenticationError as e:
83
+ error_msg = f"EMAIL_SENDER_API (HF): SMTP Authentication Error - {e}"
84
+ print(error_msg)
85
+ return jsonify({"status": "error", "message": "Email server authentication failed on the API."}), 500
86
+ except Exception as e:
87
+ import traceback
88
+ error_msg = f"EMAIL_SENDER_API (HF): Error sending email to {recipient_email} - {e}"
89
+ print(error_msg)
90
+ print(traceback.format_exc())
91
+ return jsonify({"status": "error", "message": f"Failed to send email via API: {e}"}), 500
92
+
93
+ # This __main__ block is mostly for local testing.
94
+ # On Hugging Face Spaces with Gunicorn, Gunicorn directly imports and runs the 'app' object.
95
+ # if __name__ == '__main__':
96
+ # port = int(os.environ.get("PORT", 7860)) # Default port for HF Spaces if not overridden
97
+ # print(f"Starting Email Sender API Service on port {port} locally...")
98
+ # app.run(host='0.0.0.0', port=port, debug=True)