gwaag
Posts: 65
Joined: Tue Apr 08, 2014 5:06 pm

how to send email after an event

Tue Sep 25, 2018 4:16 am

Hello,
I installed
apt-get install ssmtp
apt-get install mailutils
chmod 774 /etc/ssmtp/ssmtp.conf done and edited / configured, properly.

I am able to send / recive a email from command line like this:
echo “Controme is down” | mail -s “ContromeWifidown” [email protected]


The below python script works, no problem.

How can I now incorparate to send email in below python script, that if "Wifi off", it sends a email??

Code: Select all

#!/usr/bin/env python3

import subprocess as sp
import os
from time import sleep



status, result = sp.getstatusoutput("ping -c1 -w2 192.168.0.2")   # ping Contromegateway

if(status == 0):
    print("wifi_on")
else:
    print("wifi_off")

HERE CODE LINE FOR EMAIL PLEASE

    sleep(1)
    os.system("pilight-send -p kaku_switch -i 23317358 -u 1 -f")   #  off 3x
    sleep(2)
    os.system("pilight-send -p kaku_switch -i 23317358 -u 1 -f")
    sleep(2)
    os.system("pilight-send -p kaku_switch -i 23317358 -u 1 -f")

    sleep(5)

    os.system("pilight-send -p kaku_switch -i 23317358 -u 1 -t")   #  on 3x
    sleep(2)
    os.system("pilight-send -p kaku_switch -i 23317358 -u 1 -t")
    sleep(2)
    os.system("pilight-send -p kaku_switch -i 23317358 -u 1 -t")
    sleep(60)



Andyroo
Posts: 240
Joined: Sat Jun 16, 2018 12:49 am
Location: Side of the hill in Lincolnshire

Re: how to send email after an event

Tue Sep 25, 2018 11:43 am

If you ant to use the command line then have a look at the python subprocess command - this is used to call command line routines from python.

A full explanation is to be found at https://docs.python.org/2/library/subprocess.html


If you want to use a Python module then https://www.pythonforbeginners.com/code ... end-email/ will help.

Let us know how you get on.
Need Pi spray - these things are breeding in my house...

gwaag
Posts: 65
Joined: Tue Apr 08, 2014 5:06 pm

Re: how to send email after an event

Tue Sep 25, 2018 5:16 pm

Hello,
will this work??

#!/usr/bin/env python3

import subprocess as sp
import os
from time import sleep
import smtplib


status, result = sp.getstatusoutput("sudo ping -c1 -w2 192.168.0.2") # ping Contromegateway

Code: Select all

if(status == 0):
    print("wifi_on")
else:
    print("wifi_off")

    s = smtplib.SMTP('smtp server')
    s.login('[email protected]' , 'xxxx')
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.quit
    print ("Email sent")

Andyroo
Posts: 240
Joined: Sat Jun 16, 2018 12:49 am
Location: Side of the hill in Lincolnshire

Re: how to send email after an event

Tue Sep 25, 2018 5:48 pm

Have you had a look at THONNY on the Pi as it lets you step through each line of code to help you debug the program?

The line that will give you an issue is:

Code: Select all

s.sendmail(msg['From'], msg['To'], msg.as_string())
as you have not defined From, To and msg

The simplest one I can point you to is http://effbot.org/pyfaq/how-do-i-send-m ... script.htm. Obviously you will need to change server / to / from etc :lol: (i'm being a pain - I know but its a better learning experience to read code than have it on a plate)
Need Pi spray - these things are breeding in my house...

gwaag
Posts: 65
Joined: Tue Apr 08, 2014 5:06 pm

Re: how to send email after an event

Tue Sep 25, 2018 6:03 pm

Hello,
now it works ok.

Code: Select all

#!/usr/bin/env python3

import time
import socket
import datetime
import sys
import subprocess as sp
import os
from time import sleep
import smtplib
from email.mime.text import MIMEText

status, result = sp.getstatusoutput("sudo ping -c1 -w2 192.168.0.2")   # ping Contromegateway

if(status == 1):
    print("wifi_on")
else:
    print("wifi_off")

#create email
    message = """Wifi Controme down"""
    msg = MIMEText(message)
    msg['subject'] = 'Problem mit Controme'
    msg['from'] = '[email protected]'
    msg['to'] = '[email protected]'
    msg['Date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
# send mail
    s = smtplib.SMTP('mail.spinternet.ch')
    s.starttls()
    s.login('[email protected]' , 'xxxx')
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.quit
    print ("Email sent")

Return to “Beginners”

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], gcanturk, Google [Bot] and 37 guests