-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_newsletter.py
More file actions
63 lines (52 loc) · 1.92 KB
/
send_newsletter.py
File metadata and controls
63 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from dotenv import load_dotenv
from newsletter_api.database import get_all_subscribers
load_dotenv()
SENDER_EMAIL = os.getenv("SENDER_EMAIL")
SENDER_PASSWORD = os.getenv("SENDER_PASSWORD")
SMTP_SERVER = os.getenv("SMTP_SERVER", "smtp.gmail.com")
SMTP_PORT = int(os.getenv("SMTP_PORT", 587))
HTML_PATH = "data/vector_daily.html"
LOGO_PATH = "data/logo.png"
SUBJECT = "📰 The Vector Daily - AI Newsletter"
def send_email(recipient_email):
if not os.path.exists(HTML_PATH):
print(f"❌ Newsletter file not found at {HTML_PATH}")
return
with open(HTML_PATH, "r", encoding="utf-8") as f:
html_content = f.read()
msg = MIMEMultipart("related")
msg["From"] = SENDER_EMAIL
msg["To"] = recipient_email
msg["Subject"] = SUBJECT
alt = MIMEMultipart("alternative")
msg.attach(alt)
alt.attach(MIMEText(html_content, "html"))
if os.path.exists(LOGO_PATH):
with open(LOGO_PATH, "rb") as f:
logo = MIMEImage(f.read())
logo.add_header("Content-ID", "<logo>")
logo.add_header("Content-Disposition", "inline", filename="logo.jpg")
msg.attach(logo)
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
server.send_message(msg)
print(f"✅ Sent newsletter to {recipient_email}")
except Exception as e:
print(f"❌ Failed to send to {recipient_email}: {e}")
def main():
subscribers = get_all_subscribers()
if not subscribers:
print("⚠️ No subscribers found in database.")
return
print(f"📬 Sending newsletter to {len(subscribers)} subscribers...")
for email in subscribers:
send_email(email)
if __name__ == "__main__":
main()