在我的邮件中没有从 smtplib 获得所需的输出

问题描述 投票:0回答:1

我正在编写一个使用新闻 API 并将新闻发送到我的邮件的程序。一切都很好,但是当我收到邮件时,它在一行中向我提供了所有新闻,我希望它使用一些断行。这就是我得到的结果。

这是我写的代码。

import requests
from send_email import send_email

api_key = "xxxxx"
url = ("xxxxx")

# Make a request
request = requests.get(url)

# Get a dictionary with data
content = request.json()

body = ''
# Access the article title and description
for article in content["articles"]:
    if article["title"] is not None:
        body = body + article["title"] + "\n" + article["description"] + 2*"\n"


#   Sending article titles and description via email
body = body.encode("utf-8")
message = f"""\
Subject: Python API News

{body}
"""
send_email(message)

我使用的发送电子邮件的代码运行良好。我已经在我的其他程序中使用了它。

这就是我想要的结果,请帮忙。

我没有尝试做任何事情来解决它,因为我不知道是什么原因造成的。

python smtp smtplib newsapi
1个回答
0
投票

由于我不知道其结构中存在的内容,我将为您提供尽可能少的示例,以便您适应功能:

import smtplib
from email.mime.text import MIMEText

smtp_server = 'your server here'
smtp_port = your_port_number_here_as_int
username = 'your username'
password = 'your password'
from_addr = 'from addres with @'
to_addr = 'to address with @'

msg = MIMEText('Hello my friend 5P33DC0R3,\n\nThis is a test email.\n\nBest,\n bye bye')
msg['Subject'] = 'Test Email'
msg['From'] = from_addr
msg['To'] = to_addr

server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(username, password)
server.send_message(msg)
server.quit()
© www.soinside.com 2019 - 2024. All rights reserved.