| Server IP : 3.111.61.48 / Your IP : 216.73.216.67 Web Server : Apache System : Linux ip-10-0-5-176 6.8.0-1057-aws #60~22.04.1-Ubuntu SMP Wed May 27 08:16:59 UTC 2026 x86_64 User : ubuntu ( 1000) PHP Version : 8.2.31 Disable Function : exec,passthru,shell_exec,system MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/vhost/itmtrsrv.arukustech.com/api/utils/ |
Upload File : |
import time
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from urllib.parse import urlparse
from config.settings import EMAIL, EMAIL_CRED_FILE
# -----------------------------------------
# HELPERS
# -----------------------------------------
def clean_url(url):
parsed = urlparse(url)
return parsed.netloc
def load_creds():
creds = {}
with open(EMAIL_CRED_FILE) as f:
for line in f:
if "=" in line:
k, v = line.strip().split("=", 1)
creds[k] = v
return creds
def get_receivers():
receivers = EMAIL.get("receiver")
if isinstance(receivers, str):
return [receivers]
return receivers or []
def get_server_name(server_name):
return server_name or os.getenv("SERVER_NAME") or os.uname().nodename
# -----------------------------------------
# SERVER ALERT EMAIL
# -----------------------------------------
def send_server_alert(server_name, alert_type, message):
creds = load_creds()
receivers = get_receivers()
resolved_server = get_server_name(server_name)
subject = f"[CRITICAL] {alert_type} Alert - {resolved_server}"
msg = MIMEMultipart("related")
msg["From"] = EMAIL["sender"]
msg["To"] = ", ".join(receivers)
msg["Subject"] = subject
msg_alt = MIMEMultipart("alternative")
msg.attach(msg_alt)
# -----------------------------------------
# HTML BODY
# -----------------------------------------
html = f"""
<html>
<body style="margin:0;padding:0;background:#f4f6f9;font-family:Arial,sans-serif;">
<div style="max-width:650px;margin:20px auto;background:#ffffff;border:1px solid #ddd;border-radius:10px;overflow:hidden;">
<!-- HEADER TITLE -->
<div style="background:#1e293b;color:#ffffff;padding:18px;text-align:center;font-size:18px;font-weight:bold;">
Server Monitoring Alert
</div>
<!-- LOGO BELOW HEADER -->
<div style="text-align:center;padding:15px;background:#f8fafc;">
<img src="cid:company_logo" style="width:150px;height:auto;">
</div>
<!-- BODY -->
<div style="padding:20px;">
<table style="width:100%;border-collapse:collapse;font-size:14px;">
<tr>
<td style="padding:10px;border-bottom:1px solid #eee;"><b>Hostname</b></td>
<td style="padding:10px;border-bottom:1px solid #eee;">{os.uname().nodename}</td>
</tr>
<tr>
<td style="padding:10px;border-bottom:1px solid #eee;"><b>Server Name</b></td>
<td style="padding:10px;border-bottom:1px solid #eee;">{resolved_server}</td>
</tr>
<tr>
<td style="padding:10px;border-bottom:1px solid #eee;"><b>Alert Type</b></td>
<td style="padding:10px;border-bottom:1px solid #eee;color:red;"><b>{alert_type}</b></td>
</tr>
<tr>
<td style="padding:10px;border-bottom:1px solid #eee;"><b>Message</b></td>
<td style="padding:10px;border-bottom:1px solid #eee;">{message}</td>
</tr>
<tr>
<td style="padding:10px;"><b>Time</b></td>
<td style="padding:10px;">{time.strftime('%d-%b-%Y %H:%M:%S')} IST</td>
</tr>
</table>
</div>
</div>
</body>
</html>
"""
msg_alt.attach(MIMEText(html, "html"))
# -----------------------------------------
# LOGO ATTACHMENT
# -----------------------------------------
try:
logo_path = EMAIL.get("logo_path")
if logo_path and os.path.exists(logo_path):
with open(logo_path, "rb") as f:
img = MIMEImage(f.read())
img.add_header("Content-ID", "<company_logo>")
img.add_header(
"Content-Disposition",
"inline",
filename="logo.png"
)
msg.attach(img)
except Exception:
pass
# -----------------------------------------
# SMTP SEND
# -----------------------------------------
with smtplib.SMTP(
EMAIL["smtp_server"],
EMAIL["smtp_port"]
) as s:
s.starttls()
s.login(
creds["AWS_SMTP_USERNAME"],
creds["AWS_SMTP_PASSWORD"]
)
s.sendmail(
EMAIL["sender"],
receivers,
msg.as_string()
)
# -----------------------------------------
# PASSWORD RESET EMAIL
# -----------------------------------------
def send_password_reset_email(user_email, reset_link):
creds = load_creds()
msg = MIMEMultipart("related")
msg["From"] = EMAIL["sender"]
msg["To"] = user_email
msg["Subject"] = "Reset your company Monitor password"
msg_alt = MIMEMultipart("alternative")
msg.attach(msg_alt)
html = f"""
<html>
<body style="font-family:Arial,sans-serif;background:#f6f8fb;">
<div style="max-width:500px;margin:40px auto;background:#fff;border-radius:10px;padding:30px;text-align:center;">
<h2>Password Reset</h2>
<p>This link expires in 30 minutes.</p>
<a href="{reset_link}" style="
display:inline-block;
padding:12px 22px;
background:#4f46e5;
color:#fff;
text-decoration:none;
border-radius:6px;
">
Reset Password
</a>
</div>
</body>
</html>
"""
msg_alt.attach(MIMEText(html, "html"))
with smtplib.SMTP(
EMAIL["smtp_server"],
EMAIL["smtp_port"]
) as s:
s.starttls()
s.login(
creds["AWS_SMTP_USERNAME"],
creds["AWS_SMTP_PASSWORD"]
)
s.sendmail(
EMAIL["sender"],
[user_email],
msg.as_string()
)