first commit
This commit is contained in:
BIN
server/utils/__pycache__/email.cpython-313.pyc
Normal file
BIN
server/utils/__pycache__/email.cpython-313.pyc
Normal file
Binary file not shown.
BIN
server/utils/__pycache__/welcome_email.cpython-313.pyc
Normal file
BIN
server/utils/__pycache__/welcome_email.cpython-313.pyc
Normal file
Binary file not shown.
139
server/utils/email.py
Normal file
139
server/utils/email.py
Normal file
@@ -0,0 +1,139 @@
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
import os
|
||||
|
||||
def send_email(to_email, subject, body):
|
||||
smtp_host = os.environ.get("SMTP_HOST")
|
||||
smtp_port = int(os.environ.get("SMTP_PORT", 587))
|
||||
smtp_user = os.environ.get("SMTP_USER")
|
||||
smtp_pass = os.environ.get("SMTP_PASS")
|
||||
sender_email = os.environ.get("SMTP_FROM", smtp_user)
|
||||
|
||||
if not all([smtp_host, smtp_port, smtp_user, smtp_pass]):
|
||||
raise ValueError("SMTP config incomplete in environment variables.")
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = sender_email
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body)
|
||||
|
||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_pass)
|
||||
server.send_message(msg)
|
||||
|
||||
|
||||
# Send email with attachment
|
||||
def send_email_with_attachment(to_email, subject, body, attachment_path):
|
||||
smtp_host = os.environ.get("SMTP_HOST")
|
||||
smtp_port = int(os.environ.get("SMTP_PORT", 587))
|
||||
smtp_user = os.environ.get("SMTP_USER")
|
||||
smtp_pass = os.environ.get("SMTP_PASS")
|
||||
sender_email = os.environ.get("SMTP_FROM", smtp_user)
|
||||
|
||||
if not all([smtp_host, smtp_port, smtp_user, smtp_pass]):
|
||||
raise ValueError("SMTP config incomplete in environment variables.")
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = sender_email
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body)
|
||||
|
||||
if attachment_path and os.path.isfile(attachment_path):
|
||||
with open(attachment_path, "rb") as f:
|
||||
file_data = f.read()
|
||||
file_name = os.path.basename(attachment_path)
|
||||
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename=file_name)
|
||||
else:
|
||||
raise FileNotFoundError(f"Attachment file not found: {attachment_path}")
|
||||
|
||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_pass)
|
||||
server.send_message(msg)
|
||||
|
||||
|
||||
# Send email using Gmail directly
|
||||
def send_gmail(to_email, subject, body):
|
||||
smtp_host = "smtp.gmail.com"
|
||||
smtp_port = 587
|
||||
smtp_user = 'macamete.robert@gmail.com'
|
||||
smtp_pass = 'advx yqlv jkaa czvr'
|
||||
sender_email = 'macamete.robert@gmail.com'
|
||||
|
||||
if not all([smtp_user, smtp_pass]):
|
||||
raise ValueError("GMAIL_USER and GMAIL_PASS must be set in environment variables.")
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = sender_email
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body)
|
||||
|
||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_pass)
|
||||
server.send_message(msg)
|
||||
|
||||
|
||||
# Send email with attachment using Gmail directly
|
||||
def send_gmail_with_attachment(to_email, subject, body, attachment_path):
|
||||
smtp_host = "smtp.gmail.com"
|
||||
smtp_port = 587
|
||||
smtp_user = 'macamete.robert@gmail.com'
|
||||
smtp_pass = 'advx yqlv jkaa czvr'
|
||||
sender_email = 'macamete.robert@gmail.com'
|
||||
|
||||
if not all([smtp_user, smtp_pass]):
|
||||
raise ValueError("GMAIL_USER and GMAIL_PASS must be set in environment variables.")
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = sender_email
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body)
|
||||
|
||||
if attachment_path and os.path.isfile(attachment_path):
|
||||
with open(attachment_path, "rb") as f:
|
||||
file_data = f.read()
|
||||
file_name = os.path.basename(attachment_path)
|
||||
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename=file_name)
|
||||
else:
|
||||
raise FileNotFoundError(f"Attachment file not found: {attachment_path}")
|
||||
|
||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_pass)
|
||||
server.send_message(msg)
|
||||
|
||||
# Send email with attachment
|
||||
def send_custom_email_with_attachment(to_email, subject, body, attachment_path, SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS):
|
||||
smtp_host = SMTP_HOST
|
||||
smtp_port = int(SMTP_PORT)
|
||||
smtp_user = SMTP_USER
|
||||
smtp_pass = SMTP_PASS
|
||||
sender_email = smtp_user
|
||||
|
||||
if not all([smtp_host, smtp_port, smtp_user, smtp_pass]):
|
||||
raise ValueError("SMTP config incomplete in environment variables.")
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = sender_email
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body)
|
||||
|
||||
if attachment_path and os.path.isfile(attachment_path):
|
||||
with open(attachment_path, "rb") as f:
|
||||
file_data = f.read()
|
||||
file_name = os.path.basename(attachment_path)
|
||||
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename=file_name)
|
||||
else:
|
||||
raise FileNotFoundError(f"Attachment file not found: {attachment_path}")
|
||||
|
||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_pass)
|
||||
server.send_message(msg)
|
||||
61
server/utils/welcome_email.py
Normal file
61
server/utils/welcome_email.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import os
|
||||
import logging
|
||||
from utils.email import send_gmail_with_attachment
|
||||
|
||||
|
||||
class WelcomeMessage:
|
||||
"""
|
||||
Sends a welcome email with an optional attached user manual (PDF).
|
||||
|
||||
- Looks for the manual in SERVER_ASSETS_DIR (env) or defaults to ../assets next to this file.
|
||||
- Manual filename can be customized with WELCOME_MANUAL_FILENAME (env), default: manual.pdf.
|
||||
"""
|
||||
|
||||
def __init__(self, email: str):
|
||||
self.email = email
|
||||
self.subject = "Bine ati venit!"
|
||||
|
||||
# Allow overriding assets folder and manual filename via env for containerized deployments.
|
||||
default_assets = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "assets"))
|
||||
self.assets_folder = os.getenv("SERVER_ASSETS_DIR", default_assets)
|
||||
self.manual_filename = os.getenv("WELCOME_MANUAL_FILENAME", "Manual.pdf")
|
||||
self.manual = os.path.join(self.assets_folder, self.manual_filename)
|
||||
|
||||
logging.info("WelcomeMessage assets dir: %s", self.assets_folder)
|
||||
logging.info("WelcomeMessage manual path: %s", self.manual)
|
||||
|
||||
self.body = f"""
|
||||
|
||||
Stimate/ă domn/ă,
|
||||
|
||||
Ne face plăcere să vă urăm bun venit la JuridicBloc. Vă mulțumim că ați ales platforma noastră pentru a vă susține nevoile administratiei.
|
||||
|
||||
Pentru a vă ajuta să începeți, am atașat Manualul de utilizare la acest e-mail. Acesta oferă instrucțiuni pas cu pas privind configurarea contului, o prezentare generală a funcțiilor și cele mai bune practici pentru utilizarea eficientă a aplicatiei JuridicBloc.
|
||||
|
||||
Vă recomandăm să consultați manualul atunci când vă este convenabil pentru a vă familiariza cu capacitățile sistemului. Dacă aveți nevoie de asistență suplimentară, echipa noastră de asistență este disponibilă la adresa support@juridicbloc.ro.
|
||||
|
||||
Așteptăm cu nerăbdare să vă susținem succesul și să construim un parteneriat pe termen lung.
|
||||
|
||||
Cu sinceritate,
|
||||
Echipa JuridicBloc
|
||||
|
||||
"""
|
||||
|
||||
def send_email(self):
|
||||
# If the manual exists, send with attachment; otherwise, log and send nothing (or integrate a no-attachment sender later).
|
||||
if os.path.isfile(self.manual):
|
||||
send_gmail_with_attachment(
|
||||
to_email=self.email,
|
||||
subject=self.subject,
|
||||
body=self.body,
|
||||
attachment_path=self.manual,
|
||||
)
|
||||
else:
|
||||
logging.warning(
|
||||
f"Welcome manual missing, skipping attachment. Looked at: {self.manual} "
|
||||
f"(set SERVER_ASSETS_DIR or WELCOME_MANUAL_FILENAME to adjust). "
|
||||
f"Current working directory: {os.getcwd()}"
|
||||
)
|
||||
# If you later implement send_gmail() without attachment, call it here.
|
||||
# from utils.email import send_gmail
|
||||
# send_gmail(to_email=self.email, subject=self.subject, body=self.body)
|
||||
Reference in New Issue
Block a user