0

`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)
jAY
  • 1
  • 1
  • The thing you are sending is not a valid email message. The article is hidden in the headers, or perhaps your MTA sanitizes it and throws away the invalid stuff. – tripleee Feb 02 '23 at 17:02
  • if you want to `send_mail` many times, then it needs to be called from inside the loop. – D.L Feb 02 '23 at 17:03

0 Answers0