-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.py
127 lines (111 loc) · 3.82 KB
/
mailer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from __future__ import print_function
__author__ = "Vishwajeet Narwal (Macbull)"
__email__ = "[email protected]"
from apiclient import discovery
from apiclient import errors
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from msg import msg,count,excel,subject,limit,salutation
from oauth2client import client
from oauth2client import tools
import base64
import httplib2
import oauth2client
import openpyxl
import os
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/gmail.send'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Macbull Mailer'
def get_credentials(count):
home_dir = os.getcwd()
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'gmail'+str(count)+'.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def CreateMessage(sender, to, subject, message_text):
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
def SendMessage(service, user_id, message):
try:
message = (service.users().messages().send(userId=user_id, body=message).execute())
print ('Message Id: %s' % message['id'])
return message
except errors.HttpError, error:
print ('An error occurred: %s' % error)
def DraftMessage(salutation,name,msg):
msg=salutation+" "+name+"\n"+msg
return msg
def readExcel(listpath):
email=[]
names=[]
wb = openpyxl.load_workbook(listpath)
sheetdata=wb.active
if sheetdata.max_column==1:
name=False
else:
name=True
for i in range(1,sheetdata.max_row+1,1):
if name:
names.append(sheetdata.cell(row=i, column=1).value)
email.append(sheetdata.cell(row=i, column=2).value)
else:
email.append(sheetdata.cell(row=i, column=1).value)
return name,names,email
def main():
i=0
credentials=[]
http=[]
service=[]
quantity={}
name,names,email=readExcel(excel)
while i < count:
credentials.append(get_credentials(i))
http.append(credentials[i].authorize(httplib2.Http()))
service.append(discovery.build('gmail', 'v1', http=http[i]))
quantity[i]=0
i=i+1
i=0
while i < len(email):
j=0
old_i=i
while j < count:
if quantity[j]<limit:
if name:
draft=DraftMessage(salutation,names[i],msg)
else:
draft=DraftMessage(salutation,"",msg)
message=CreateMessage("[email protected]",email[i],subject,draft)
mes=SendMessage(service[j],'me',message)
print('Sent to ',email[i])
quantity[j]+=1
i=i+1
j=j+1
if i==old_i:
print("Limit reached for all account")
print ("Last Email Sent to "+email[i-1])
break
if __name__ == '__main__':
main()