def send_alerts(): sent = [] with ConnectionContext(safe=False): for alert in Alert.alerts_to_send(): try: send_email(**alert.email_data) alert.update_sent() sent.append(alert.email) except ConnectionError: pass return jsonify({'emails_sent': sent})