-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
61 lines (52 loc) · 1.82 KB
/
script.py
File metadata and controls
61 lines (52 loc) · 1.82 KB
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
import os
import gspread
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
x = datetime.now()
date = x.strftime('%d-%m-%Y')
jasonFilePath = " " #created from the cloud program using google drive api
sheetKey=" " # extracted from the url
path = '~/Users/Downloads/' #the path to witch the file will be saved (dont forget to add the '/' at the end of the path)
filename = f"{date}.xlsx"
filepath = path+filename
email_from = "yourmail@gmail.com"
email_target = "targetmail@example.com"
pswd = " " # get this from the google account password
####Create the File ####
gc = gspread.service_account(filename=jasonFilePath)
sh = gc.open_by_key(sheetKey)
export_file = sh.export(format='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
with open(filepath, 'wb') as f:
f.write(export_file)
print(f"The file:{filename} has created at {path}")
####Sending the File via Gmail####
subject = "subject"
body = " "
msg = MIMEMultipart()
msg['From'] = email_from
msg['To'] = email_target
msg['Subject'] = subject
msg.attach(MIMEText(body))
attachment= open(filepath, 'rb')
# Encode as base 64
attachment_package = MIMEBase('application', 'octet-stream')
attachment_package.set_payload((attachment).read())
encoders.encode_base64(attachment_package)
attachment_package.add_header('Content-Disposition', f"attachment; filename= " + filename)
msg.attach(attachment_package)
text = msg.as_string()
print("Connectig to servers....")
svr= smtplib.SMTP("smtp.gmail.com", 587)
svr.starttls()
svr.login(email_from, pswd)
svr.sendmail(email_from, email_target, text)
print(f"The email sent to {email_target}")
svr.quit()
#####Deleteing the file######
print("Deleting the file")
os.remove(filepath)
print("File deleted")