def send_email(): # check the last time an email was sent. if the duration is # above 7 days, then send an email. else do nothing latest_email, date = Email.get_latest(Email) duration = datetime.timedelta(0) if latest_email: today = datetime.datetime.now() duration = today - date if duration > datetime.timedelta(7): # get sales and purchases sales = Sale.filter(Sale, "all", "", "", date, datetime.datetime(9999, 12, 31)) total_sales = utils.compute_sales(sales) purchases = Purchase.filter(Purchase, "", date, datetime.datetime(9999, 12, 31)) total_purchases = utils.compute_purchases(purchases) # get boss' email address boss_email = current_user.manager.boss_email # send email msg = Message("Weekly sales and purchases", sender='*****@*****.**', recipients=[boss_email]) msg.html = render_template('manager/sales_and_purchases_email.html', sales=sales, purchases=purchases, total_sales=total_sales, total_purchases=total_purchases, date=date) try: mail.send(msg) Email() # return render_template('manager/sales_and_purchases_email.html', sales=sales, purchases=purchases, total_sales=total_sales, total_purchases=total_purchases, date=date) except: pass return redirect(url_for('user.login'))
def reset_password(): if request.method == "POST": email = request.form.get("email") if email: manager = Manager.get_by_email(Manager, email) if manager: # send email new_password = randomString() msg = Message("Password Reset", sender='*****@*****.**', recipients=[email]) msg.body = "Hi " + manager.first_name + " " + manager.last_name + ",\nYour new password is: " + new_password + "\nYour username is: " + manager.user.username try: mail.send(msg) flash("Your Password has been changed", "info") flash( "The reset email has been sent to '" + email + "'. Please sign into your email account to get a new password", 'success') manager.update_password(new_password) except: flash( "Failed! Reset email could not be sent. Make sure you're online and try again", "danger") else: flash("Incorrect Email", 'danger') else: flash("Invalid Email", 'danger') return redirect(url_for('manager.reset_password')) elif request.method == "GET": return render_template('global/reset-password.html', mod=module)
def send_reset_email(user): token = user.get_reset_token() msg = Message('Password Reset Request', sender='*****@*****.**', recipients=[user.email]) msg.body = f''' To reset your password, click the following link, or copy and paste it into your web browser: {url_for('reset_token', token=token, _external=True)} If you did not make this request then please ignore this email.''' mail.send(msg)
def send_email_text(title, recipients, text): try: with app.app_context(): message = Message( subject=title, recipients=recipients, html=text ) mail.send(message) except Exception as e: logger.warning(f"sending email error: {e}")
def send_email(title, recipients, template, context): try: with app.app_context(): message = Message( subject=title, recipients=recipients, html=render_template(template, **context) ) mail.send(message) except Exception as e: logger.warning(f"Sending email error: {e}")