Пример #1
0
def send_email(subject, body=None, html=None, recipients=None, throttle=None):
    """Send an email. Optionally throttle the amount an identical email goes out.

    If the throttle argument is set, an md5 checksum derived from the subject, body, html, and recipients is stored in
    Redis with a lock timeout. On the first email sent, the email goes out like normal. But when other emails with the
    same subject, body, html, and recipients is supposed to go out, and the lock hasn't expired yet, the email will be
    dropped and never sent.

    Positional arguments:
    subject -- the subject line of the email.

    Keyword arguments.
    body -- the body of the email (no HTML).
    html -- the body of the email, can be HTML (overrides body).
    recipients -- list or set (not string) of email addresses to send the email to. Defaults to the ADMINS list in the
        Flask config.
    throttle -- time in seconds or datetime.timedelta object between sending identical emails.
    """
    recipients = recipients or current_app.config['ADMINS']
    if throttle is not None:
        md5 = hashlib.md5('{}{}{}{}'.format(subject, body, html,
                                            recipients)).hexdigest()
        seconds = throttle.total_seconds() if hasattr(
            throttle, 'total_seconds') else throttle
        lock = redis.lock(EMAIL_THROTTLE.format(md5=md5), timeout=int(seconds))
        have_lock = lock.acquire(blocking=False)
        if not have_lock:
            current_app.logger.debug('Suppressing email: {}'.format(subject))
            return
    msg = Message(subject=subject, recipients=recipients, body=body, html=html)
    mail.send(msg)
Пример #2
0
def send_exception(subject):
    """Send Python exception tracebacks via email to the ADMINS list.

    Use the same HTML styling as Flask tracebacks in debug web servers.

    This function must be called while the exception is happening. It picks up the raised exception with sys.exc_info().

    Positional arguments:
    subject -- subject line of the email (to be prepended by 'Application Error: ').
    """
    # Generate and modify html.
    tb = tbtools.get_current_traceback()  # Get exception information.
    with _override_html():
        html = tb.render_full().encode('utf-8', 'replace')
    html = html.replace('<blockquote>',
                        '<blockquote style="margin: 1em 0 0; padding: 0;">')
    subject = 'Application Error: {}'.format(subject)

    # Apply throttle.
    md5 = hashlib.md5('{}{}'.format(subject, html)).hexdigest()
    seconds = int(current_app.config['MAIL_EXCEPTION_THROTTLE'])
    lock = redis.lock(EMAIL_THROTTLE.format(md5=md5), timeout=seconds)
    have_lock = lock.acquire(blocking=False)
    if not have_lock:
        current_app.logger.debug('Suppressing email: {}'.format(subject))
        return

    # Send email.
    msg = Message(subject=subject,
                  recipients=current_app.config['ADMINS'],
                  html=html)
    mail.send(msg)
Пример #3
0
def send_email(subject, body=None, html=None, recipients=None, throttle=None):
    """Send an email. Optionally throttle the amount an identical email goes out.

    If the throttle argument is set, an md5 checksum derived from the subject, body, html, and recipients is stored in
    Redis with a lock timeout. On the first email sent, the email goes out like normal. But when other emails with the
    same subject, body, html, and recipients is supposed to go out, and the lock hasn't expired yet, the email will be
    dropped and never sent.

    Positional arguments:
    subject -- the subject line of the email.

    Keyword arguments.
    body -- the body of the email (no HTML).
    html -- the body of the email, can be HTML (overrides body).
    recipients -- list or set (not string) of email addresses to send the email to. Defaults to the ADMINS list in the
        Flask config.
    throttle -- time in seconds or datetime.timedelta object between sending identical emails.
    """
    recipients = recipients or current_app.config['ADMINS']
    if throttle is not None:
        md5 = hashlib.md5('{}{}{}{}'.format(subject, body, html, recipients)).hexdigest()
        seconds = throttle.total_seconds() if hasattr(throttle, 'total_seconds') else throttle
        lock = redis.lock(EMAIL_THROTTLE.format(md5=md5), timeout=int(seconds))
        have_lock = lock.acquire(blocking=False)
        if not have_lock:
            current_app.logger.debug('Suppressing email: {}'.format(subject))
            return
    msg = Message(subject=subject, recipients=recipients, body=body, html=html)
    mail.send(msg)
