| 14836 |
kshitij.so |
1 |
'''Created on Mar 10, 2015
|
|
|
2 |
|
|
|
3 |
@author: amit
|
|
|
4 |
'''
|
|
|
5 |
from datetime import date
|
|
|
6 |
from email import encoders
|
|
|
7 |
from email.mime.base import MIMEBase
|
|
|
8 |
from email.mime.multipart import MIMEMultipart
|
|
|
9 |
from email.mime.text import MIMEText
|
|
|
10 |
import MySQLdb
|
|
|
11 |
import datetime
|
|
|
12 |
import smtplib
|
|
|
13 |
import xlwt
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
DB_HOST = "localhost"
|
|
|
18 |
DB_USER = "root"
|
|
|
19 |
DB_PASSWORD = "shop2020"
|
|
|
20 |
DB_NAME = "dtr"
|
|
|
21 |
TMP_FILE = "User_Invite_Report.xls"
|
|
|
22 |
|
| 20046 |
rajender |
23 |
LIST_EMAILS = ['rajender.singh@shop2020.in','shailesh.kumar@shop2020.in,amit.sirohi@shop2020.in,ritesh.chauhan@shop2020.in']
|
| 14836 |
kshitij.so |
24 |
# KEY NAMES
|
|
|
25 |
SENDER = "cnc.center@shop2020.in"
|
|
|
26 |
PASSWORD = "5h0p2o2o"
|
|
|
27 |
SUBJECT = "User Invite Activation Report for " + date.today().isoformat()
|
|
|
28 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
29 |
SMTP_PORT = 587
|
|
|
30 |
|
|
|
31 |
USER_INVITE_QUERY="""
|
|
|
32 |
SELECT id,email,mobile_number,activated,created FROM `users` WHERE activated!=1
|
|
|
33 |
"""
|
|
|
34 |
|
|
|
35 |
date_format = xlwt.XFStyle()
|
|
|
36 |
date_format.num_format_str = 'yyyy/mm/dd'
|
|
|
37 |
|
|
|
38 |
datetime_format = xlwt.XFStyle()
|
|
|
39 |
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
|
|
|
40 |
|
|
|
41 |
default_format = xlwt.XFStyle()
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
def getDbConnection():
|
|
|
45 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
def generateInviteReport():
|
|
|
49 |
selectSql = USER_INVITE_QUERY
|
|
|
50 |
conn = getDbConnection()
|
|
|
51 |
cursor = conn.cursor()
|
|
|
52 |
cursor.execute(selectSql)
|
|
|
53 |
result = cursor.fetchall()
|
|
|
54 |
workbook = xlwt.Workbook()
|
|
|
55 |
worksheet = workbook.add_sheet("User")
|
|
|
56 |
boldStyle = xlwt.XFStyle()
|
|
|
57 |
f = xlwt.Font()
|
|
|
58 |
f.bold = True
|
|
|
59 |
boldStyle.font = f
|
|
|
60 |
column = 0
|
|
|
61 |
row = 0
|
|
|
62 |
worksheet.write(row, 0, 'ID', boldStyle)
|
|
|
63 |
worksheet.write(row, 1, 'Email', boldStyle)
|
|
|
64 |
worksheet.write(row, 2, 'Mobile Number', boldStyle)
|
|
|
65 |
worksheet.write(row, 3, 'Activated', boldStyle)
|
|
|
66 |
worksheet.write(row, 4, 'Created', boldStyle)
|
|
|
67 |
|
|
|
68 |
for r in result:
|
|
|
69 |
row += 1
|
|
|
70 |
column = 0
|
|
|
71 |
for data in r :
|
|
|
72 |
worksheet.write(row, column, int(data) if type(data) is float else data, datetime_format if type(data) is datetime.datetime else default_format)
|
|
|
73 |
column += 1
|
|
|
74 |
workbook.save(TMP_FILE)
|
| 20172 |
aman.kumar |
75 |
sendmail(['rajender.singh@shop2020.in','rajneesh.arora@saholic.com', 'khushal.bhatia@saholic.com', 'shailesh.kumar@shop2020.in,amit.sirohi@shop2020.in,ritesh.chauhan@shop2020.in'], "", TMP_FILE, SUBJECT)
|
| 14836 |
kshitij.so |
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
def sendmail(email, message, fileName, title):
|
|
|
80 |
if email == "":
|
|
|
81 |
return
|
|
|
82 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
83 |
mailServer.ehlo()
|
|
|
84 |
mailServer.starttls()
|
|
|
85 |
mailServer.ehlo()
|
|
|
86 |
|
|
|
87 |
# Create the container (outer) email message.
|
|
|
88 |
msg = MIMEMultipart()
|
|
|
89 |
msg['Subject'] = title
|
|
|
90 |
msg.preamble = title
|
|
|
91 |
html_msg = MIMEText(message, 'html')
|
|
|
92 |
msg.attach(html_msg)
|
|
|
93 |
|
|
|
94 |
fileMsg = MIMEBase('application', 'vnd.ms-excel')
|
|
|
95 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
96 |
encoders.encode_base64(fileMsg)
|
|
|
97 |
fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
|
|
98 |
msg.attach(fileMsg)
|
| 20172 |
aman.kumar |
99 |
MAILTO=['rajender.singh@shop2020.in','rajneesh.arora@saholic.com', 'khushal.bhatia@saholic.com', 'shailesh.kumar@shop2020.in,amit.sirohi@shop2020.in,ritesh.chauhan@shop2020.in']
|
| 14836 |
kshitij.so |
100 |
mailServer.login(SENDER, PASSWORD)
|
|
|
101 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
102 |
|
|
|
103 |
def main():
|
|
|
104 |
generateInviteReport()
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
if __name__ == '__main__':
|
|
|
108 |
main()
|
|
|
109 |
|