update
This commit is contained in:
88
backend/services/email_service.py
Normal file
88
backend/services/email_service.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from jinja2 import Template
|
||||
from config import Config
|
||||
from database import subscribers_collection, articles_collection
|
||||
|
||||
|
||||
def send_newsletter(max_articles=10):
|
||||
"""Send newsletter to all subscribers with AI-summarized articles"""
|
||||
if not Config.EMAIL_USER or not Config.EMAIL_PASSWORD:
|
||||
print("Email credentials not configured")
|
||||
return
|
||||
|
||||
# Get latest articles with AI summaries from database
|
||||
cursor = articles_collection.find(
|
||||
{'summary': {'$exists': True, '$ne': None}}
|
||||
).sort('created_at', -1).limit(max_articles)
|
||||
|
||||
articles = []
|
||||
for doc in cursor:
|
||||
articles.append({
|
||||
'title': doc.get('title', ''),
|
||||
'author': doc.get('author'),
|
||||
'link': doc.get('link', ''),
|
||||
'summary': doc.get('summary', ''),
|
||||
'source': doc.get('source', ''),
|
||||
'published_at': doc.get('published_at', '')
|
||||
})
|
||||
|
||||
if not articles:
|
||||
print("No articles with summaries to send")
|
||||
return
|
||||
|
||||
# Load email template
|
||||
template_path = Path(__file__).parent.parent / 'templates' / 'newsletter_template.html'
|
||||
with open(template_path, 'r', encoding='utf-8') as f:
|
||||
template_content = f.read()
|
||||
|
||||
template = Template(template_content)
|
||||
|
||||
# Prepare template data
|
||||
now = datetime.now()
|
||||
template_data = {
|
||||
'date': now.strftime('%A, %B %d, %Y'),
|
||||
'year': now.year,
|
||||
'article_count': len(articles),
|
||||
'articles': articles,
|
||||
'unsubscribe_link': 'http://localhost:3000', # Update with actual unsubscribe link
|
||||
'website_link': 'http://localhost:3000'
|
||||
}
|
||||
|
||||
# Render HTML
|
||||
html_content = template.render(**template_data)
|
||||
|
||||
# Get all active subscribers
|
||||
subscribers_cursor = subscribers_collection.find({'status': 'active'})
|
||||
subscribers = [doc['email'] for doc in subscribers_cursor]
|
||||
|
||||
# Send emails
|
||||
for subscriber in subscribers:
|
||||
try:
|
||||
msg = MIMEMultipart('alternative')
|
||||
msg['Subject'] = f'Munich News Daily - {datetime.now().strftime("%B %d, %Y")}'
|
||||
msg['From'] = f'Munich News Daily <{Config.EMAIL_USER}>'
|
||||
msg['To'] = subscriber
|
||||
msg['Date'] = datetime.now().strftime('%a, %d %b %Y %H:%M:%S %z')
|
||||
msg['Message-ID'] = f'<{datetime.now().timestamp()}.{subscriber}@dongho.kim>'
|
||||
msg['X-Mailer'] = 'Munich News Daily'
|
||||
|
||||
# Add plain text version as fallback
|
||||
plain_text = "This email requires HTML support. Please view it in an HTML-capable email client."
|
||||
msg.attach(MIMEText(plain_text, 'plain', 'utf-8'))
|
||||
|
||||
# Add HTML version
|
||||
msg.attach(MIMEText(html_content, 'html', 'utf-8'))
|
||||
|
||||
server = smtplib.SMTP(Config.SMTP_SERVER, Config.SMTP_PORT)
|
||||
server.starttls()
|
||||
server.login(Config.EMAIL_USER, Config.EMAIL_PASSWORD)
|
||||
server.send_message(msg)
|
||||
server.quit()
|
||||
|
||||
print(f"Newsletter sent to {subscriber}")
|
||||
except Exception as e:
|
||||
print(f"Error sending to {subscriber}: {e}")
|
||||
Reference in New Issue
Block a user