add sending of email; fix misc. bugs

This commit is contained in:
stupidcomputer 2025-01-11 15:44:20 -06:00
parent 5ff6fc7ca2
commit def5cd11ec
2 changed files with 28 additions and 21 deletions

View File

@ -1,8 +1,10 @@
from pcomon.messages import MessageDeliverer, Message
from pcomon.messages import PCOMessageDeliverer, PCOMessage
import json
import sys
from subprocess import Popen, PIPE
read_messages = "/run/test/messages"
def get_creds(filename: str) -> dict[str, str]:
@ -33,14 +35,16 @@ def write_read_messages(read_messages: list[str], from_file: str = read_messages
)
)
def send_email(msg: Message) -> None:
print(msg)
def send_email(msg: PCOMessage) -> None:
p = Popen(["sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.email.as_string().encode('utf-8', 'ignore'))
p.wait()
def entry():
creds_file = sys.argv[1]
creds = get_creds(creds_file)
deliverer = MessageDeliverer(
deliverer = PCOMessageDeliverer(
app_id=creds["PCO_APP_ID"],
app_secret=creds["PCO_APP_SECRET"],
person_id=creds["PCO_PERSON_ID"],

View File

@ -1,7 +1,7 @@
import requests
from requests.auth import HTTPBasicAuth
from email.parser import BytesParser
from email import message_from_string
from email.message import Message
from email.header import Header
from email.utils import formataddr
@ -13,7 +13,7 @@ from typing import ClassVar
api_domain = "https://api.planningcenteronline.com"
@dataclass
class Message:
class PCOMessage:
eml_content: str
message_id: str
from_name: str
@ -21,27 +21,30 @@ class Message:
@property
def email(self):
parsed_email: Message = BytesParser(self.eml_content)
email_text = parsed_email.get_body(
preference_list=("plain", "html")
)
parsed_email: Message = message_from_string(self.eml_content)
for part in parsed_email.walk():
if part.get_content_type() == "text/plain":
email_text = part.get_payload(decode=True).decode('utf-8')
message = Message()
msg['From'] = 'pcomon@mail.beepboop.systems'
msg['From'] = formataddr((
message['From'] = 'pcomon@mail.beepboop.systems'
message['From'] = formataddr((
str(Header(self.from_name + " (via pcomon)", "utf-8")), "pcomon@mail.beepboop.systems"
))
msg['To'] = 'ryan@beepboop.systems'
msg['Subject'] = self.subject
msg.add_header("Content-Type", "text")
msg.set_payload(email_text)
message['To'] = 'ryan@beepboop.systems'
message['Subject'] = self.subject
message.add_header("Content-Type", "text")
message.set_payload(email_text)
return message
@dataclass
class MessageDeliverer:
class PCOMessageDeliverer:
app_id: str
app_secret: str
person_id: str
messages: ClassVar[list[Message]] = []
messages: ClassVar[list[PCOMessage]] = []
has_fetched: ClassVar[bool] = False
@property
@ -60,7 +63,7 @@ class MessageDeliverer:
msg_eml_content = requests.get(message["attributes"]["file"]["url"]).text
self.messages.append(
Message(
PCOMessage(
eml_content=msg_eml_content,
message_id=message["id"],
from_name=message["attributes"]["from_name"],
@ -71,7 +74,7 @@ class MessageDeliverer:
self.has_fetched = True
@property
def id_dictionary(self) -> dict[str, Message]:
def id_dictionary(self) -> dict[str, PCOMessage]:
if not self.has_fetched:
return ValueError("you need to call self.fetch_messages() first")
@ -95,7 +98,7 @@ def main():
app_secret = env["PCO_APP_SECRET"]
person_id = env["PCO_PERSON_ID"]
deliverer = MessageDeliverer(app_id, app_secret, person_id)
deliverer = PCOMessageDeliverer(app_id, app_secret, person_id)
deliverer.fetch_messages()
print(deliverer.messages)