Пример:
тема: лучшие коты дня
тело:
Вот 10 лучших сообщений от r/cats за сегодня:
- заголовок сообщения
встроенное изображение - ячейка сообщения
встроенное изображение
...
import praw
from praw.models import MoreComments
import re
from email.message import EmailMessage
import ssl
import smtplib
import requests
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
username = 'abc123'
client_id= 'sjfksknfsekfnllsf'
client_secret = 'snkrfsklsflslfsfefmsfllls'
password = 'abc123'
reddit_instance = praw.Reddit(
client_id = client_id,
client_secret = client_secret,
username=username,
password=password,
user_agent = 'redditAPI'
)
subreddit = reddit_instance.subreddit('cats')
email_password = 'abcd abcd abcd abcd'
email_sender = '[email protected]'
email_receiver = '[email protected]'
subject = "Cat Soup for Your Soul"
msg = MIMEMultipart()
msg['From'] = email_sender
msg['To'] = email_receiver
msg['Subject'] = subject
email_body = ''
index = 1
for post in subreddit.top(limit = None, time_filter = 'day'):
if index > 25:
break
elif 'jpg' in post.url or 'png' in post.url:
# download image
img = requests.get(post.url).content
# create MIMEImage obj
img_mime = MIMEImage(img)
img_mime.add_header('Content-Disposition', f'attachment; filename=image_{index}.png')
# format email body with title and image
email_body+= f'{index+1}. {post.title}\n'
email_body+= f"Attachment: image_{index}.png\n\n"
index+= 1
else:
continue
msg.attach(MIMEText(email_body, 'plain'))
msg.attach(img_mime)
context = ssl.create_default_context()
# Send email
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_receiver, msg)
Подробнее здесь: https://stackoverflow.com/questions/786 ... o-my-email