-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_db.py
72 lines (63 loc) · 2.11 KB
/
backup_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import smtplib, ssl, email, os
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
import datetime
#Details
port = 587 # For starttls
smtp_server = "outlook.office365.com"
sender_email = "your email address"
receiver_email = "your email address"
password = input("Type your password and press enter:")
#password = "asd"
body = """This message is sent from Python."""
#Sends off the email
def sendMail(attach, tries=0, subject=None):
ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H:%M:%S')
#create attachment
success = False
print("Setting up ...")
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = str(subject) + " " + ts
message["Bcc"] = receiver_email
#add body to email
message.attach(MIMEText(body, "plain"))
filename = attach
#print("Open in binary ...")
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
#print("Encoding ...")
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
print("Adding attachment ...")
message.attach(part)
text = message.as_string()
print("Logging in ...")
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
print("Sending email ...")
try:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)
server.quit()
except Exception as e:
print("Error occured:")
print(e)
if tries <= 2:
sendMail(attach, tries+1)
finally:
print("Email sent.")
success = True
return success
#sendMail("trains.db")