How to send email to the user that is logged to warn the user

33 views Asked by At

I create a flask web app that monitors the gas value and current value. Also, it includes the graph for gas value and current value, the status of led, buzzer, and solenoid. When it reaches at a certain threshold, it will email the user that is logged in in the web app but the error is it doesn't send any email. and it displays when I run the code:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.9/site-packages/flask/app.py", line 1458, in wsgi_app
    response = self.handle_exception(e)
  File "/home/pi/.local/lib/python3.9/site-packages/flask/app.py", line 1455, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/pi/.local/lib/python3.9/site-packages/flask/app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/pi/.local/lib/python3.9/site-packages/flask/app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/pi/.local/lib/python3.9/site-packages/flask/app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/pi/Desktop/FLASK WEB APP/website/views.py", line 110, in current_value
    send_email(current_user.email, 'High Current Value Alert', 'Current value has exceeded 0.2.')
  File "/home/pi/Desktop/FLASK WEB APP/website/views.py", line 63, in send_email
    mail.send(msg)
  File "/home/pi/.local/lib/python3.9/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/home/pi/.local/lib/python3.9/site-packages/flask_mail.py", line 427, in send
    connection.send(self)
  File "/home/pi/.local/lib/python3.9/site-packages/flask_mail.py", line 190, in send
    message.as_bytes() if PY3 else message.as_string(),
  File "/home/pi/.local/lib/python3.9/site-packages/flask_mail.py", line 385, in as_bytes
    return self._message().as_bytes()
  File "/home/pi/.local/lib/python3.9/site-packages/flask_mail.py", line 307, in _message
    ascii_attachments = current_app.extensions['mail'].ascii_attachments
KeyError: 'mail'

This is what I put in my code to send email to the user:

def send_email(recipient, subject, body):
    msg = Message(subject, sender= '[email protected]', recipients=[recipient])
    msg.body = body
    mail.send(msg)

@views.route('/gas_value')
def gas_value():
    gas_value = mq6_modules[0].mq.MQPercentage()["GAS_LPG"]   # Assuming the first instance is used
    if gas_value >= 0.2:
        gas_reading = GasReading(value=gas_value)
        db.session.add(gas_reading)
        db.session.commit()
        send_email(current_user.email, 'High Gas Value Alert', 'Gas value has exceeded 0.2.')
    return jsonify({'gas_value': gas_value})

@views.route('/current_value')
def current_value():
    current_value = powers[0].get_electricalsensor_readings()
    if current_value is not None:
        current_value_float = float(current_value)  # Convert current_value to float
    
        # Display the current value continuously
        response = {'current_value': current_value}

        # Store in the database if the value is greater than or equal to 0.2
        if current_value_float >= 0.2:
            current_reading = CurrentReading(value=current_value_float)
            db.session.add(current_reading)
            db.session.commit()
            send_email(current_user.email, 'High Current Value Alert', 'Current value has exceeded 0.2.')

        return jsonify(response)
    else:
        return jsonify({'current_value': 'Unavailable'})

here is the link for my whole program: text

0

There are 0 answers