`I am trying to iterate through articles available in an API key. Then I send the same to my email. Unfortunately, I only receive one email as opposed to the entire articles in the API key.
Here is what I tried to do: `
import os
import smtplib
import ssl
import requests
api_key = "x"
url = "https://newsapi.org/v2/everything?q=tesla&from=2023-01-02&sortBy=publishedAt&apiKey=x"
# Make request
request = requests.get(url)
contents = request.json()
# Access the article titles and description
body = ""
for content in contents["articles"]:
articles = content["title"] + "\n" + content["description"] + 2*"\n"
articles_updated = articles.encode("utf-8")
def send_mail(message):
host = "smtp.gmail.com"
port = 465
username = "x"
password = "x"
receiver = "x"
context = ssl.create_default_context()
with smtplib.SMTP_SSL(host, port, context=context) as server:
server.login(username, password)
server.sendmail(username, receiver, message)
send_mail(articles)