<commit_before>import smtplib
import pystache
import os
from email.mime.text import MIMEText
from werkzeug.utils import secure_filename

from KerbalStuff.database import db
from KerbalStuff.objects import User
from KerbalStuff.config import _cfg, _cfgi

def send_confirmation(user):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    with open("emails/confirm-account") as f:
        message = MIMEText(pystache.render(f.read(), { 'user': user, "domain": _cfg("domain"), 'confirmation': user.confirmation }))
    message['X-MC-Important'] = "true"
    message['Subject'] = "Welcome to Kerbal Stuff!"
    message['From'] = "<EMAIL>"
    message['To'] = user.email
    smtp.sendmail("<EMAIL>", [ user.email ], message.as_string())
    smtp.quit()

def send_reset(user):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    with open("emails/password-reset") as f:
        message = MIMEText(pystache.render(f.read(), { 'user': user, "domain": _cfg("domain"), 'confirmation': user.passwordReset }))
    message['X-MC-Important'] = "true"
    message['Subject'] = "Reset your password on Kerbal Stuff"
    message['From'] = "<EMAIL>"
    message['To'] = user.email
    smtp.sendmail("<EMAIL>", [ user.email ], message.as_string())
    smtp.quit()

def send_update_notification(mod, version):
    if _cfg("smtp-host") == "":
        return
    followers = [u.email for u in mod.followers]
    changelog = mod.default_version().changelog
    if changelog:
        changelog = '\n'.join(['    ' + l for l in changelog.split('\n')])

    for follower in followers:
        smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
        smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
        with open("emails/mod-updated") as f:
            message = MIMEText(pystache.render(f.read(),
                {
                    'mod': mod,
                    'domain': _cfg("domain"),
                    'latest': version,
                    'url': '/mod/' + str(mod.id) + '/' + secure_filename(mod.name)[:64],
                    'changelog': changelog
                }))
        message['Subject'] = mod.user.username + " has just updated " + mod.name + "!"
        message['From'] = "<EMAIL>"
        message['To'] = follower
        smtp.sendmail("<EMAIL>", [ follower ], message.as_string())
        smtp.quit()

def send_autoupdate_notification(mod):
    if _cfg("smtp-host") == "":
        return
    followers = [u.email for u in mod.followers]
    changelog = mod.default_version().changelog
    if changelog:
        changelog = '\n'.join(['    ' + l for l in changelog.split('\n')])

    for follower in followers:
        smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
        smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
        with open("emails/mod-autoupdated") as f:
            message = MIMEText(pystache.render(f.read(),
                {
                    'mod': mod,
                    'domain': _cfg("domain"),
                    'latest': mod.default_version(),
                    'url': '/mod/' + str(mod.id) + '/' + secure_filename(mod.name)[:64],
                    'changelog': changelog
                }))
        message['Subject'] = mod.name + " is compatible with KSP " + mod.versions[0].ksp_version + "!"
        message['From'] = "<EMAIL>"
        message['To'] = follower
        smtp.sendmail("<EMAIL>", [ follower ], message.as_string())
        smtp.quit()

def send_bulk_email(users, subject, body):
    if _cfg("smtp-host") == "":
        return
    for u in users:
        smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
        smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
        message = MIMEText(body)
        message['Subject'] = subject
        message['From'] = "<EMAIL>"
        message['To'] = u
        smtp.sendmail("<EMAIL>", [ u ], message.as_string())
        smtp.quit()<commit_msg>Improve ability to send emails in bulk<commit_after>import smtplib
import pystache
import os
from email.mime.text import MIMEText
from werkzeug.utils import secure_filename

from KerbalStuff.database import db
from KerbalStuff.objects import User
from KerbalStuff.config import _cfg, _cfgi

def send_confirmation(user):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    with open("emails/confirm-account") as f:
        message = MIMEText(pystache.render(f.read(), { 'user': user, "domain": _cfg("domain"), 'confirmation': user.confirmation }))
    message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = "Welcome to Kerbal Stuff!"
    message['From'] = "<EMAIL>"
    message['To'] = user.email
    smtp.sendmail("<EMAIL>", [ user.email ], message.as_string())
    smtp.quit()

def send_reset(user):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    with open("emails/password-reset") as f:
        message = MIMEText(pystache.render(f.read(), { 'user': user, "domain": _cfg("domain"), 'confirmation': user.passwordReset }))
    message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = "Reset your password on Kerbal Stuff"
    message['From'] = "<EMAIL>"
    message['To'] = user.email
    smtp.sendmail("<EMAIL>", [ user.email ], message.as_string())
    smtp.quit()

def send_update_notification(mod, version):
    if _cfg("smtp-host") == "":
        return
    followers = [u.email for u in mod.followers]
    changelog = mod.default_version().changelog
    if changelog:
        changelog = '\n'.join(['    ' + l for l in changelog.split('\n')])

    targets = list()
    for follower in followers:
        targets.append(follower)
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    with open("emails/mod-updated") as f:
        message = MIMEText(pystache.render(f.read(),
            {
                'mod': mod,
                'domain': _cfg("domain"),
                'latest': version,
                'url': '/mod/' + str(mod.id) + '/' + secure_filename(mod.name)[:64],
                'changelog': changelog
            }))
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = mod.user.username + " has just updated " + mod.name + "!"
    message['From'] = "<EMAIL>"
    message['To'] = ";".join(targets)
    smtp.sendmail("<EMAIL>", targets, message.as_string())
    smtp.quit()

def send_autoupdate_notification(mod):
    if _cfg("smtp-host") == "":
        return
    followers = [u.email for u in mod.followers]
    changelog = mod.default_version().changelog
    if changelog:
        changelog = '\n'.join(['    ' + l for l in changelog.split('\n')])

    targets = list()
    for follower in followers:
        targets.append(follower)
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    with open("emails/mod-autoupdated") as f:
        message = MIMEText(pystache.render(f.read(),
            {
                'mod': mod,
                'domain': _cfg("domain"),
                'latest': mod.default_version(),
                'url': '/mod/' + str(mod.id) + '/' + secure_filename(mod.name)[:64],
                'changelog': changelog
            }))
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = mod.name + " is compatible with KSP " + mod.versions[0].ksp_version + "!"
    message['From'] = "<EMAIL>"
    message['To'] = ";".join(targets)
    smtp.sendmail("<EMAIL>", targets, message.as_string())
    smtp.quit()

def send_bulk_email(users, subject, body):
    if _cfg("smtp-host") == "":
        return
    targets = list()
    for u in users:
        targets.append(u)
    smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
    smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    message = MIMEText(body)
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = subject
    message['From'] = "<EMAIL>"
    message['To'] = ";".join(targets)
    smtp.sendmail("<EMAIL>", targets, message.as_string())
    smtp.quit()
{'fraction_non_alphanumeric': 0.1757405456035593, 'fraction_numerical': 0.001170823088631308, 'mean_word_length': 2.806595365418895, 'pattern_counts': {'":': 14, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2443588', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2834, 'n_words': 542}