def email_invoices_timer_callback(): logger.info("email_invoices_timer_callback()..") with app.app_context(): invoices = Invoice.all_with_email_and_not_terminated(db.session) for invoice in invoices: order = bronze_order_status(invoice) if order: if invoice.status != order["status"]: invoice_url = url_for("invoice", token=invoice.token) hostname = urllib.parse.urlparse(invoice_url).hostname sender = "no-reply@" + hostname formatted_amount = '{0:0.2f}'.format(invoice.amount / 100.0) # send email msg = Message('ZAP bill payment status updated', sender=sender, recipients=[invoice.email]) msg.html = 'Invoice <a href="{}">{}</a> has updated the {} invoice with the amount of ${} to status "{}"'.format( invoice_url, invoice.token, invoice.utility_name, formatted_amount, order["status"]) msg.body = 'Invoice {} has updated the {} invoice with the amount of ${} to status {}'.format( invoice_url, invoice.utility_name, formatted_amount, order["status"]) mail.send(msg) # update invoice object invoice.status = order["status"] db.session.commit()