diff --git a/BackUp b/BackUp new file mode 100644 index 0000000..0a4d14c --- /dev/null +++ b/BackUp @@ -0,0 +1,72 @@ +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication +from email.mime.text import MIMEText +from email.encoders import encode_base64 +import os.path +import py7zr +import os +os.chdir(os.path.dirname(os.path.abspath(__file__))) + + +# Create a 7z archive named 'BlockBit.7z' and add the contents of 'source_dir' +with py7zr.SevenZipFile('BlockBit.7z', 'w') as archive: + archive.writeall('replace this with the file that everything is enclosed in', arcname='optional_name_in_archive') # change this line to your needs + + + +# Replace with the actual path to your .7z file +file_path = '' # replace with where your .7z file will be after creating it +def delete7z(): + try: + os.remove(file_path) + print(f"File '{file_path}' deleted successfully.") + except FileNotFoundError: + print(f"Error: File '{file_path}' not found.") + except PermissionError: + print(f"Error: No permission to delete file '{file_path}'.") + except Exception as e: + print(f"Error deleting file: {e}") + + +# Email settings +sender_email = 'example@gmail.com' #replace with your email +sender_password = '' # Use an app password, not your Gmail password +receiver_email = 'example@gmail.com' #replace with your email +subject = 'Email with 7z attachment' +body = 'Please find the attached 7z file.' +file_path = '' # Replace with where you want the .7z file to go while it is sending + + +msg = MIMEMultipart() +msg['From'] = sender_email +msg['To'] = receiver_email +msg['Subject'] = subject + +# Add the email body +msg.attach(MIMEText(body, 'plain')) + +# Attach the 7z file +with open(file_path, 'rb') as file: + part = MIMEApplication(file.read(), Name=os.path.basename(file_path)) + part.add_header('Content-Disposition', f'attachment; filename="{os.path.basename(file_path)}"') + msg.attach(part) + +# Encode the attachment +#encoders.encode_base64(part) + +# Send the email +try: + with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server: + server.login(sender_email, sender_password) + server.sendmail(sender_email, receiver_email, msg.as_string()) + print('Email sent successfully!') + delete7z() +except Exception as e: + print(f'Error sending email: {e}') + delete7z() + + + + +