commit 223660525e2202390c5fdba86ed7b7619490f46c Author: tfa Date: Tue Sep 27 14:58:59 2022 +0200 first commit diff --git a/base-config.yaml b/base-config.yaml new file mode 100644 index 0000000..fe0bf06 --- /dev/null +++ b/base-config.yaml @@ -0,0 +1,7 @@ +sender_addr: "mail@tld.com" +sender_passwd: "super_password123" +smtp_server: "mail.stmp.com" +smtp_port: 587 + +send_to: "mail.tld.com" + diff --git a/botMail.py b/botMail.py new file mode 100644 index 0000000..9e8c1c8 --- /dev/null +++ b/botMail.py @@ -0,0 +1,61 @@ +from typing import Type +from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper +from maubot import Plugin, MessageEvent +from maubot.handlers import command + +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import email +import email.encoders + +import re + +class Config(BaseProxyConfig): + def do_update(self, helper: ConfigUpdateHelper) -> None: + helper.copy("sender_addr") + helper.copy("sender_passwd") + helper.copy("stmp_server") + helper.copy("smtp_port") + helper.copy("send_to") + +class MailBot(Plugin): + async def start(self) -> None: + #self.on_external_config_update() + self.config.load_and_update() + + @command.new(name="mail") + @command.argument("pattern", pass_raw=True, required=True) + async def command(self, evt: MessageEvent, pattern: str) -> None: + await evt.mark_read() + try: + txtemail = re.search('<(.*)>', pattern) + subject = txtemail.group(1) + txtemail = re.sub('<(.*)>',"", pattern) + + smtp_server=smtplib.SMTP(self.config["smtp_server"],self.config["smtp_port"]) + smtp_server.ehlo() + smtp_server.starttls() + smtp_server.ehlo() + smtp_server.login(self.config["sender_addr"], self.config["sender_passwd"]) + + msg = MIMEMultipart() + user = re.search('@(.*):', evt.sender) + msg['From'] = user.group(1) + '<' + self.config["sender_addr"] + '>' + self.log.info(msg['From']) + msg['To'] = self.config["send_to"] + msg['Subject'] = subject + msg['Date'] = email.utils.formatdate(localtime=True) + body = txtemail + msg.attach(MIMEText(body)) + smtp_server.sendmail(msg['From'],msg['To'],msg.as_string()) + self.log.info(msg.as_string()) + await evt.respond('Mail envoyé') + except: + await evt.respond('Erreur envoi mail') + await evt.respond("""!mail Corps du message""") + + + @classmethod + def get_config_class(cls) -> Type[BaseProxyConfig]: + return Config diff --git a/maubot.yaml b/maubot.yaml new file mode 100644 index 0000000..b0bb3b5 --- /dev/null +++ b/maubot.yaml @@ -0,0 +1,9 @@ +maubot: 0.1.0 +id: tfa.mailBot +version: 0.0.1 +license: AGPL-3.0-or-later +modules: +- botMail +main_class: MailBot +extra_files: +- base-config.yaml