From df32f3c614a184ef17dcae80643e49d49c6cf7d4 Mon Sep 17 00:00:00 2001 From: b3yond Date: Mon, 1 Jan 2018 11:23:50 +0100 Subject: [PATCH] added class to write mails to users --- config.toml.example | 8 +++++++ retootbot.py | 0 retweetbot.py | 0 sendmail.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ ticketfrei.py | 0 5 files changed, 61 insertions(+) mode change 100644 => 100755 retootbot.py mode change 100644 => 100755 retweetbot.py create mode 100755 sendmail.py mode change 100644 => 100755 ticketfrei.py diff --git a/config.toml.example b/config.toml.example index 10176df..bb39b2c 100644 --- a/config.toml.example +++ b/config.toml.example @@ -17,6 +17,14 @@ shutdown_contact_screen_name = 'links_tech' access_token_key = "876046057721008128-J35moxFXUvLb24MnaMVbVpqiEtxBlcc" access_token_secret = "I7PQZMHuJDS5WslgUhqEeZbEWGhwLhmOetvwFoTn8YDKW" +[mail] +mailserver = "smtp.riseup.net" +port = 587 +user = "ticketfrei" +passphrase = "sup3rs3cur3" + + + # [trigger] # goodlists are one regex per line. # badlists are one badword per line. diff --git a/retootbot.py b/retootbot.py old mode 100644 new mode 100755 diff --git a/retweetbot.py b/retweetbot.py old mode 100644 new mode 100755 diff --git a/sendmail.py b/sendmail.py new file mode 100755 index 0000000..161ce79 --- /dev/null +++ b/sendmail.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import smtplib +import pytoml as toml +from email.mime.text import MIMEText + + +class Mailer(object): + """ + Maintains the connection to the mailserver and sends text to users. + """ + + def __init__(self, config): + """ + + :param config: The config file generated from config.toml + """ + # This generates the From address by stripping the part until the first + # period from the mail server address and won't work always. + self.fromaddr = config["mail"]["user"] + "@" + \ + config["mail"]["mailserver"].partition(".")[2] + + # starts a client session with the SMTP server + self.s = smtplib.SMTP(config["mail"]["mailserver"]) + self.s.starttls() + self.s.login(config["mail"]["user"], config["mail"]["passphrase"]) + + def send(self, text, recipient, subject): + """ + + :param text: (string) the content of the mail + :param recipient: (string) the recipient of the mail + :param subject: (string) the subject of the mail + :return: string for logging purposes, contains recipient & subject + """ + msg = MIMEText(text) + + msg["From"] = self.fromaddr + msg["To"] = recipient + msg["Subject"] = subject + + self.s.send_message(msg) + + return "Sent mail to " + recipient + ": " + subject + +# For testing: +if __name__ == '__main__': + # read config in TOML format (https://github.com/toml-lang/toml#toml) + with open('config.toml') as configfile: + config = toml.load(configfile) + + m = Mailer(config) + print(m.send("This is a test mail.", m.fromaddr, "Test")) diff --git a/ticketfrei.py b/ticketfrei.py old mode 100644 new mode 100755