-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendGmailAttachment.py
executable file
·87 lines (73 loc) · 2.38 KB
/
sendGmailAttachment.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 6 11:18:13 2014
@author: emilio based on Rodrigo Coutinho
http://kutuma.blogspot.com.es/2007/08/sending-emails-via-gmail-with-python.html
"""
import os
import sys
import json
import shutil
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
# Load data from json file
jsonFile=open('config.json')
jsonData = json.load(jsonFile)
jsonFile.close()
user = jsonData['user']
pwd = jsonData['pwd']
tmpFolder = sys.path[0] +'/tmp/'
analysedFolder = sys.path[0] + '/analysed/'
attFolder = sys.path[0] + '/attachments/'
sentFolder = sys.path[0] + '/sent/'
def gmailWithAttachment(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = user
msg['To'] = to
# msg['Subject'] = "Your"
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(user, pwd)
mailServer.sendmail(user, to, msg.as_string())
mailServer.close()
# The list of analysed files we have to send attached
analysedList = []
analysedSgfFiles = os.listdir(analysedFolder)
for sgfFile in analysedSgfFiles:
analysedList.append(sgfFile)
print 'analysedList'
print analysedList
# The list of emails-sgfFiles
dataList = []
dataFile = open(sys.path[0] + '/data_file.txt','r')
for line in dataFile:
dataList.append(line)
print 'dataList'
print dataList
for analysedFile in analysedSgfFiles:
for data in dataList:
if data.split(' ')[0].split('.')[0] in analysedFile:
print 'Coincidence!'
print data.split(' ')[0]
print analysedFile
print data.split(' ')[1]
gmailWithAttachment(data.split(' ')[1],
"Your analysed sgf",
"This is the result of the analysis of your game by gnugo",
"analysed/" + analysedFile)
# And now we move the just sent file to the sent folder
shutil.move(analysedFolder + analysedFile, sentFolder + analysedFile)