Alert Mail Raspberry Pi

Alert Mail Raspberry Pi

Temperatures above 80 °C can damage your raspberry pi.

Especially if your using your pi as a webserver, webcam-stream or as a display system, in a 24 hour-operation, you may want to get warned via Email. Getting a notification from your pi is pretty simple, just use the script below:

If you have any questions about the notification system or any other Pi Topic, feel free to comment.
Cheers Alex


27 thoughts on “Alert Mail Raspberry Pi

    1. You need to give it a .py Extension. You could put in the root or home directory (I would recommend). To Run: Sudo python You may want to autostart the script to:~/.config/lxsession/LXDE/autostart

      I hope this helps you out.


    2. i got a error on line 21

      root@ruudberry:~# python
      File “”, line 21
      server.login(“raspberrypi”, “qwerty123”)
      IndentationError: unindent does not match any outer indentation level


  1. I would recommend you to copy the script from above in a new *.py file, because I’ve made a few changes, hopefully it’s now more clearly. Make sure to have the same indentation that I have.

    If this does not help, try this:

    Another reason why you got the “Server login error”, might be caused by a wrong username, password or server. If you could tell me your email provider, I could help you editing the script for your needs. Please let me know if you have any issues.

    You don’t need to have a mail program because we are importing the smpt library. 🙂


  2. :-))))))))

    Critical warning! The actual temperature is: 45.5

    It’s working!
    (i put it for the test to 10 degrees)

    Thanks for the support!

    Best regards,


    1. We thank you for the help to improve the article. We hope you enjoy to work with the Raspberry Pi.

      Best regards,

      Michael from quaint project.


  3. Hi Alex,

    I have changed my password, it’s a little bit longer, but now i get the following error.

    File “/root/”, line 25
    server.login(“”, “&(“LN3KXJWLYQL0ZQPQrrKmGFqUOneq3OMUkKEBWxHP38SKkC3egrffIfq3”)
    SyntaxError: invalid syntax

    Can you tell me how to solve this issue?

    Thanks in advance.


    1. Hey there,
      sorry for my verrrry late reply. It seems like I’ve missed your comment. The problem is that you have a quotation mark in your password and this is normally the beginning and ending of a string. If you still want to solve it you could put it like this:

      server.login(‘’, ‘&(“LN3KXJWLYQL0ZQPQrrKmGFqUOneq3OMUkKEBWxHP38SKkC3egrffIfq3’)

      These single quotes have a higher priority than the quotation mark.

      Best Alex


  4. Hello, I know it’s not a recent post, but how does the script work if I launch it with autostart? Will it check température as it evolves ans then stop zone the alert is done, or will it just check the temp at startup ans stop whatever is thé result of the temp?
    Just to know if it’s better to do it in @reboot with cron or every hour. Yeah i’m a noob trying to figure things 🙂
    Thank you for this script, it’s the main thing I want to monitor on the rpi.


    1. Hey there,
      thanks for you comment, you’ve luck this post is still active!

      there are basically two ways how you could do it.
      1. Modify the script to run a endless While loop with the condition true. This would run the script as long as the script is not interrupted. (The temperature would be checked every loop).

      2. You could make a cronjob that runs every 30 minutes (no need to reboot) and call the script. In my opinion this method would have the benefit that you wouldn’t get a view hundred mails in a really short time.

      Right now the Script the script starts and checks if the temperature is to high -> send mail, if not it simply ends the script.

      I hope my comment helps to solve all your questions! If not, feel free to reply again.



    2. Hello, thank you! It’s a perfect answer. From my understanding of python it’s what I suspected ans what I checked after blowing on my rPi to play with the temp ans see the script had ended on the trial value :).
      So I made a cronjob every 30mn and it’s allright for my needs.
      Thanks for the answer and great website by the way, a little bit advanced for my level but that’s the way to learn :), already bookmarked.


    3. Thanks so much for your kind words! I can tell you from all of our crew members that we all were once beginners with the Raspberry Pi. If you have any topic we could cover or wich meets your interests feel free to tell us. Thanks for bookmarking!


  5. Hi Codewarrior, I was able to get the script to work. Like it !
    Actually, I am also looking for a script that monitors several GPIO Ports and emails me when things change (NC/NO).
    I know there i a lot on the web, but there is also a lot of crap and stuff where people assume you are a top programmer and understand everything from nature. If I see something it crashes my Raspberry, or doesn’t do anything at all.

    Remarkable thing is, yours simply works !
    That’s why I use it now and was wondering if you can help me with the multiple NC/NO GPIOs monitoring.

    What it needs to do:
    – monitor a switch (open/closed) and email me about it.
    – monitor water level to prevent flooding and email me about it
    – stay “active” no cron jobs, no reboot, just like a background service.

    If you can help me or guide me to something that simply WORKS, I would appreciate it !


    1. Hi 🙂 ,
      a fundamental knowledge about electronic and computer science and most importantly curiosity is definitely not bad for your advanced “Pi” project.
      My recommendations:
      1. checkout wiringpi as a first approach
      2. Using the Raspberry PI for a “professional” Application, mission critical task is a rather bad idea..
      2.1 Tip: You can generate alternating current so the electrolysis will not kill the wires. You then pass the AC through a diode and apply fi it with some NAND gate. ……

      Greetings Michael


    2. Yunicx,

      Thanks for your comment. Please apologise my late response.

      For the project you’d mentioned I would go for a python scripted solution. I would basically create a (infinite) loop that checks the GPIO ports and calls another function to send out those emails, if the measured values are too high.
      I can only assists Michael’s recommendation that wiringpi would be the way to go to monitor the GPIO ports.

      There are several ways to run a script in the background. You could either use nohup & or you could use Pythons fork() function. Which basically runs a script as a deamon.

      Which component are you using for measuring the water height?

      Please let us know if you have any additional questions!



  6. Hi, please please help me. Im very new to RasPi and I´m trying to use your script with no success. What I did is first: nano, once there I added this script:


    import os
    import smtplib
    from email.mime.text import MIMEText

    At First we have to get the current CPU-Temperature with this defined function

    def getCPUtemperature():
     res = os.popen(‘vcgencmd measure_temp’).readline()

    Now we convert our value into a float number

    temp = float(getCPUtemperature())

    Check if the temperature is abouve 60°C (you can change this value, but it shouldn’t be above 70)

    if (temp >60):
     # Enter your smtp Server-Connection
     server = smtplib.SMTP(‘’, 587)
     #if your using gmail:
     # Login
     server.login(“”, “MYACTUALPASSWORD”)
     # Now comes the Text we want to send:
     value = “URGENTE! La temperatura es: ” + getCPUtemperature()
     msg = MIMEText(value)
     # The Subject of your E-Mail
     msg[‘Subject’] = “URGENTE! Temperatura: ” + getCPUtemperature()
     # Consigner of your E-Mail
     msg[‘From’] = “Raspberry Pi”
     # recipient of your E-Mail
     msg[‘To’] = “”
     # Finally send the mail
     server.sendmail(“”, “”, msg.as_string())
     print “Everything was working fine! Best regards

    As you can see it is in Spanish, but there is something wrong going on here. I get the following error:

    pi@retropie:~ $ sudo python
    File “”, line 5
    SyntaxError: invalid syntax

    What is wrong here?



    1. Hi there,

      I’ve uploaded the script to GitHub’s Gist. The syntax error you’d faced has to do with wrong indention. (WP sometimes messes up the format). In Python it’s necessary to indent the body of a function or a if either for blank spaces or one tab.

      I hope this helps.



    2. I dont understand your reply. What can I do in order to use your script?

      Sorry but as I mentioned Im COMPLETYL NEW. Can you give me STEP by STEP instructions on how to do this from SSH or VNC remote connection?

      Thanks a lot


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.