mailer.py (1321B)
1 import smtplib 2 import json 3 from pathlib import Path 4 5 class Mailer: 6 def __init__(self): 7 current_dir = Path(__file__).parent 8 9 settings_path = current_dir / '..' / '..' / 'settings.json' 10 11 with open(settings_path, 'r') as f: 12 settings = json.load(f) 13 14 self.notification_recipient = settings['recipient_email'] 15 self.email = settings['email'] 16 self.password = settings['email_password'] 17 self.smtpsrv = settings['smtp_server'] 18 19 def send(self, subject, message, recipient): 20 with smtplib.SMTP(self.smtpsrv, 587) as server: 21 server.starttls() 22 server.login(self.email, self.password) 23 server.sendmail(self.email, recipient, f"Subject: {subject}\n\n{message}") 24 25 def send_notification(self, subject, message): 26 self.send(subject, message, self.notification_recipient) 27 28 def send_confirmation(self, recipient): 29 with open('confirmation.html', 'r', encoding='utf-8') as file: 30 html_content = file.read() 31 self.send('Confirmation Email', html_content, recipient) 32 33 def format_message(self, name, email, content): 34 return f"MESSAGE RECEIVED FROM: {name}\nWITH EMAIL: {email}\nCONTENT:\n{content}" 35 36 def format_subject(self, email): 37 return f"Contact from: {email}"