Raspberry.

Write python program for sending Email when control is switched on in Raspberry.
mail@pastecode.io avatar
unknown
python
a month ago
1.1 kB
5
Indexable
Never
import smtplib as sl
from email.mime.multipart import MIMEMultipart as mlp 
from email.mime.text import MIMEText as txt
import ssl as s
import Rpi.GPIO as gp 

gp.setmode(gp.BOARD)
gp.setup(17,gp.IN)
gp.setwarnings(False)

sender_mail_id = ''
sender_mail_appkey = ''
receiver_mail_id = ''

def send_mail():
    msg = mlp.MIMEMultipart()
    
    msg['From'] = sender_mail_id
    msg['To'] = receiver_mail_id
    msg['Subject'] = input("Enter Mail Subject: ")
    
    body = input("Enter Mail Body: ")
    context = s.create_default_context()
    msg.attach(txt(body,'plain'))
    
    with sl.SMPT_SSL('smtp.gmail.com', 456, context = context) as server:
        server.login(sender_mail_id,sender_mail_appkey)
        server.sendmail(sender_mail_appkey,receiver_mail_id,msgg.as_string)
        print('Sent Mail Successfully')
        
try:
    while True:
        if gp.input(17) == gp.HIGH:
            print("Control Switched On!")
            send_mail()
            break

except KeyboardInterrupt:
    print("\nProgram terminated by user.")
finally:
    gp.cleanup()