# Path: amivapi/events/utils.py
# def get_token_secret():
#     db = current_app.data.driver.db['config']
#     result = db.find_one({'TOKEN_SECRET': {'$exists': True}})
#     return result['TOKEN_SECRET']
# 
# Path: amivapi/utils.py
# def mail(to, subject, text):
#     """Send a mail to a list of recipients.
# 
#     The mail is sent from the address specified by `API_MAIL` in the config,
#     and the subject formatted according to `API_MAIL_SUBJECT`.
# 
# 
#     Args:
#         to(list of strings): List of recipient addresses
#         subject(string): Subject string
#         text(string): Mail content
#     """
#     sender = app.config['API_MAIL']
#     subject = app.config['API_MAIL_SUBJECT'].format(subject=subject)
# 
#     if app.config.get('TESTING', False):
#         app.test_mails.append({
#             'subject': subject,
#             'from': sender,
#             'receivers': to,
#             'text': text
#         })
#     elif config.SMTP_SERVER and config.SMTP_PORT:
#         msg = MIMEText(text)
#         msg['Subject'] = subject
#         msg['From'] = sender
#         msg['To'] = ';'.join([to] if isinstance(to, str) else to)
# 
#         try:
#             with smtplib.SMTP(config.SMTP_SERVER,
#                               port=config.SMTP_PORT,
#                               timeout=config.SMTP_TIMEOUT) as smtp:
#                 status_code, _ = smtp.starttls()
#                 if status_code != 220:
#                     app.logger.error("Failed to create secure "
#                                      "SMTP connection!")
#                     return
# 
#                 if config.SMTP_USERNAME and config.SMTP_PASSWORD:
#                     smtp.login(config.SMTP_USERNAME, config.SMTP_PASSWORD)
#                 else:
#                     smtp.ehlo()
# 
#                 try:
#                     smtp.sendmail(msg['From'], to, msg.as_string())
#                 except smtplib.SMTPRecipientsRefused:
#                     error = ("Failed to send mail:\n"
#                              "From: %s\nTo: %s\n"
#                              "Subject: %s\n\n%s")
#                     app.logger.error(error % (sender, str(to), subject, text))
#         except smtplib.SMTPException as e:
#             app.logger.error("SMTP error trying to send mails: %s" % e)


from flask import current_app, url_for
from itsdangerous import URLSafeSerializer
from amivapi.events.utils import get_token_secret
from amivapi.utils import mail
             name=name,
             title=event.get('title_en') or event.get('title_de'),
             link=deletion_link,
             deadline=event['time_register_end'].strftime('%H.%M %d.%m.%Y')))


def send_confirmmail_to_unregistered_users(items):
    """Send a confirmation email for external signups(email only)

    Args:
        item: The item, which was just inserted into the database
    """
    for item in items:
        if item.get('user') is None:
            event = current_app.data.find_one(
                'events', None,
                **{current_app.config['ID_FIELD']: item['event']})

            title = event.get('title_en') or event.get('title_de')

            s = URLSafeSerializer(get_token_secret())
            token = s.dumps(str(item['_id']))

            if current_app.config.get('SERVER_NAME') is None:
                current_app.logger.warning("SERVER_NAME is not set. E-Mail "
                                           "links will not work!")

            confirm_link = url_for('emails.on_confirm_email', token=token,
                                   _external=True)


# Path: amivapi/events/utils.py
# def get_token_secret():
#     db = current_app.data.driver.db['config']
#     result = db.find_one({'TOKEN_SECRET': {'$exists': True}})
#     return result['TOKEN_SECRET']
# 
# Path: amivapi/utils.py
# def mail(to, subject, text):
#     """Send a mail to a list of recipients.
# 
#     The mail is sent from the address specified by `API_MAIL` in the config,
#     and the subject formatted according to `API_MAIL_SUBJECT`.
# 
# 
#     Args:
#         to(list of strings): List of recipient addresses
#         subject(string): Subject string
#         text(string): Mail content
#     """
#     sender = app.config['API_MAIL']
#     subject = app.config['API_MAIL_SUBJECT'].format(subject=subject)
# 
#     if app.config.get('TESTING', False):
#         app.test_mails.append({
#             'subject': subject,
#             'from': sender,
#             'receivers': to,
#             'text': text
#         })
#     elif config.SMTP_SERVER and config.SMTP_PORT:
#         msg = MIMEText(text)
#         msg['Subject'] = subject
#         msg['From'] = sender
#         msg['To'] = ';'.join([to] if isinstance(to, str) else to)
# 
#         try:
#             with smtplib.SMTP(config.SMTP_SERVER,
#                               port=config.SMTP_PORT,
#                               timeout=config.SMTP_TIMEOUT) as smtp:
#                 status_code, _ = smtp.starttls()
#                 if status_code != 220:
#                     app.logger.error("Failed to create secure "
#                                      "SMTP connection!")
#                     return
# 
#                 if config.SMTP_USERNAME and config.SMTP_PASSWORD:
#                     smtp.login(config.SMTP_USERNAME, config.SMTP_PASSWORD)
#                 else:
#                     smtp.ehlo()
# 
#                 try:
#                     smtp.sendmail(msg['From'], to, msg.as_string())
#                 except smtplib.SMTPRecipientsRefused:
#                     error = ("Failed to send mail:\n"
#                              "From: %s\nTo: %s\n"
#                              "Subject: %s\n\n%s")
#                     app.logger.error(error % (sender, str(to), subject, text))
#         except smtplib.SMTPException as e:
#             app.logger.error("SMTP error trying to send mails: %s" % e)


# Path: amivapi/events/emails.py
from flask import current_app, url_for
from itsdangerous import URLSafeSerializer
from amivapi.events.utils import get_token_secret
from amivapi.utils import mail

             name=name,
             title=event.get('title_en') or event.get('title_de'),
             link=deletion_link,
             deadline=event['time_register_end'].strftime('%H.%M %d.%m.%Y')))


def send_confirmmail_to_unregistered_users(items):
    """Send a confirmation email for external signups(email only)

    Args:
        item: The item, which was just inserted into the database
    """
    for item in items:
        if item.get('user') is None:
            event = current_app.data.find_one(
                'events', None,
                **{current_app.config['ID_FIELD']: item['event']})

            title = event.get('title_en') or event.get('title_de')

            s = URLSafeSerializer(get_token_secret())
            token = s.dumps(str(item['_id']))

            if current_app.config.get('SERVER_NAME') is None:
                current_app.logger.warning("SERVER_NAME is not set. E-Mail "
                                           "links will not work!")

            confirm_link = url_for('emails.on_confirm_email', token=token,
                                   _external=True)


            mail([item['email']],