Пример #4
0
def send_exception(subject):
    """Send Python exception tracebacks via email to the ADMINS list.

    Use the same HTML styling as Flask tracebacks in debug web servers.

    This function must be called while the exception is happening. It picks up the raised exception with sys.exc_info().

    Positional arguments:
    subject -- subject line of the email (to be prepended by 'Application Error: ').
    """
    # Generate and modify html.
    tb = tbtools.get_current_traceback()  # Get exception information.
    with _override_html():
        html = tb.render_full().encode('utf-8', 'replace')
    html = html.replace('<blockquote>', '<blockquote style="margin: 1em 0 0; padding: 0;">')
    subject = 'Application Error: {}'.format(subject)

    # Apply throttle.
    md5 = hashlib.md5('{}{}'.format(subject, html)).hexdigest()
    seconds = int(current_app.config['MAIL_EXCEPTION_THROTTLE'])
    lock = redis.lock(EMAIL_THROTTLE.format(md5=md5), timeout=seconds)
    have_lock = lock.acquire(blocking=False)
    if not have_lock:
        current_app.logger.debug('Suppressing email: {}'.format(subject))
        return

    # Send email.
    msg = Message(subject=subject, recipients=current_app.config['ADMINS'], html=html)
    mail.send(msg)
Пример #5
0
def generate_report(siteid,startday,endday):
    '''Create and send report for given site during the specified periodic_task


    '''
    site = Wifisite.query.filter_by(id=siteid).first()
    start_date = startday.format('DD-MM-YYYY')
    end_date   = endday.format('DD-MM-YYYY')
    current_app.logger.debug('Going to process report for :%s from :%s to :%s'%(site.name,start_date,end_date))    
    #get all entries within given period
    entries = Guest.query.filter(and_(Guest.site_id==siteid,Guest.demo ==0, 
                    Guest.created_at >= startday,Guest.created_at <= endday)).all()

    csvList = '\n'.join(','.join(row.to_list()) for row in entries)  


    filename = "Report_%s_to_%s.csv"%(start_date,end_date)  
    attachment = Attachment(filename=filename,content_type='txt/plain',data=csvList)
    msg = Message("Wifi usage report for the period :%s to :%s"%(start_date,end_date),
                    sender=current_app.config['REPORT_EMAIL_SENDER'],
                    recipients=[site.client.email,site.reports_list],attachments =[attachment],bcc=current_app.config['ADMINS'])

    msg.body  = "Dear %s,\n\n"\
            "\tPlease find the wifi usage report for the period of starting from:%s to %s \n"\
            "\nRegards\n"\
            "Admin"%(site.name,start_date,end_date)
    mail.send(msg)
Пример #6
0
def generate_report(siteid, startday, endday):
    '''Create and send report for given site during the specified periodic_task


    '''
    site = Wifisite.query.filter_by(id=siteid).first()
    start_date = startday.format('DD-MM-YYYY')
    end_date = endday.format('DD-MM-YYYY')
    current_app.logger.debug(
        'Going to process report for :%s from :%s to :%s' %
        (site.name, start_date, end_date))
    #get all entries within given period
    entries = Guest.query.filter(
        and_(Guest.site_id == siteid, Guest.demo == 0,
             Guest.created_at >= startday, Guest.created_at <= endday)).all()

    csvList = '\n'.join(','.join(row.to_list()) for row in entries)

    filename = "Report_%s_to_%s.csv" % (start_date, end_date)
    attachment = Attachment(filename=filename,
                            content_type='txt/plain',
                            data=csvList)
    msg = Message("Wifi usage report for the period :%s to :%s" %
                  (start_date, end_date),
                  sender=current_app.config['REPORT_EMAIL_SENDER'],
                  recipients=[site.client.email, site.reports_list],
                  attachments=[attachment],
                  bcc=current_app.config['ADMINS'])

    msg.body  = "Dear %s,\n\n"\
            "\tPlease find the wifi usage report for the period of starting from:%s to %s \n"\
            "\nRegards\n"\
            "Admin"%(site.name,start_date,end_date)
    mail.send(msg)