Last active
March 11, 2025 08:57
-
-
Save Abhayparashar31/daa5de4dc849cf1c05c11c7d821fa68a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.client | |
import urllib.parse | |
import smtplib | |
import json | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
# Step 1: Fetch News Data from MediaStack API | |
def fetch_news(): | |
conn = http.client.HTTPConnection('api.mediastack.com') | |
params = urllib.parse.urlencode({ | |
'access_key': 'e0be64430e8f77ff85d08f9c1229c001', | |
'categories': 'technology,science', | |
'sort': 'published_desc', | |
'limit': 5, # Fetch latest 5 articles | |
'sources':'cnn,bbc' | |
}) | |
conn.request('GET', '/v1/news?{}'.format(params)) | |
res = conn.getresponse() | |
data = res.read() | |
return json.loads(data.decode('utf-8')) # Convert JSON response to dictionary | |
# Step 2: Format News in a Beautiful HTML Template | |
def format_news(): | |
news_data = fetch_news() | |
news_list = news_data.get("data", []) | |
if not news_list: | |
return "<h2 style='color: red;'>No latest news found.</h2>" | |
email_body = """ | |
<html> | |
<head> | |
<style> | |
body { font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; } | |
.container { background-color: #ffffff; padding: 20px; border-radius: 8px; } | |
h2 { color: #2E86C1; text-align: center; } | |
.news-item { border-bottom: 1px solid #ddd; padding: 10px 0; } | |
.news-title { font-size: 18px; color: #2E86C1; font-weight: bold; } | |
.news-desc { font-size: 14px; color: #333; margin: 5px 0; } | |
.news-link { text-decoration: none; color: #E74C3C; font-weight: bold; } | |
.footer { margin-top: 20px; font-size: 12px; text-align: center; color: #777; } | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h2>π° Your Daily Tech & Cyber News Digest</h2> | |
""" | |
for idx, article in enumerate(news_list, start=1): | |
title = article.get("title", "No Title") | |
description = article.get("description", "No Description") | |
url = article.get("url", "#") # Link to full article | |
email_body += f""" | |
<div class="news-item"> | |
<p class="news-title">πΉ {idx}. {title}</p> | |
<p class="news-desc">{description}</p> | |
<a class="news-link" href="{url}" target="_blank">π Read More</a> | |
</div> | |
""" | |
email_body += """ | |
<div class="footer"> | |
βοΈ <i>This email was automatically generated using the <b>Mediastack API.<b></i><br> | |
π Stay informed, stay ahead! | |
</div> | |
</div> | |
</body> | |
</html> | |
""" | |
return email_body | |
# Step 3: Send Email Function | |
def send_email(): | |
sender_email = "" # Replace with your email | |
sender_password = "" # Use an App Password (if using Gmail) | |
recipient_email = "" # Replace with recipient's email | |
subject = "π° Your Daily Tech & Cyber News Digest" | |
body = format_news() | |
msg = MIMEMultipart() | |
msg['From'] = sender_email | |
msg['To'] = recipient_email | |
msg['Subject'] = subject | |
msg.attach(MIMEText(body, 'html')) # Use 'html' to send formatted email | |
try: | |
server = smtplib.SMTP("smtp.gmail.com", 587) | |
server.starttls() # Secure the connection | |
server.login(sender_email, sender_password) | |
server.sendmail(sender_email, recipient_email, msg.as_string()) | |
server.quit() | |
print("β Email sent successfully!") | |
except Exception as e: | |
print(f"β Error sending email: {e}") | |
# Step 4: Call the Function | |
send_email() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment