Пример #1
0
    def backUpData(self):
        fromaddr = '*****@*****.**'
        toaddr = '*****@*****.**'
        myPlace = ''
        try:
            with open('placename.txt','r') as myfile:
                myPlace = myfile.readlines()
        except:
            pass
        try:
            conn = mysql.connector.connect(host='localhost',user='******',password='',database='user_data')
            mycursor = conn.cursor()

            sql = 'SELECT * FROM user_info'
            mycursor.execute(sql)
            result = mycursor.fetchall()
            with open('userdata.csv','a',newline='') as csvfile:
                cwriter = csv.writer(csvfile)
                cwriter.writerows(result)
        except:
            pass
        msg = MIMEMultipart()

        msg['From'] = fromaddr
        msg['to'] = toaddr
        msg['Subject'] = "Todays Data Backup from"+str(myPlace)
        body = 'Todays Data Backup from '+ myPlace[0] + '\n Date: '+ str(date.today())

        msg.attach(MIMEText(body,'plain'))

        filename = 'userdata.csv'
        with open(filename,'r') as f: 
            attachment = MIMEText(f.read())

        attachment.add_header('Content-Disposition', 'attachment', filename=filename)
        msg.attach(attachment)
        try:
            server = smtplib.SMTP(host='smtp.gmail.com', port=587)
            server.starttls()
            server.login(fromaddr,'PUC@1234')
            text = msg.as_string()
            server.sendmail(fromaddr,toaddr,text)
            server.quit()
        except :
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Critical)
            msg.setText('Connection Error!')
            msg.setWindowTitle("Error")
            msg.setStandardButtons(QMessageBox.Ok)
            retval = msg.exec_()
Пример #2
0
    def backUpData(self):
        fromaddr = '*****@*****.**'
        toaddr = '*****@*****.**'
        myPlace = ''
        try:
            with open('placename.txt', 'r') as myfile:
                myPlace = myfile.readlines()
        except:
            pass

        try:
            with open('userdata.csv') as csvFile:
                result = csv.reader(csvFile)
        except:
            pass

        msg = MIMEMultipart()

        msg['From'] = fromaddr
        msg['to'] = toaddr
        msg['Subject'] = "Todays Data Backup from" + str(myPlace)
        body = 'Todays Data Backup from ' + myPlace[0] + '\n Date: ' + str(
            date.today())

        msg.attach(MIMEText(body, 'plain'))

        filename = 'userdata.csv'
        with open(filename, 'r') as f:
            attachment = MIMEText(f.read())

        attachment.add_header('Content-Disposition',
                              'attachment',
                              filename=filename)
        msg.attach(attachment)
        try:
            server = smtplib.SMTP(host='smtp.gmail.com', port=587)
            server.starttls()
            server.login(fromaddr, 'PUC@1234')
            text = msg.as_string()
            server.sendmail(fromaddr, toaddr, text)
            server.quit()
        except:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Critical)
            msg.setText('Connection Error!')
            msg.setWindowTitle("Error")
            msg.setStandardButtons(QMessageBox.Ok)
            retval = msg.exec_()
Пример #3
0
def email():


    try:
        fromaddr = "senders email address"
        password = "******"
        toaddr = "receiversemailaddress"

        # instance of MIMEMultipart
        msg = MIMEMultipart()

        # storing the senders email address
        msg['From'] = fromaddr

        # storing the receivers email address
        msg['To'] = toaddr

        # storing the subject
        msg['Subject'] = "Alert!!"

        # string to store the body of the mail
        body = "Limit Crossed"

        # attach the body with the msg instance
        msg.attach(MIMEText(body, 'plain'))

        directory = "Snaps/"
        # open the file to be sent
        for filename in os.listdir(directory):
            temp = directory+ filename
            attachment = open(temp, "rb")

            # instance of MIMEBase and named as p
            p = MIMEBase('application', 'octet-stream')

            # To change the payload into encoded form
            p.set_payload((attachment).read())

            # encode into base64
            encoders.encode_base64(p)

            p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

            # attach the instance 'p' to instance 'msg'
            msg.attach(p)

        # creates SMTP session
        s = smtplib.SMTP('smtp.gmail.com', 587)

        # start TLS for security
        s.starttls()

        # Authentication
        s.login(fromaddr, password)

        # Converts the Multipart msg into a string
        text = msg.as_string()

        # sending the mail
        s.sendmail(fromaddr, toaddr, text)

        # terminating the session
        s.quit()

        print("Email Sent")

        msg = QMessageBox()
        msg.setIcon(QMessageBox.Information)
        msg.setWindowTitle("Email Information")
        msg.setText("Email sent")
        msg.setStandardButtons(QMessageBox.Ok)
        # msg.buttonClicked.connect(sys.exit)
        retval = msg.exec_()

    except:
        print("Please check your Internet Connectivity")
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Critical)
        msg.setWindowTitle("ERROR")
        msg.setText("Please check your Internet Connectivity")
        msg.setStandardButtons(QMessageBox.Ok)
        # msg.buttonClicked.connect(sys.exit)
        retval = msg.exec_()