commit 85d38dfdf7722b7427762d7a5136b6b720f3cc00 Author: Niklas Meinzer Date: Wed Dec 18 22:20:03 2024 +0100 Add initial script and email template diff --git a/email.txt b/email.txt new file mode 100644 index 0000000..c5200cb --- /dev/null +++ b/email.txt @@ -0,0 +1,12 @@ +Hallo {name}, + +Wir haben dir ein Nutzerkonto für die neue Allmende Cloud angelegt. Die Zugangsdaten lauten: + +Nutzername: {username} +Passwort: {password} + +Du kannst dich damit unter https://cloud.allmende-gufi.de einloggen. Bitte ändere beim ersten Einloggen dein Passwort. + +Viele Grüße + +AG IT \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..8199702 --- /dev/null +++ b/main.py @@ -0,0 +1,137 @@ +import argparse +import secrets +import smtplib +import string +import subprocess +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + + +def generate_password(length: int) -> str: + alphabet = string.ascii_letters + string.digits + password = "".join(secrets.choice(alphabet) for i in range(length)) + return password + + +def send_email( + sender_email, + receiver_email, + subject, + message, + smtp_username, + smtp_password, +): + # Create a multipart message + msg = MIMEMultipart() + msg["From"] = sender_email + msg["To"] = receiver_email + msg["Subject"] = subject + + # Add body to the email + msg.attach(MIMEText(message, "plain")) + + try: + # Create a secure SSL/TLS connection to the SMTP server + server = smtplib.SMTP("localhost", 25) + server.ehlo() + server.starttls() + server.ehlo() + + # Login to the SMTP server + server.login(smtp_username, smtp_password) + + # Send the email + server.sendmail(sender_email, receiver_email, msg.as_string()) + + # Close the SMTP connection + server.quit() + + print("Email sent successfully!") + except Exception as e: + print("Failed to send email. Error:", str(e)) + + +def create_user( + name: str, + email: str, + mail_text: str, + mail_user: str, + mail_password: str, + dry_run: bool = False, +): + username = name.split()[0].lower() + "." + name.split()[1].lower()[0] + password = generate_password(20) + + cmd = [ + "yunohost", + "user", + "create", + username, + "--password", + password, + ] + if dry_run: + print(" ".join(cmd)) + else: + result = subprocess.run(cmd) + result.check_returncode() + + cmd = [ + "yunohost", + "user", + "update", + username, + "--add-mailforward", + email, + ] + if dry_run: + print(" ".join(cmd)) + else: + result = subprocess.run(cmd) + result.check_returncode() + + mail_text = mail_text.replace("{name}", name) + mail_text = mail_text.replace("{username}", username) + mail_text = mail_text.replace("{password}", password) + + if dry_run: + print(mail_text) + else: + send_email( + f"{mail_user}@cloud.allmende-gufi.de", + email, + "Dein Zugang zur neuen Allmende Cloud", + mail_text, + mail_user, + mail_password, + ) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Creates Yunohost user from a csv file and sends them an email with the password", + ) + parser.add_argument("filename", help="Input file name") + parser.add_argument( + "mail_user", help="This user will be used to send the confirmation mail" + ) + parser.add_argument( + "mail_password", help="This password will be used to send the confirmation mail" + ) + parser.add_argument("--dry-run", action="store_true") + args = parser.parse_args() + + with open(args.filename, "r") as f: + data = f.readlines() + + with open("./email.txt", "r") as f: + email_text = f.read() + + for line in data: + create_user( + *line.strip().split(","), + mail_text=email_text, + mail_user=args.mail_user, + mail_password=args.mail_password, + dry_run=args.dry_run, + )