make it able to send emails

This commit is contained in:
stupidcomputer 2024-07-11 01:31:07 -05:00
parent e12fe38168
commit 55c6f3a5f8
4 changed files with 68 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
__pycache__/
token
config.json

8
config.example.json Normal file
View File

@ -0,0 +1,8 @@
{
"token": "groupme api token",
"smtp_server": "mail.example.com",
"smtp_username": "groupme@example.com",
"smtp_password": "your_password",
"from_addr": "groupme@example.com",
"to_addr": "user@example.com"
}

View File

@ -1,14 +1,41 @@
from sys import argv
import json
from .groupme import GroupMe
from .emailwrap import send_email
def main() -> None:
filename = argv[1]
with open(filename, "r") as file:
token = file.readlines()[0].rstrip()
data = json.loads(file.read())
chats = GroupMe(token)
chats = GroupMe(data["token"])
for message in chats:
title = "[GroupMe] {} sent a message in {}".format(message["name"], message["group_name"])
body = """
Greetings,
{} sent a message in group {} -- it reads as follows:
{}
Much regards,
the internal beepboop.systems mail system
""".format(
message["name"],
message["group_name"],
message["text"]
)
send_email(
title=title,
body=body,
smtp_server=data["smtp_server"],
smtp_username=data["smtp_username"],
smtp_password=data["smtp_password"],
from_addr=data["from_addr"],
to_addr=data["to_addr"],
)
print(message)
main()

30
groupme_sync/emailwrap.py Normal file
View File

@ -0,0 +1,30 @@
import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(title: str, body: str,
smtp_server: str, smtp_username: str, smtp_password: str,
from_addr: str, to_addr: str):
"""
Send an email to an SMTP server with starttls, and some other
stuff. If you need to do anything different, just write your
own implementation.
"""
ctx = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port=587) as server:
server.starttls(context=ctx)
server.ehlo()
server.login(smtp_username, smtp_password)
message = MIMEMultipart("alternative")
message["Subject"] = title
message["From"] = from_addr
message["To"] = to_addr
part = MIMEText(body, "plain")
message.attach(part)
server.sendmail(
from_addr, to_addr, message.as_string()
)