aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: bf3d5306004c67bb36a2cad1e13acb51df04ab16 (plain) (blame)
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
# Import Python packages
from email.mime.text import MIMEText
import smtplib
import json
import requests

# Send API request for a random poem
json_data = requests.get('https://poetrydb.org/random').json()

# Extract the poem details from the JSON response
title = json_data[0]['title']
author = json_data[0]['author']
line_count = json_data[0]['linecount']
lines = ''
for line in json_data[0]['lines']:
  lines = lines + line + "\n"

# A test print() statement to ensure the request and parsing processed the data 
# correctly
# print(title, "\n", author, "\n\n", lines)

msg_body = title + "\n" + author + "\n\n" + lines

# Create plaintext message container
msg = MIMEText(msg_body)

# Prepare the metadata of the message
sender_email = ''
recipient_emails = ''
msg['Subject'] = 'Your Daily Poem (' + line_count + ' lines)'
msg['From'] = sender_email
msg['To'] = recipient_email

# Send the message via our own SMTP server, but don't include the
# envelope header.
smtp_server = 'localhost'
s = smtplib.SMTP(smtp_server)
s.sendmail(sender_email, [recipient_emails], msg.as_string())
s.quit()