Banyanss
Posts: 23
Joined: Sat May 13, 2017 8:01 am

Email Notification

Mon Dec 18, 2017 7:06 am

Hello all,

Ive got a pi set up to send an email at a certain point once a condition is met. Its working very well but now I want to expand the idea a little more.

I have been using a desktop email client to run a rule when an email comes in from this pi, however its a bit fiddly as the email client has to be configured for multiple users. Ive managed to get a hold of a small lcd touch screen and thought it would be a better idea for either the email from the original pi to trigger an action on another pi zero set up with the lcd screen.

Now my question: do you guys think that running an email client on the lcd screen with rules (I assume clawsmail and icedove are more than capable here) to flash a notification once a condition is met is the best solution?

Many Thanks

ghans
Posts: 7819
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Email Notification

Mon Dec 18, 2017 8:58 am

Do you know any prgramming language ? Are the Pis always connected to the same wired/wireless/mixed LAN ?


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: Email Notification

Mon Dec 18, 2017 11:29 am

you could use email parser to pick up on a specific part of the email such as sender, subject etc then have your code run if the email is the actual email you want to use and not spam..
this example uses gmail and requires gmail settings to allow less secure apps and allow use of pop3. it sends an email to itself, then attempts to read said email(email delay may prevent a read first time around)

Code: Select all

import poplib
from email import parser
import smtplib

frm = "your_gmail_email"
password = "your_gmail_password"

subject = "subject test"
msg = "message test"


def email_send(user, pwd, recipient, subject, body):
    gmail_user = frm
    gmail_pwd = password
    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        print "trying to send email"
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print 'successfully sent the mail'
    except:
        print "failed to send mail"


def email_check():
    pop_conn = poplib.POP3_SSL('pop.gmail.com')
    pop_conn.user(frm)
    pop_conn.pass_(password)
    #  Get messages from server:
    messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    # Concat message pieces:
    messages = ["\n".join(mssg[1]) for mssg in messages]
    #  Parse message into an email object:
    messages = [parser.Parser().parsestr(mssg) for mssg in messages]
    for message in messages:
        if message['subject'] == 'subject test':
            sender = (message['From'])
            sendee = sender.split("<")[-1].split(">")[0]
            print sendee
            print subject
            payload = message.get_payload()
            print payload
            #  do_stuff based on the email subject here
        else:
            print message['subject'] + " failed to meet requirement"
    pop_conn.quit()

send_to = frm
email_send(frm, password, send_to, subject, msg)
email_check()

User avatar
DougieLawson
Posts: 34189
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Email Notification

Thu Dec 21, 2017 5:39 am

Why use email, it's ugly, it's insecure, parsing it is horrible, triggering on a message arriving is difficult? There are better ways to send small message payloads from one computer to another like MQTT.

https://www.hivemq.com/mqtt-essentials/
Note:The use of baseball bats for educational purposes is completely disallowed on this forum.

Any DMs sent on Twitter will be answered next month.

Return to “Python”