Advertisement
Guest User

Untitled

a guest
Dec 5th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 199.96 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import logging
  5. from tools import repnuplogger
  6. import time
  7. import json
  8. import smtplib
  9. from email.MIMEMultipart import MIMEMultipart
  10. from email.MIMEText import MIMEText
  11. from email.MIMEImage import MIMEImage
  12.  
  13. import hashlib
  14.  
  15.  
  16. def date_handler(obj):
  17. return obj.isoformat() if hasattr(obj, 'isoformat') else obj
  18.  
  19.  
  20. # generate summary report for the user
  21. def generate_summary_report(user_id, db_conn, config, type, sn, g_admin_emails):
  22. logger = logging.getLogger("repnuplogger")
  23. fbuser = db_conn.selectOne('tbl_fb_users', "user_id='" + str(user_id) + "'", 'fbid', 'fbname', 'textStatus', 'imageStatus', 'emailId')
  24. user = db_conn.selectOne('tbl_users', "id='" + str(user_id) + "'", 'created', 'approvedSummaryDate', 'email', 'uuid')
  25. user_setting = db_conn.selectOne('tbl_settings',"user_id='"+str(user_id)+"'",'progress_report')
  26. mailFlag = True
  27. notifyAdminFlag = True
  28.  
  29. userTotalAlert = 0
  30. if user_setting:
  31. if user_setting[0] == 0:
  32. mailFlag = False
  33.  
  34. twuser = db_conn.selectOne('tbl_twitter_users', "user_id='" + str(user_id) + "'")
  35. inuser = db_conn.selectOne('tbl_instagram_users', "user_id='" + str(user_id) + "'")
  36. logger.info("Generating summary report for user %s" % str(user_id))
  37. report = {}
  38. report['user_id'] = str(user_id)
  39. if type:
  40. report['reportType'] = str(type)
  41. else:
  42. report['reportType'] = 0
  43.  
  44. if fbuser:
  45. allAlertsRecord = db_conn.select_as_dict('tbl_fb_text_alerts',where="userFbId='"+str(fbuser[0])+"' and innocent=0 and (violence > 1 or inappropriate > 1 OR indecency > 1 OR alcohol > 1 OR other > 1 OR questionable > 1)")
  46. tmpAlert = db_conn.selectCount("select count(algoQueryId) as t from tbl_fb_text_alerts where userFbId='"+str(fbuser[0])+"'")
  47. if tmpAlert:
  48. tmpalert = str(tmpAlert[0])
  49. userTotalAlert = userTotalAlert + int(tmpalert)
  50. else:
  51. allAlertsRecord = []
  52. badPost = 0
  53. badComments = 0
  54. badShare = 0
  55. badTweetPhoto = 0
  56. badTweet = 0
  57. badInstaCaption = 0
  58. badInstaComment = 0
  59. oldCount = {}
  60. newCount = {}
  61. totalCounts = {}
  62. lastFeedTime = ''
  63. checkList = ['comments', 'commentsPhoto', 'bad_images', 'bad_likes']
  64. index = 0
  65. allAlerts = {}
  66. totalAlert = 0
  67.  
  68. for rec in allAlertsRecord:
  69. if rec['type'] not in checkList and rec['statusType'] !='shared_story':
  70. badPost = badPost + 1
  71. elif rec['type'] == 'comments' or rec['type'] == 'commentsPhoto':
  72. badComments = badComments + 1
  73. elif rec['type'] == 'shared_story':
  74. badShare = badShare + 1
  75.  
  76. # create confirmed Alert category based on bit field value > 1
  77. confirmedAlertCat = []
  78. if str(rec['violence']) > '1':
  79. confirmedAlertCat.append('violence')
  80. if str(rec['inappropriate']) > '1':
  81. confirmedAlertCat.append('inappropriate')
  82. if str(rec['indecency']) > '1':
  83. confirmedAlertCat.append('indecency')
  84. if str(rec['alcohol']) > '1':
  85. confirmedAlertCat.append('alcohol')
  86. if str(rec['other']) > '1':
  87. confirmedAlertCat.append('other')
  88. if str(rec['questionable']) > '1':
  89. confirmedAlertCat.append('questionable')
  90.  
  91. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  92.  
  93. allAlerts[index] = {}
  94. allAlerts[index]['AllAlert'] = {}
  95. allAlerts[index]['AllAlert']['algoQueryid'] = rec['algoQueryId']
  96. allAlerts[index]['AllAlert']['feedId'] = rec['feedId']
  97. allAlerts[index]['AllAlert']['userFbId'] = rec['userFbId']
  98. allAlerts[index]['AllAlert']['wallFbId'] = rec['wallFbId']
  99. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  100. allAlerts[index]['AllAlert']['fromFbId'] = rec['fromFbId']
  101. allAlerts[index]['AllAlert']['type'] = rec['type']
  102. allAlerts[index]['AllAlert']['statusType'] = rec['statusType']
  103. allAlerts[index]['AllAlert']['message'] = rec['message']
  104. allAlerts[index]['AllAlert']['messageTag'] = rec['messageTag']
  105. allAlerts[index]['AllAlert']['alertWord'] = rec['alertWord']
  106. allAlerts[index]['AllAlert']['alertMatchWord'] = rec['alertMatchWord']
  107. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  108. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  109. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  110. allAlerts[index]['AllAlert']['language'] = rec['language']
  111. allAlerts[index]['AllAlert']['manual'] = rec['manual']
  112. allAlerts[index]['AllAlert']['privacy'] = rec['privacy']
  113. allAlerts[index]['AllAlert']['sn_type'] = 'fb'
  114. if lastFeedTime > str(rec['feedTime']) or lastFeedTime == '':
  115. lastFeedTime = str(rec['feedTime'])
  116. index = index+1
  117.  
  118. if twuser:
  119.  
  120. allAlertsRecord = db_conn.select_as_dict('tbl_tw_text_alerts', where="userTwId='" + str(twuser[2]) + "' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR other >1)")
  121. tmpTwAlert = db_conn.selectCount("select count(algoQueryId) as t from tbl_tw_text_alerts where userTwId='"+str(twuser[2])+"'")
  122.  
  123. if tmpTwAlert:
  124. tmpalert = str(tmpTwAlert[0])
  125. userTotalAlert = userTotalAlert + int(tmpalert)
  126.  
  127. for rec in allAlertsRecord:
  128. if 'photo' in rec['type']:
  129. badTweetPhoto += 1
  130. else:
  131. badTweet += 1
  132.  
  133. # create confirmed Alert category based on bit field value > 1
  134. confirmedAlertCat = []
  135. if str(rec['violence']) > '1':
  136. confirmedAlertCat.append('violence')
  137. if str(rec['inappropriate']) > '1':
  138. confirmedAlertCat.append('inappropriate')
  139. if str(rec['indecency']) > '1':
  140. confirmedAlertCat.append('indecency')
  141. if str(rec['alcohol']) > '1':
  142. confirmedAlertCat.append('alcohol')
  143. if str(rec['other']) > '1':
  144. confirmedAlertCat.append('other')
  145. if str(rec['questionable']) > '1':
  146. confirmedAlertCat.append('questionable')
  147.  
  148. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  149.  
  150. allAlerts[index] = {}
  151. allAlerts[index]['AllAlert'] = {}
  152. allAlerts[index]['AllAlert']['algoQueryid'] = rec['algoQueryId']
  153. allAlerts[index]['AllAlert']['tweetId'] = rec['tweetId']
  154. allAlerts[index]['AllAlert']['userTwId'] = rec['userTwId']
  155. allAlerts[index]['AllAlert']['fromTwId'] = rec['fromTwId']
  156. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  157. allAlerts[index]['AllAlert']['fromScreenName'] = rec['fromScreenName']
  158. allAlerts[index]['AllAlert']['type'] = rec['type']
  159. allAlerts[index]['AllAlert']['message'] = rec['message']
  160. allAlerts[index]['AllAlert']['alertWord'] = rec['alertWord']
  161. allAlerts[index]['AllAlert']['alertMatchWord'] = rec['alertMatchWord']
  162. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  163. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  164. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  165. allAlerts[index]['AllAlert']['language'] = rec['language']
  166. allAlerts[index]['AllAlert']['privacy'] = rec['privacy']
  167. allAlerts[index]['AllAlert']['possibleSensitive'] = rec['possibleSensitive']
  168. allAlerts[index]['AllAlert']['sn_type'] = 'tw'
  169. if lastFeedTime > str(rec['feedTime']) or lastFeedTime == '':
  170. lastFeedTime = str(rec['feedTime'])
  171. index += 1
  172. if inuser:
  173. allAlertsRecord = db_conn.select_as_dict('tbl_insta_text_alerts',where="userInsId='"+str(inuser[2])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR other >1)")
  174. tmpInAlert = db_conn.selectCount("select count(algoQueryId) from tbl_insta_text_alerts where userInsId='"+str(inuser[2])+"'")
  175. if tmpInAlert:
  176. tmpalert = str(tmpInAlert[0])
  177. userTotalAlert = userTotalAlert + int(tmpalert)
  178.  
  179. for rec in allAlertsRecord:
  180. if rec['type'] == 'caption':
  181. badInstaCaption = badInstaCaption + 1
  182. else:
  183. badInstaComment = badInstaComment + 1
  184.  
  185. # create confirmed Alert category based on bit field value > 1
  186. confirmedAlertCat = []
  187. if str(rec['violence']) > '1':
  188. confirmedAlertCat.append('violence')
  189. if str(rec['inappropriate']) > '1':
  190. confirmedAlertCat.append('inappropriate')
  191. if str(rec['indecency']) > '1':
  192. confirmedAlertCat.append('indecency')
  193. if str(rec['alcohol']) > '1':
  194. confirmedAlertCat.append('alcohol')
  195. if str(rec['other']) > '1':
  196. confirmedAlertCat.append('other')
  197. if str(rec['questionable']) > '1':
  198. confirmedAlertCat.append('questionable')
  199.  
  200. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  201.  
  202. allAlerts[index] = {}
  203. allAlerts[index]['AllAlert'] = {}
  204. allAlerts[index]['AllAlert']['algoQueryid'] = rec['algoQueryId']
  205. allAlerts[index]['AllAlert']['mediaId'] = rec['mediaId']
  206. allAlerts[index]['AllAlert']['feedId'] = rec['feedId']
  207. allAlerts[index]['AllAlert']['userInsId'] = rec['userInsId']
  208. allAlerts[index]['AllAlert']['fromInsId'] = rec['fromInsId']
  209. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  210. allAlerts[index]['AllAlert']['fromScreenName'] = rec['fromScreenName']
  211. allAlerts[index]['AllAlert']['type'] = rec['type']
  212. allAlerts[index]['AllAlert']['message'] = rec['message']
  213. allAlerts[index]['AllAlert']['alertWord'] = rec['alertWord']
  214. allAlerts[index]['AllAlert']['alertMatchWord'] = rec['alertMatchWord']
  215. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  216. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  217. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  218. allAlerts[index]['AllAlert']['language'] = rec['language']
  219. allAlerts[index]['AllAlert']['link'] = rec['link']
  220. allAlerts[index]['AllAlert']['sn_type'] = 'in'
  221.  
  222. index = index+1
  223.  
  224. if (len(allAlertsRecord) == userTotalAlert) and report['reportType'] == "2":
  225. # for initial report check if total alert and confirmed alerts are equal then generate detail report
  226. report['reportType'] = "0"
  227. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  228. #db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",approvedSummaryDate=str(rectime),aprovalStatus="1")
  229. query = "update tbl_users set approvedSummaryDate='"+str(rectime)+"',aprovalStatus=0 where id ='"+str(report['user_id'])+"'"
  230. logger.info(query)
  231. db_conn.sqlQuery(query)
  232. notifyAdminFlag = False
  233.  
  234. if len(allAlertsRecord) == 0 and report['reportType'] != "2" and report['reportType'] != "3" and report['reportType'] != "4":
  235. report['noAlert'] = 1
  236.  
  237. oldCount['badPosts'] = badPost
  238. oldCount['badComments'] = badComments
  239. oldCount['badShares'] = badShare
  240. oldCount['badTweetPhoto'] = badTweetPhoto
  241. oldCount['badTweet'] = badTweet
  242. oldCount['badInstaComment'] = badInstaComment
  243. oldCount['badInstaCaption'] = badInstaCaption
  244.  
  245. totalFeedCount = 0
  246.  
  247. totalAlert = badPost + badComments + badShare
  248. totalTweetAlert = badTweet + badTweetPhoto
  249. totalInAlert = badInstaCaption + badInstaComment
  250. totalImageCount = 0
  251. totalTweetCount = 0
  252. totalTweetMediaCount = 0
  253. totalInstaCount = 0
  254. totalInstaMediaCount = 0
  255.  
  256. if fbuser:
  257. fbcounts = db_conn.selectOne('tbl_fb_user_counts',"fbid='"+str(fbuser[0])+"'")
  258. if fbcounts:
  259. textcount = json.loads(fbcounts[1])
  260. imagecount = json.loads(fbcounts[2])
  261. totalCounts['feeds'] = {}
  262. if 'commentCount' not in textcount:
  263. textcount['commentCount'] = 0
  264. if 'postCount' not in textcount:
  265. textcount['postCount'] = 0
  266. if 'sharedCount' not in textcount:
  267. textcount['sharedCount'] = 0
  268.  
  269. totalCounts['feeds']['all'] = textcount['commentCount'] + textcount['postCount'] + textcount['sharedCount']
  270. totalFeedCount = totalCounts['feeds']['all']
  271. totalCounts['feeds']['posts'] = textcount['postCount']
  272. totalCounts['feeds']['comments'] = textcount['commentCount']
  273. totalCounts['feeds']['shares'] = textcount['sharedCount']
  274. totalCounts['images'] = {}
  275. totalCounts['images']['tagged'] = imagecount['photoTagged']
  276. totalCounts['images']['upload'] = imagecount['photoUploadedCount']
  277. totalCounts['images']['like'] = imagecount['photoLikedCount']
  278. totalCounts['feeds']['images'] = imagecount['photoTagged'] + imagecount['photoUploadedCount']
  279. totalCounts['feeds']['likes'] = imagecount['photoLikedCount']
  280. totalCounts['alerts'] = len(allAlertsRecord)
  281. totalImageCount = imagecount['photoTagged'] + imagecount['photoUploadedCount'] + imagecount['photoLikedCount']
  282. if twuser:
  283.  
  284. twcounts = db_conn.selectOne('tbl_tw_user_counts',"twid='"+str(twuser[2])+"'")
  285. if twcounts:
  286. tweetcount = json.loads(twcounts[1])
  287. photocount = json.loads(twcounts[2])
  288. totalCounts['tweets'] = {}
  289. totalCounts['tweets']['all'] = tweetcount['tweet'] + tweetcount['retweet']
  290. totalTweetCount = totalCounts['tweets']['all']
  291. totalCounts['tweets']['tweet'] = tweetcount['tweet']
  292. totalCounts['tweets']['retweet'] = tweetcount['retweet']
  293. totalTweetCount = totalCounts['tweets']['all']
  294.  
  295. totalCounts['tweetimages'] = {}
  296. totalCounts['tweetimages']['all'] = photocount['tweet'] + photocount['retweet']
  297. totalCounts['tweetimages']['tweet'] = photocount['tweet']
  298. totalCounts['tweetimages']['retweet'] = photocount['retweet']
  299. totalTweetMediaCount = photocount['tweet'] + photocount['retweet']
  300.  
  301. if inuser:
  302.  
  303. incounts = db_conn.selectOne('tbl_insta_user_counts',"instagramId='"+str(inuser[2])+"'")
  304. if incounts:
  305. intexcount = json.loads(incounts[2])
  306. inimagecount = incounts[1]
  307. totalCounts['instagram'] = {}
  308. totalCounts['instagram']['all'] = intexcount['commentCount'] + intexcount['captionCount']+inimagecount
  309. totalCounts['instagram']['comment'] = intexcount['commentCount']
  310. totalCounts['instagram']['caption'] = intexcount['captionCount']
  311. totalCounts['instagram']['media'] = inimagecount
  312. totalInstaCount = intexcount['commentCount'] + intexcount['captionCount']
  313. totalInstaMediaCount = inimagecount
  314.  
  315. report['oldCount'] = json.dumps(json.dumps(oldCount) )
  316. #new count and old count are equal for now, a new future feature will update the new count
  317. report['newCount'] = json.dumps(json.dumps(oldCount))
  318. report['netCount'] = json.dumps(json.dumps(totalCounts) )
  319.  
  320. report['joinningDate'] = user[0]
  321. report['approvalDate'] = user[1]
  322. report['timeBack'] = lastFeedTime
  323. report['report'] = json.dumps(allAlerts, default=date_handler)
  324. report['id'] = hashlib.md5(str(time.time())+str(fbuser[0])).hexdigest()
  325. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  326. if report['approvalDate'] is None:
  327. report['approvalDate'] = '0000-00-00'
  328.  
  329. try:
  330.  
  331. if report['reportType'] == '4':
  332. db_conn.updateRec('tbl_reports', "user_id='"+str(report['user_id'])+"'",
  333. report=report['report'],
  334. created=rectime,
  335. modified=rectime)
  336. db_conn.updateRec('tbl_users', "id='"+str(report['user_id'])+"'",
  337. is_new=1, regenerate_report=0, last_email_sent=rectime)
  338. return
  339. if report['reportType'] == "3":
  340. db_conn.updateRec('tbl_reports', "user_id='"+str(report['user_id'])+"'",
  341. report=report['report'],
  342. created=rectime,
  343. modified=rectime)
  344. if sendDailyReportMail(report['id'], report['user_id'], fbuser[0], fbuser[1], user[2], config, mailFlag, user[3]):
  345. db_conn.updateRec('tbl_users', "id='"+str(report['user_id'])+"'",
  346. is_new=1, mail_status="daily_report_mail", regenerate_report=0, modified=rectime, last_email_sent=rectime)
  347. return
  348. db_conn.sqlQuery("INSERT into tbl_reports (id,user_id,report,newCounts,oldCounts,netCounts,approvalDate,timeBack,reportType,created,modified) values ('"+str(report['id'])+"',"+report['user_id']+","+json.dumps(report['report'])+","+report['newCount']+","+report['oldCount']+","+report['netCount']+",'"+str(report['approvalDate'])+"','"+str(report['timeBack'])+"',"+str(report['reportType'])+",'"+(rectime)+"','"+str(rectime)+"')")
  349. if sn == 'fb':
  350. db_conn.updateRec('tbl_users', "id='"+str(report['user_id'])+"'", status=2)
  351. if 'noAlert' in report:
  352. if(sendNoAlertMail(report['id'],report['user_id'],fbuser[0],fbuser[1],user[2],totalFeedCount,totalImageCount,totalCounts['feeds']['posts'],totalCounts['feeds']['comments'],totalCounts['feeds']['shares'],totalCounts['feeds']['likes'],config,mailFlag,user[3])):
  353. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  354. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=2,modified=rectime)
  355. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="summary_mail",status=6,modified=rectime,last_email_sent=rectime)
  356. else:
  357. flag = False
  358.  
  359. if report['reportType'] == "2":
  360. if(sendInitialMail(report['id'],report['user_id'],fbuser[0],fbuser[1],user[2],totalFeedCount,totalAlert,totalImageCount,config,mailFlag,user[3])):
  361. flag = True
  362. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="initial_report_mail", status=2, modified=rectime, last_email_sent=rectime)
  363. else:
  364. if(sendSummaryMail(report['id'],report['user_id'],fbuser[0],fbuser[1],user[2],totalFeedCount,totalAlert,totalImageCount,config,mailFlag,user[3])):
  365. flag = True
  366. db_conn.updateRec('tbl_users', "id='"+str(report['user_id'])+"'", mail_status="summary_mail", status=6, modified=rectime, last_email_sent=rectime)
  367.  
  368. if flag:
  369. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  370. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=1,modified=rectime)
  371.  
  372. elif sn == 'tw':
  373. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",twitterStatus=2)
  374.  
  375. if 'noAlert' in report:
  376. totalFeedCount = totalTweetMediaCount + totalTweetCount
  377. if(sendNoTwAlertMail(report['id'],report['user_id'],twuser[2],twuser[3],user[2],totalFeedCount,totalTweetCount,totalTweetMediaCount,config,mailFlag,user[3])):
  378. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  379. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=2,modified=rectime)
  380. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="summary_mail",twitterStatus=6,modified=rectime, last_email_sent=rectime)
  381. else:
  382. flag = False
  383.  
  384. if report['reportType'] == "2":
  385. #logger.info("Sending email ")
  386. #if(sendInitialTwMail(report['id'],report['user_id'],twuser[2],twuser[3],user[2],totalTweetCount,totalTweetAlert,config,mailFlag,user[3])):
  387. # flag = True
  388. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="initial_report_mail",twitterStatus=2,modified=rectime, last_email_sent=rectime)
  389. else:
  390. if(sendTwSummaryMail(report['id'],report['user_id'],twuser[2],twuser[3],user[2],totalTweetCount,totalTweetAlert,config,mailFlag,user[3])):
  391. flag = True
  392. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="summary_mail",twitterStatus=6,modified=rectime, last_email_sent=rectime)
  393.  
  394. if flag:
  395. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  396. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=1,modified=rectime)
  397. elif sn == 'in':
  398. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",instagramStatus=2)
  399.  
  400. if 'noAlert' in report:
  401.  
  402. if(sendNoInAlertMail(report['id'],report['user_id'],inuser[2],inuser[4],user[2],totalInstaCount,totalInstaMediaCount,config,mailFlag,user[3])):
  403. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  404. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=2,modified=rectime)
  405. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="summary_mail",instagramStatus=6,modified=rectime, last_email_sent=rectime)
  406. else:
  407. flag = False
  408.  
  409. if report['reportType'] == "2":
  410. #logger.info("Sending email ")
  411. #if(sendInitialInMail(report['id'],report['user_id'],inuser[4],user[2],config,mailFlag,user[3])):
  412. flag = True
  413. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="initial_report_mail",instagramStatus=2,modified=rectime, last_email_sent=rectime)
  414. else:
  415. if(sendInSummaryMail(report['id'],report['user_id'],inuser[4],user[2],totalInstaMediaCount,totalInAlert,config,mailFlag,user[3])):
  416. flag = True
  417. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="summary_mail",instagramStatus=6,modified=rectime, last_email_sent=rectime)
  418.  
  419. if flag:
  420. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  421. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=1,modified=rectime)
  422. if report['reportType'] == "2" and notifyAdminFlag:
  423. notify_admin(1,user_id,db_conn,g_admin_emails,config)
  424.  
  425. except Exception as e:
  426. logger.error("Unable to create summary report for user id %s with error %s"%(user_id,str(e)))
  427.  
  428.  
  429. def generate_detailed_report(user_id, db_conn, config, sn, mtype='1'):
  430. """
  431. mtype = 1 generate report and send detailed mail
  432. mtype = 2 regenerate report witout mail
  433. mtype = 3 regenerate report and send daily mail
  434. """
  435. logger = logging.getLogger("repnuplogger")
  436. fbuser = db_conn.selectOne('tbl_fb_users',"user_id='"+str(user_id)+"'",'fbid','fbname','textStatus','imageStatus','emailId')
  437. user = db_conn.selectOne('tbl_users',"id='"+str(user_id)+"'",'created','approvedSummaryDate','email','uuid')
  438. user_setting = db_conn.selectOne('tbl_settings',"user_id='"+str(user_id)+"'",'progress_report')
  439. mailFlag = True
  440. if user_setting:
  441. if user_setting[0] == 0:
  442. mailFlag = False
  443. twuser = db_conn.selectOne('tbl_twitter_users',"user_id='"+str(user_id)+"'")
  444. inuser = db_conn.selectOne('tbl_instagram_users',"user_id='"+str(user_id)+"'")
  445.  
  446. logger.info("Generating detail report for user %s"%str(user_id))
  447. report = {}
  448. report['user_id'] = str(user_id)
  449. report['reportType'] = str(mtype)
  450. badPost = 0
  451. badComments = 0
  452. badShare = 0
  453. badImages = 0
  454.  
  455. oldCount = {}
  456. newCount = {}
  457. totalCounts = {}
  458. lastFeedTime = ''
  459. checkList = ['comments','commentsPhoto','bad_images','bad_likes']
  460. index = 0
  461. allAlerts = {}
  462. totalAlert = 0
  463. badTweet = 0
  464. badTweetPhoto = 0
  465. badInstaCaption = 0
  466. badInstaComment = 0
  467. badInstaMedia = 0
  468.  
  469. if fbuser:
  470.  
  471. allAlertsRecord = db_conn.select_as_dict('tbl_fb_text_alerts', where="userFbId='"+str(fbuser[0])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR other >1) ")
  472.  
  473. for rec in allAlertsRecord:
  474. if rec['type'] not in checkList and rec['statusType'] != 'shared_story':
  475. badPost = badPost + 1
  476. elif rec['type'] == 'comments' or rec['type'] == 'commentsPhoto':
  477. badComments = badComments + 1
  478. elif rec['type'] == 'shared_story':
  479. badShare = badShare + 1
  480. # create confirmed Alert category based on bit field value > 1
  481. confirmedAlertCat = []
  482. if str(rec['violence']) > '1':
  483. confirmedAlertCat.append('violence')
  484. if str(rec['inappropriate']) > '1':
  485. confirmedAlertCat.append('inappropriate')
  486. if str(rec['indecency']) > '1':
  487. confirmedAlertCat.append('indecency')
  488. if str(rec['alcohol']) > '1':
  489. confirmedAlertCat.append('alcohol')
  490. if str(rec['other']) > '1':
  491. confirmedAlertCat.append('other')
  492. if str(rec['questionable']) > '1':
  493. confirmedAlertCat.append('questionable')
  494.  
  495. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  496. allAlerts[index] = {}
  497. allAlerts[index]['AllAlert'] = {}
  498. allAlerts[index]['AllAlert']['algoQueryid'] = rec['algoQueryId']
  499. allAlerts[index]['AllAlert']['feedId'] = rec['feedId']
  500. allAlerts[index]['AllAlert']['userFbId'] = rec['userFbId']
  501. allAlerts[index]['AllAlert']['wallFbId'] = rec['wallFbId']
  502. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  503. allAlerts[index]['AllAlert']['fromFbId'] = rec['fromFbId']
  504. allAlerts[index]['AllAlert']['type'] = rec['type']
  505. allAlerts[index]['AllAlert']['statusType'] = rec['statusType']
  506. allAlerts[index]['AllAlert']['message'] = rec['message']
  507. allAlerts[index]['AllAlert']['messageTag'] = rec['messageTag']
  508. allAlerts[index]['AllAlert']['alertWord'] = rec['alertWord']
  509. allAlerts[index]['AllAlert']['alertMatchWord'] = rec['alertMatchWord']
  510. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  511. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  512. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  513. allAlerts[index]['AllAlert']['language'] = rec['language']
  514. allAlerts[index]['AllAlert']['manual'] = rec['manual']
  515. allAlerts[index]['AllAlert']['privacy'] = rec['privacy']
  516. allAlerts[index]['AllAlert']['sn_type'] = 'fb'
  517. if (lastFeedTime > str(rec['feedTime']) or lastFeedTime == ''):
  518. lastFeedTime = str(rec['feedTime'])
  519. index += 1
  520.  
  521. allImageAlertsRecord = db_conn.select_as_dict('tbl_fb_photos', where="userFbId='"+str(fbuser[0])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR smoking >1)")
  522.  
  523. for rec in allImageAlertsRecord:
  524. badImages = badImages + 1
  525. # create confirmed Alert category based on bit field value > 1
  526. confirmedAlertCat = []
  527. if str(rec['violence']) > '1':
  528. confirmedAlertCat.append('violence')
  529. if str(rec['inappropriate']) > '1':
  530. confirmedAlertCat.append('inappropriate')
  531. if str(rec['indecency']) > '1':
  532. confirmedAlertCat.append('indecency')
  533. if str(rec['alcohol']) > '1':
  534. confirmedAlertCat.append('alcohol')
  535. if str(rec['smoking']) > '1':
  536. confirmedAlertCat.append('smoking')
  537. if str(rec['questionable']) > '1':
  538. confirmedAlertCat.append('questionable')
  539.  
  540. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  541. allAlerts[index] = {}
  542. allAlerts[index]['AllAlert'] = {}
  543. allAlerts[index]['AllAlert']['algoQueryid'] = hashlib.md5(str(rec['feedId'])).hexdigest()
  544. allAlerts[index]['AllAlert']['feedId'] = rec['feedId']
  545. allAlerts[index]['AllAlert']['userFbId'] = rec['userFbId']
  546. allAlerts[index]['AllAlert']['wallFbId'] = rec['userFbId']
  547. allAlerts[index]['AllAlert']['fromName'] = ''
  548. allAlerts[index]['AllAlert']['fromFbId'] = rec['fromFbId']
  549. allAlerts[index]['AllAlert']['type'] = 'bad_image'
  550. allAlerts[index]['AllAlert']['description'] = rec['photo']
  551. allAlerts[index]['AllAlert']['photoType'] = rec['photoType']
  552. allAlerts[index]['AllAlert']['message'] = rec['name']
  553. allAlerts[index]['AllAlert']['messageTag'] = ''
  554. allAlerts[index]['AllAlert']['alertWord'] = ''
  555. allAlerts[index]['AllAlert']['alertMatchWord'] = ''
  556. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  557. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  558. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  559. allAlerts[index]['AllAlert']['manual'] = ''
  560. allAlerts[index]['AllAlert']['privacy'] = rec['privacy']
  561. allAlerts[index]['AllAlert']['photo'] = rec['photo']
  562. allAlerts[index]['AllAlert']['link'] = rec['link']
  563. allAlerts[index]['AllAlert']['sn_type'] = 'fb'
  564. index += 1
  565.  
  566. if twuser:
  567. allAlertsRecord = db_conn.select_as_dict('tbl_tw_text_alerts', where="userTwId='"+str(twuser[2])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR other >1)")
  568. for rec in allAlertsRecord:
  569. if 'photo' in rec['type']:
  570. badTweetPhoto = badTweetPhoto + 1
  571. else:
  572. badTweet = badTweet + 1
  573.  
  574. # create confirmed Alert category based on bit field value > 1
  575. confirmedAlertCat = []
  576. if str(rec['violence']) > '1':
  577. confirmedAlertCat.append('violence')
  578. if str(rec['inappropriate']) > '1':
  579. confirmedAlertCat.append('inappropriate')
  580. if str(rec['indecency']) > '1':
  581. confirmedAlertCat.append('indecency')
  582. if str(rec['alcohol']) > '1':
  583. confirmedAlertCat.append('alcohol')
  584. if str(rec['other']) > '1':
  585. confirmedAlertCat.append('other')
  586. if str(rec['questionable']) > '1':
  587. confirmedAlertCat.append('questionable')
  588.  
  589. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  590.  
  591. allAlerts[index] = {}
  592. allAlerts[index]['AllAlert'] = {}
  593. allAlerts[index]['AllAlert']['algoQueryid'] = rec['algoQueryId']
  594. allAlerts[index]['AllAlert']['tweetId'] = rec['tweetId']
  595. allAlerts[index]['AllAlert']['userTwId'] = rec['userTwId']
  596. allAlerts[index]['AllAlert']['fromTwId'] = rec['fromTwId']
  597. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  598. allAlerts[index]['AllAlert']['fromScreenName'] = rec['fromScreenName']
  599. allAlerts[index]['AllAlert']['type'] = rec['type']
  600. allAlerts[index]['AllAlert']['message'] = rec['message']
  601. allAlerts[index]['AllAlert']['alertWord'] = rec['alertWord']
  602. allAlerts[index]['AllAlert']['alertMatchWord'] = rec['alertMatchWord']
  603. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  604. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  605. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  606. allAlerts[index]['AllAlert']['language'] = rec['language']
  607. allAlerts[index]['AllAlert']['privacy'] = rec['privacy']
  608. allAlerts[index]['AllAlert']['possibleSensitive'] = rec['possibleSensitive']
  609. allAlerts[index]['AllAlert']['sn_type'] = 'tw'
  610. if lastFeedTime > str(rec['feedTime']) or lastFeedTime == '':
  611. lastFeedTime = str(rec['feedTime'])
  612. index += 1
  613.  
  614. allImageAlertsRecord = db_conn.select_as_dict('tbl_tw_photos', where="userTwId='"+str(twuser[2])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR smoking >1)")
  615.  
  616. for rec in allImageAlertsRecord:
  617. badImages = badImages + 1
  618. # create confirmed Alert category based on bit field value > 1
  619. confirmedAlertCat = []
  620. if str(rec['violence']) > '1':
  621. confirmedAlertCat.append('violence')
  622. if str(rec['inappropriate']) > '1':
  623. confirmedAlertCat.append('inappropriate')
  624. if str(rec['indecency']) > '1':
  625. confirmedAlertCat.append('indecency')
  626. if str(rec['alcohol']) > '1':
  627. confirmedAlertCat.append('alcohol')
  628. if str(rec['smoking']) > '1':
  629. confirmedAlertCat.append('smoking')
  630. if str(rec['questionable']) > '1':
  631. confirmedAlertCat.append('questionable')
  632.  
  633. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  634. allAlerts[index] = {}
  635. allAlerts[index]['AllAlert'] = {}
  636. allAlerts[index]['AllAlert']['algoQueryid'] = hashlib.md5(str(rec['tweetId'])).hexdigest()
  637. allAlerts[index]['AllAlert']['tweetId'] = rec['tweetId']
  638. allAlerts[index]['AllAlert']['userTwId'] = rec['userTwId']
  639. allAlerts[index]['AllAlert']['fromTwId'] = rec['fromTwId']
  640. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  641. allAlerts[index]['AllAlert']['fromScreenName'] = rec['fromName']
  642. allAlerts[index]['AllAlert']['type'] = 'bad_image'
  643. allAlerts[index]['AllAlert']['description'] = ''
  644. allAlerts[index]['AllAlert']['photoType'] = ''
  645. allAlerts[index]['AllAlert']['message'] = ''
  646. allAlerts[index]['AllAlert']['messageTag'] = ''
  647. allAlerts[index]['AllAlert']['alertWord'] = ''
  648. allAlerts[index]['AllAlert']['alertMatchWord'] = ''
  649. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  650. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  651. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  652. allAlerts[index]['AllAlert']['manual'] = ''
  653. allAlerts[index]['AllAlert']['privacy'] = rec['privacy']
  654. allAlerts[index]['AllAlert']['photo'] = rec['photo']
  655. imglink = "https://twitter.com/" + str(rec['screenName']) + "/status/" + str(rec['tweetId'])
  656. allAlerts[index]['AllAlert']['link'] = str(imglink)
  657. allAlerts[index]['AllAlert']['possibleSensitive'] = rec['possibleSensitive']
  658. allAlerts[index]['AllAlert']['sn_type'] = 'tw'
  659. index += 1
  660.  
  661. if inuser:
  662. allInAlertsRecord = db_conn.select_as_dict('tbl_insta_text_alerts',where="userInsId='"+str(inuser[2])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR other >1)")
  663. for rec in allInAlertsRecord:
  664. if rec['type'] == 'caption':
  665. badInstaCaption = badInstaCaption + 1
  666. else:
  667. badInstaComment = badInstaComment + 1
  668. # create confirmed Alert category based on bit field value > 1
  669. confirmedAlertCat = []
  670. if str(rec['violence']) > '1':
  671. confirmedAlertCat.append('violence')
  672. if str(rec['inappropriate']) > '1':
  673. confirmedAlertCat.append('inappropriate')
  674. if str(rec['indecency']) > '1':
  675. confirmedAlertCat.append('indecency')
  676. if str(rec['alcohol']) > '1':
  677. confirmedAlertCat.append('alcohol')
  678. if str(rec['other']) > '1':
  679. confirmedAlertCat.append('other')
  680. if str(rec['questionable']) > '1':
  681. confirmedAlertCat.append('questionable')
  682.  
  683. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  684. allAlerts[index] = {}
  685. allAlerts[index]['AllAlert'] = {}
  686. allAlerts[index]['AllAlert']['algoQueryid'] = rec['algoQueryId']
  687. allAlerts[index]['AllAlert']['mediaId'] = rec['mediaId']
  688. allAlerts[index]['AllAlert']['feedId'] = rec['feedId']
  689. allAlerts[index]['AllAlert']['userInsId'] = rec['userInsId']
  690. allAlerts[index]['AllAlert']['fromInsId'] = rec['fromInsId']
  691. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  692. allAlerts[index]['AllAlert']['fromScreenName'] = rec['fromScreenName']
  693. allAlerts[index]['AllAlert']['type'] = rec['type']
  694. allAlerts[index]['AllAlert']['message'] = rec['message']
  695. allAlerts[index]['AllAlert']['alertWord'] = rec['alertWord']
  696. allAlerts[index]['AllAlert']['alertMatchWord'] = rec['alertMatchWord']
  697. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  698. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  699. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  700. allAlerts[index]['AllAlert']['language'] = rec['language']
  701. allAlerts[index]['AllAlert']['link'] = rec['link']
  702. allAlerts[index]['AllAlert']['sn_type'] = 'in'
  703. index = index+1
  704.  
  705. allInImageAlertsRecord = db_conn.select_as_dict('tbl_insta_photos',where="userInsId='"+str(inuser[2])+"' and innocent=0 and (violence >1 OR inappropriate >1 OR indecency >1 OR alcohol >1 OR questionable >1 OR smoking >1)")
  706.  
  707. for rec in allInImageAlertsRecord:
  708. badImages = badImages + 1
  709. badInstaMedia = badInstaMedia + 1
  710. # create confirmed Alert category based on bit field value > 1
  711.  
  712. confirmedAlertCat = []
  713. if str(rec['violence']) > '1':
  714. confirmedAlertCat.append('violence')
  715. if str(rec['inappropriate']) > '1':
  716. confirmedAlertCat.append('inappropriate')
  717. if str(rec['indecency']) > '1':
  718. confirmedAlertCat.append('indecency')
  719. if str(rec['alcohol']) > '1':
  720. confirmedAlertCat.append('alcohol')
  721. if str(rec['smoking']) > '1':
  722. confirmedAlertCat.append('smoking')
  723. if str(rec['questionable']) > '1':
  724. confirmedAlertCat.append('questionable')
  725.  
  726. confirmedAlertCatStr = ','.join(confirmedAlertCat)
  727. allAlerts[index] = {}
  728. allAlerts[index]['AllAlert'] = {}
  729. allAlerts[index]['AllAlert']['algoQueryid'] = hashlib.md5(str(rec['mediaId'])).hexdigest()
  730. allAlerts[index]['AllAlert']['mediaId'] = rec['mediaId']
  731. allAlerts[index]['AllAlert']['feedId'] = rec['fromInsId']
  732. allAlerts[index]['AllAlert']['userInsId'] = rec['userInsId']
  733. allAlerts[index]['AllAlert']['fromInsId'] = rec['userInsId']
  734. allAlerts[index]['AllAlert']['fromName'] = rec['fromName']
  735. allAlerts[index]['AllAlert']['fromScreenName'] = rec['fromScreenName']
  736. allAlerts[index]['AllAlert']['type'] = 'bad_image'
  737. allAlerts[index]['AllAlert']['description'] = ''
  738. allAlerts[index]['AllAlert']['photoType'] = ''
  739. allAlerts[index]['AllAlert']['message'] = ''
  740. allAlerts[index]['AllAlert']['messageTag'] = ''
  741. allAlerts[index]['AllAlert']['alertWord'] = ''
  742. allAlerts[index]['AllAlert']['alertMatchWord'] = ''
  743. allAlerts[index]['AllAlert']['originalAlertCategory'] = ""
  744. allAlerts[index]['AllAlert']['confirmedAlertCategory'] = confirmedAlertCatStr
  745. allAlerts[index]['AllAlert']['feedTime'] = rec['feedTime']
  746. allAlerts[index]['AllAlert']['manual'] = ''
  747. allAlerts[index]['AllAlert']['photo'] = rec['photo']
  748. allAlerts[index]['AllAlert']['link'] = rec['link']
  749. allAlerts[index]['AllAlert']['sn_type'] = 'in'
  750. index = index + 1
  751.  
  752. if len(allAlertsRecord) == 0 and len(allImageAlertsRecord) == 0:
  753. report['noAlert'] = 1
  754.  
  755. oldCount['badPosts'] = badPost
  756. oldCount['badComments'] = badComments
  757. oldCount['badShares'] = badShare
  758. oldCount['badImages'] = badImages
  759. totalFeedCount = 0
  760.  
  761. totalAlert = badPost + badComments + badShare
  762. totalTweetAlert = badTweet + badTweetPhoto
  763. totalInAlert = badInstaComment + badInstaCaption + badInstaMedia
  764. totalImageCount = 0
  765. totalTweetCount = 0
  766. totalTweetMediaCount = 0
  767. totalInstaCount = 0
  768. totalInstaMediaCount = 0
  769.  
  770. if fbuser:
  771. images = db_conn.selectCount("SELECT count(id) from tbl_fb_photos where userFbId='"+str(fbuser[0])+"'")
  772. likes = db_conn.selectCount("SELECT count(id) from tbl_fb_photos where userFbId='"+str(fbuser[0])+"' and photoType='like_photo'")
  773. totalImageCount = images[0]
  774. totalLikeCount = likes[0]
  775.  
  776. fbcounts = db_conn.selectOne('tbl_fb_user_counts', "fbid='" + str(fbuser[0]) + "'")
  777. if fbcounts:
  778. textcount = json.loads(fbcounts[1])
  779. imagecount = json.loads(fbcounts[2])
  780. totalCounts['feeds'] = {}
  781. totalCounts['feeds']['all'] = textcount['commentCount'] + textcount['postCount'] + textcount['sharedCount']
  782. totalFeedCount = totalCounts['feeds']['all']
  783. totalCounts['feeds']['posts'] = textcount['postCount']
  784. totalCounts['feeds']['comments'] = textcount['commentCount']
  785. totalCounts['feeds']['shares'] = textcount['sharedCount']
  786. totalCounts['images'] = {}
  787. totalCounts['images']['tagged'] = imagecount['photoTagged']
  788. totalCounts['images']['upload'] = imagecount['photoUploadedCount']
  789. totalCounts['images']['like'] = imagecount['photoLikedCount']
  790. totalCounts['feeds']['images'] = totalImageCount
  791. totalCounts['feeds']['likes'] = totalLikeCount
  792. totalCounts['alerts'] = len(allAlertsRecord)
  793. totalImageCount = imagecount['photoTagged'] + imagecount['photoUploadedCount'] + imagecount['photoLikedCount']
  794.  
  795. if twuser:
  796. totalAlert = totalAlert + badTweetPhoto + badShare
  797. twcounts = db_conn.selectOne('tbl_tw_user_counts',"twid='"+str(twuser[2])+"'")
  798. tw_images = db_conn.selectCount("SELECT count(id) from tbl_tw_photos where userTwId='"+str(twuser[2])+"'")
  799. totalTweetMediaCount = tw_images[0]
  800. if twcounts:
  801. tweetcount = json.loads(twcounts[1])
  802. photocount = json.loads(twcounts[2])
  803. totalCounts['tweets'] = {}
  804. totalCounts['tweets']['all'] = tweetcount['tweet'] + tweetcount['retweet']
  805. totalTweetCount = totalCounts['tweets']['all']
  806. totalCounts['tweets']['tweet'] = tweetcount['tweet']
  807. totalCounts['tweets']['retweet'] = tweetcount['retweet']
  808. totalTweetCount = totalCounts['tweets']['all']
  809.  
  810. totalCounts['tweetimages'] = {}
  811. totalCounts['tweetimages']['all'] = photocount['tweet'] + photocount['retweet']
  812. totalCounts['tweetimages']['tweet'] = photocount['tweet']
  813. totalCounts['tweetimages']['retweet'] = photocount['retweet']
  814.  
  815. if inuser:
  816.  
  817. incounts = db_conn.selectOne('tbl_insta_user_counts',"instagramId='"+str(inuser[2])+"'")
  818. if incounts:
  819. intexcount = json.loads(incounts[2])
  820. inimagecount = incounts[1]
  821. totalCounts['instagram'] = {}
  822. totalCounts['instagram']['all'] = intexcount['commentCount'] + intexcount['captionCount']+inimagecount
  823. totalCounts['instagram']['comment'] = intexcount['commentCount']
  824. totalCounts['instagram']['caption'] = intexcount['captionCount']
  825. totalCounts['instagram']['media'] = inimagecount
  826. totalInstaCount = intexcount['commentCount'] + intexcount['captionCount']
  827. totalInstaMediaCount = inimagecount
  828.  
  829. report['oldCount'] = json.dumps(json.dumps(oldCount) )
  830. #new count and old count are equal for now, a new future feature will update the new count
  831. report['newCount'] = json.dumps(json.dumps(oldCount))
  832. report['netCount'] = json.dumps(json.dumps(totalCounts) )
  833.  
  834. report['joinningDate'] = str(user[0])
  835. report['approvalDate'] = str(user[1])
  836. from pprint import pprint
  837. pprint(allAlerts)
  838. report['report'] = json.dumps(allAlerts, default=date_handler)
  839. report['id'] = hashlib.md5(str(time.time())+str(fbuser[0])).hexdigest()
  840. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  841. try:
  842.  
  843. if report['reportType'] == '2':
  844. db_conn.updateRec('tbl_reports', "user_id='"+str(report['user_id'])+"'",
  845. report=report['report'],
  846. created=rectime,
  847. modified=rectime)
  848. db_conn.updateRec('tbl_users', "id='"+str(report['user_id'])+"'",
  849. is_new=1, mail_status="detail_report_mail", status=10, regenerate_report=0, modified=rectime, last_email_sent=rectime)
  850. logger.info("Detailed report regenerated for user %s",user_id)
  851. return
  852. if report['reportType'] == '3':
  853. db_conn.updateRec('tbl_reports', "user_id='"+str(report['user_id'])+"'",
  854. report=report['report'],
  855. created=rectime,
  856. modified=rectime)
  857. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",
  858. is_new=1, mail_status="detail_report_mail", status=10, regenerate_report=0, modified=rectime, last_email_sent=rectime)
  859. sendDailyReportMail(report['id'], report['user_id'], fbuser[0], fbuser[1], user[2], config, mailFlag, user[3], pro=True)
  860. logger.info("Detailed 24h report generated for user %s", user_id)
  861. return
  862.  
  863. db_conn.sqlQuery("INSERT into tbl_reports (id,user_id,report,newCounts,oldCounts,netCounts,approvalDate,reportType,created,modified) values ('" + str(report['id']) + "'," + report['user_id'] + "," + json.dumps(report['report'])+","+report['newCount']+","+report['oldCount']+","+report['netCount']+",'"+report['approvalDate']+"',"+str(report['reportType'])+",'"+rectime+"','"+rectime+"')")
  864. if sn == 'fb':
  865.  
  866. if 'noAlert' in report:
  867. if(sendNoDetailAlertMail(report['id'],report['user_id'],fbuser[0],fbuser[1],user[2],totalFeedCount,totalImageCount,totalCounts['feeds']['posts'],totalCounts['feeds']['comments'],totalCounts['feeds']['shares'],totalCounts['feeds']['likes'],config,mailFlag,user[3])):
  868. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  869. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=2,modified=rectime)
  870. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="detail_report_mail",status=10,modified=rectime, last_email_sent=rectime)
  871. else:
  872. if(sendDetailReportMail(report['id'],report['user_id'],fbuser[0],fbuser[1],user[2],badImages,totalImageCount,config,mailFlag,user[3])):
  873. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  874. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=1,modified=rectime)
  875. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="detail_report_mail", status=10, modified=rectime, last_email_sent=rectime)
  876.  
  877. elif sn == 'tw':
  878.  
  879. if 'noAlert' in report:
  880. if(sendNoDetailTwAlertMail(report['id'],report['user_id'],twuser[2],twuser[3],user[2],totalTweetCount,totalTweetMediaCount,config,mailFlag,user[3])):
  881. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  882. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=2,modified=rectime)
  883. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="detail_report_mail",twitterStatus=10,modified=rectime, last_email_sent=rectime)
  884. else:
  885. if(sendDetailTwReportMail(report['id'],report['user_id'],twuser[2],twuser[3],user[2],config,mailFlag,user[3])):
  886. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  887. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=1,modified=rectime)
  888. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="detail_report_mail",twitterStatus=10,modified=rectime, last_email_sent=rectime)
  889. elif sn == 'in':
  890.  
  891. if 'noAlert' in report:
  892. if(sendNoDetailInAlertMail(report['id'],report['user_id'],inuser[4],user[2],totalInstaCount,totalInstaMediaCount,config,mailFlag,user[3])):
  893. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  894. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=2,modified=rectime)
  895. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="detail_report_mail",instagramStatus=10,modified=rectime, last_email_sent=rectime)
  896. else:
  897. if(sendDetailInReportMail(report['id'], report['user_id'], inuser[4], user[2], config, mailFlag, user[3])):
  898. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  899. db_conn.updateRec('tbl_reports',"id='"+str(report['id'])+"'",mailStatus=1,modified=rectime)
  900. db_conn.updateRec('tbl_users',"id='"+str(report['user_id'])+"'",mail_status="detail_report_mail",instagramStatus=10,modified=rectime, last_email_sent=rectime)
  901.  
  902.  
  903. logger.info("Detailed report generated for user %s",user_id)
  904. except Exception as e:
  905. logger.error("Unable to create detailed report for user id %s with error %s"%(user_id,str(e)))
  906.  
  907.  
  908. # process to send email to admins that user is ready to review
  909. def notify_admin(type,user_id,db_conn,g_admin_emails,config):
  910.  
  911. logger = logging.getLogger("repnuplogger")
  912. fbuser = db_conn.selectOne('tbl_fb_users',"user_id='"+str(user_id)+"'",'fbid','fbname','textStatus','imageStatus')
  913.  
  914. logger.info("Sending mail to admin to review user for fb %s"%str(fbuser[0]))
  915. host_name = config.get('smtp_settings','host')
  916. user_name = config.get('smtp_settings','username')
  917. password = config.get('smtp_settings','password')
  918. mail_template_path = config.get('smtp_settings','mail_template_path')
  919. siteurl = config.get('admin_settings','site_url')
  920. sender = config.get('smtp_settings','sender')
  921. cc_address = config.get('admin_settings','cc_email')
  922.  
  923. if fbuser:
  924. if str(fbuser[2]) =="3":
  925. try:
  926. fo = open(os.path.join(mail_template_path, 'admin_report_ready.html'))
  927. filecontent = fo.read()
  928. fo.close()
  929. messagebody = filecontent.replace("site_url", siteurl)
  930. fbuser_name = fbuser[1].encode('utf-8')
  931. messagebody = messagebody.replace("fb_user_id", str(user_id))
  932. messagebody = messagebody.replace("fb_user_name", fbuser_name)
  933. fbuser_name = fbuser[1].encode('utf-8')
  934. msgRoot = MIMEMultipart('related')
  935. if type ==1:
  936. msgRoot['Subject'] = "Please review text for " + fbuser_name
  937. else :
  938. msgRoot['Subject'] = "Please review images for " + fbuser_name
  939.  
  940. msgRoot['From'] = "RepNup <reports@repnup.com>"
  941. msgRoot['To'] = g_admin_emails
  942.  
  943. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  944.  
  945. s = smtplib.SMTP(host_name)
  946. s.set_debuglevel(0)
  947. s.ehlo()
  948. if s.has_extn('STARTTLS'):
  949. s.starttls()
  950. s.ehlo()
  951. s.login(user_name,password)
  952. html_body = MIMEText(messagebody,'html')
  953. msg= MIMEMultipart('alternative')
  954. msgRoot.attach(msg)
  955. msg.attach(html_body)
  956. attachment = os.path.join(mail_template_path,'rep.png')
  957. fp = open(attachment,'rb')
  958. img = MIMEImage(fp.read())
  959. fp.close()
  960. img.add_header('Content-ID',"<header-image>")
  961. img.add_header('Content-Disposition', 'inline', filename='rep.png')
  962.  
  963. attachment2 = os.path.join(mail_template_path,'fb.png')
  964. fp1 = open(attachment2,'rb')
  965. fbimg = MIMEImage(fp1.read())
  966. fp1.close()
  967. fbimg.add_header('Content-ID',"<fb-icon-image>")
  968. fbimg.add_header('Content-Disposition', 'inline', filename='fb.png')
  969.  
  970. attachment3 = os.path.join(mail_template_path,'tw.png')
  971. fp2 = open(attachment3,'rb')
  972. twimg = MIMEImage(fp2.read())
  973. fp2.close()
  974. twimg.add_header('Content-ID',"<tw-icon-image>")
  975. twimg.add_header('Content-Disposition', 'inline', filename='tw.png')
  976.  
  977. msg.attach(img)
  978.  
  979.  
  980. TOADDR = [msgRoot['To']]
  981.  
  982. s.sendmail(sender,TOADDR,msgRoot.as_string())
  983.  
  984. s.quit()
  985. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  986. if type ==1:
  987. db_conn.updateRec('tbl_users',"id='"+str(user_id)+"'",mail_status="admin_review_mail",status=4,modified=rectime)
  988. else:
  989. db_conn.updateRec('tbl_users',"id='"+str(user_id)+"'",mail_status="admin_review_mail",status=8,modified=rectime)
  990. logger.info("Successfully send mail to admin to review user %s"%str(fbuser[0]))
  991. return True
  992. except Exception as e:
  993. logger.error("Error while sending email to admin for facebook user %s"%str(e))
  994. return False
  995. else:
  996. return False
  997.  
  998.  
  999. #Send initial report email to user
  1000. def sendInitialMail(report_id,user_id,fbid,fbname,email,totalFeedCount,totalAlert,totalImageCount,config,mailFlag,uuid):
  1001. if mailFlag == False:
  1002. return True
  1003. logger = logging.getLogger("repnuplogger")
  1004. logger.info("Sending initial summary mail to user %s",user_id)
  1005. host_name = config.get('smtp_settings','host')
  1006. user_name = config.get('smtp_settings','username')
  1007. password = config.get('smtp_settings','password')
  1008. mail_template_path = config.get('smtp_settings','mail_template_path')
  1009. siteurl = config.get('admin_settings','site_url')
  1010. sender = config.get('smtp_settings','sender')
  1011. cc_address = config.get('admin_settings','cc_email')
  1012. uuid = str(uuid)
  1013. #email = 'marcomtl@gmail.com';
  1014.  
  1015. try:
  1016. fo = open(os.path.join(mail_template_path,'initial_report.html'))
  1017. filecontent = fo.read()
  1018. fo.close()
  1019. messagebody = filecontent.replace("site_url",siteurl)
  1020. fbuser_name = fbname.encode('utf-8')
  1021. messagebody = messagebody.replace("app_user_name",fbuser_name)
  1022. messagebody = messagebody.replace("user_uuid",str(uuid))
  1023.  
  1024. msgRoot = MIMEMultipart('related')
  1025. msgRoot['Subject'] = "Your social media initial report from RepNup"
  1026. msgRoot['From'] = "RepNup <reports@repnup.com>"
  1027. msgRoot['To'] = email
  1028. msgRoot['Bcc'] = cc_address
  1029. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  1030.  
  1031. s = smtplib.SMTP(host_name)
  1032. s.set_debuglevel(0)
  1033. s.ehlo()
  1034. if s.has_extn('STARTTLS'):
  1035. s.starttls()
  1036. s.ehlo()
  1037. s.login(user_name,password)
  1038. html_body = MIMEText(messagebody,'html')
  1039. msg= MIMEMultipart('alternative')
  1040. msgRoot.attach(msg)
  1041. msg.attach(html_body)
  1042.  
  1043. attachment = os.path.join(mail_template_path,'logo.jpg')
  1044. fp = open(attachment,'rb')
  1045. img = MIMEImage(fp.read())
  1046. fp.close()
  1047. img.add_header('Content-ID',"<logo-image>")
  1048. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  1049.  
  1050. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  1051. fp1 = open(attachment2,'rb')
  1052. topleftwhiteimg = MIMEImage(fp1.read())
  1053. fp1.close()
  1054. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  1055. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  1056.  
  1057. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  1058. fp2 = open(attachment3,'rb')
  1059. toprightwhiteimg = MIMEImage(fp2.read())
  1060. fp2.close()
  1061. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  1062. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  1063.  
  1064. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  1065. fp3 = open(attachment4,'rb')
  1066. topleftblueimg = MIMEImage(fp3.read())
  1067. fp3.close()
  1068. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  1069. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  1070.  
  1071. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  1072. fp4 = open(attachment5,'rb')
  1073. toprightblueimg = MIMEImage(fp4.read())
  1074. fp4.close()
  1075. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  1076. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  1077.  
  1078. attachment6 = os.path.join(mail_template_path,'report_in_process.jpg')
  1079. fp5 = open(attachment6,'rb')
  1080. reportimg = MIMEImage(fp5.read())
  1081. fp5.close()
  1082. reportimg.add_header('Content-ID',"<report-image>")
  1083. reportimg.add_header('Content-Disposition', 'inline', filename='report_in_process.jpg')
  1084.  
  1085. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  1086. fp6 = open(attachment7,'rb')
  1087. bottomleftblueimg = MIMEImage(fp6.read())
  1088. fp6.close()
  1089. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  1090. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  1091.  
  1092. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  1093. fp7 = open(attachment8,'rb')
  1094. bottomrightblueimg = MIMEImage(fp7.read())
  1095. fp7.close()
  1096. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  1097. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  1098.  
  1099. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  1100. fp8 = open(attachment9,'rb')
  1101. bottomleftbrownimg = MIMEImage(fp8.read())
  1102. fp8.close()
  1103. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  1104. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  1105.  
  1106. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  1107. fp9 = open(attachment10,'rb')
  1108. bottomrightbrownimg = MIMEImage(fp9.read())
  1109. fp9.close()
  1110. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  1111. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  1112.  
  1113. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  1114. fp10 = open(attachment11,'rb')
  1115. fbimg = MIMEImage(fp10.read())
  1116. fp10.close()
  1117. fbimg.add_header('Content-ID',"<fb-image>")
  1118. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  1119.  
  1120. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  1121. fp11 = open(attachment12,'rb')
  1122. twimg = MIMEImage(fp11.read())
  1123. fp11.close()
  1124. twimg.add_header('Content-ID',"<tw-image>")
  1125. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  1126.  
  1127. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  1128. fp12 = open(attachment13,'rb')
  1129. lknimg = MIMEImage(fp12.read())
  1130. fp12.close()
  1131. lknimg.add_header('Content-ID',"<link-image>")
  1132. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  1133.  
  1134. msgRoot.attach(img)
  1135. msgRoot.attach(topleftwhiteimg)
  1136. msgRoot.attach(toprightwhiteimg)
  1137. msgRoot.attach(topleftblueimg)
  1138. msgRoot.attach(toprightblueimg)
  1139. msgRoot.attach(reportimg)
  1140. msgRoot.attach(bottomleftblueimg)
  1141. msgRoot.attach(bottomrightblueimg)
  1142. msgRoot.attach(bottomleftbrownimg)
  1143. msgRoot.attach(bottomrightbrownimg)
  1144. msgRoot.attach(fbimg)
  1145. msgRoot.attach(twimg)
  1146. msgRoot.attach(lknimg)
  1147.  
  1148. TOADDR = [msgRoot['To']]
  1149. CCADDR = [msgRoot['Bcc']]
  1150.  
  1151. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  1152. s.quit()
  1153. logger.info("Successfully send summary mail to user %s"%str(user_id))
  1154. return True
  1155. except Exception as e:
  1156. logger.error("Error while sending email %s"%str(e))
  1157. return False
  1158.  
  1159. # Send summary email to user
  1160. def sendSummaryMail(report_id, user_id, fbid, fbname, email, totalFeedCount, totalAlert, totalImageCount, config, mailFlag, uuid):
  1161. if mailFlag == False:
  1162. return True
  1163. logger = logging.getLogger("repnuplogger")
  1164. logger.info("Sending summary mail to user %s with report id %s"%(user_id,str(report_id)))
  1165. host_name = config.get('smtp_settings','host')
  1166. user_name = config.get('smtp_settings','username')
  1167. password = config.get('smtp_settings','password')
  1168. mail_template_path = config.get('smtp_settings','mail_template_path')
  1169. siteurl = config.get('admin_settings','site_url')
  1170. sender = config.get('smtp_settings','sender')
  1171. cc_address = config.get('admin_settings','cc_email')
  1172. #email = 'margomtl@gmail.com'
  1173. fbuser_name = fbname.encode('utf-8')
  1174. uuid = str(uuid)
  1175. try:
  1176. fo = open(os.path.join(mail_template_path,'summary_report.html'))
  1177. filecontent = fo.read()
  1178. fo.close()
  1179. messagebody = filecontent.replace("site_url",siteurl)
  1180.  
  1181. messagebody = messagebody.replace("app_user_name",str(fbuser_name))
  1182. messagebody = messagebody.replace("user_total_feed",str(totalFeedCount))
  1183. messagebody = messagebody.replace("user_total_alerts",str(totalAlert))
  1184. messagebody = messagebody.replace("user_total_images",str(totalImageCount))
  1185. messagebody = messagebody.replace("user_uuid",str(uuid))
  1186.  
  1187. msgRoot = MIMEMultipart('related')
  1188. msgRoot['Subject'] = "Your social media summary report from RepNup"
  1189. msgRoot['From'] = "RepNup <reports@repnup.com>"
  1190. msgRoot['To'] = email
  1191. msgRoot['Bcc'] = cc_address
  1192. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  1193.  
  1194. s = smtplib.SMTP(host_name)
  1195. s.set_debuglevel(0)
  1196. s.ehlo()
  1197. if s.has_extn('STARTTLS'):
  1198. s.starttls()
  1199. s.ehlo()
  1200. s.login(user_name,password)
  1201. html_body = MIMEText(messagebody,'html')
  1202. msg= MIMEMultipart('alternative')
  1203. msgRoot.attach(msg)
  1204. msg.attach(html_body)
  1205.  
  1206. attachment = os.path.join(mail_template_path,'logo.jpg')
  1207. fp = open(attachment,'rb')
  1208. img = MIMEImage(fp.read())
  1209. fp.close()
  1210. img.add_header('Content-ID',"<logo-image>")
  1211. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  1212.  
  1213. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  1214. fp1 = open(attachment2,'rb')
  1215. topleftwhiteimg = MIMEImage(fp1.read())
  1216. fp1.close()
  1217. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  1218. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  1219.  
  1220. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  1221. fp2 = open(attachment3,'rb')
  1222. toprightwhiteimg = MIMEImage(fp2.read())
  1223. fp2.close()
  1224. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  1225. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  1226.  
  1227. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  1228. fp3 = open(attachment4,'rb')
  1229. topleftblueimg = MIMEImage(fp3.read())
  1230. fp3.close()
  1231. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  1232. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  1233.  
  1234. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  1235. fp4 = open(attachment5,'rb')
  1236. toprightblueimg = MIMEImage(fp4.read())
  1237. fp4.close()
  1238. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  1239. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  1240.  
  1241. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  1242. fp5 = open(attachment6,'rb')
  1243. reportimg = MIMEImage(fp5.read())
  1244. fp5.close()
  1245. reportimg.add_header('Content-ID',"<report-ready-image>")
  1246. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  1247.  
  1248. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  1249. fp6 = open(attachment7,'rb')
  1250. bottomleftblueimg = MIMEImage(fp6.read())
  1251. fp6.close()
  1252. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  1253. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  1254.  
  1255. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  1256. fp7 = open(attachment8,'rb')
  1257. bottomrightblueimg = MIMEImage(fp7.read())
  1258. fp7.close()
  1259. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  1260. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  1261.  
  1262. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  1263. fp8 = open(attachment9,'rb')
  1264. bottomleftbrownimg = MIMEImage(fp8.read())
  1265. fp8.close()
  1266. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  1267. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  1268.  
  1269. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  1270. fp9 = open(attachment10,'rb')
  1271. bottomrightbrownimg = MIMEImage(fp9.read())
  1272. fp9.close()
  1273. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  1274. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  1275.  
  1276. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  1277. fp10 = open(attachment11,'rb')
  1278. fbimg = MIMEImage(fp10.read())
  1279. fp10.close()
  1280. fbimg.add_header('Content-ID',"<fb-image>")
  1281. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  1282.  
  1283. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  1284. fp11 = open(attachment12,'rb')
  1285. twimg = MIMEImage(fp11.read())
  1286. fp11.close()
  1287. twimg.add_header('Content-ID',"<tw-image>")
  1288. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  1289.  
  1290. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  1291. fp12 = open(attachment13,'rb')
  1292. lknimg = MIMEImage(fp12.read())
  1293. fp12.close()
  1294. lknimg.add_header('Content-ID',"<link-image>")
  1295. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  1296.  
  1297. attachment14 = os.path.join(mail_template_path,'top-left-green-corner.gif')
  1298. fp13 = open(attachment14,'rb')
  1299. topgreenleft = MIMEImage(fp13.read())
  1300. fp13.close()
  1301. topgreenleft.add_header('Content-ID',"top-left-green-image>")
  1302. topgreenleft.add_header('Content-Disposition', 'inline', filename='top-left-green-corner.gif')
  1303.  
  1304. attachment15 = os.path.join(mail_template_path,'top-right-green-corner.gif')
  1305. fp14 = open(attachment15,'rb')
  1306. topgreenright = MIMEImage(fp14.read())
  1307. fp14.close()
  1308. topgreenright.add_header('Content-ID',"top-right-green-image>")
  1309. topgreenright.add_header('Content-Disposition', 'inline', filename='top-right-green-corner.gif')
  1310.  
  1311. attachment16 = os.path.join(mail_template_path,'bottom-left-green-corner.gif')
  1312. fp15 = open(attachment16,'rb')
  1313. bottomleftgreen = MIMEImage(fp15.read())
  1314. fp15.close()
  1315. bottomleftgreen.add_header('Content-ID',"bottom-left-green-image>")
  1316. bottomleftgreen.add_header('Content-Disposition', 'inline', filename='bottom-left-green-corner.gif')
  1317.  
  1318. attachment17 = os.path.join(mail_template_path,'bottom-right-green-corner.gif')
  1319. fp16 = open(attachment17,'rb')
  1320. bottomrightgreen = MIMEImage(fp16.read())
  1321. fp16.close()
  1322. bottomrightgreen.add_header('Content-ID',"bottom-right-green-image>")
  1323. bottomrightgreen.add_header('Content-Disposition', 'inline', filename='bottom-right-green-corner.gif')
  1324.  
  1325. msgRoot.attach(img)
  1326. msgRoot.attach(topleftwhiteimg)
  1327. msgRoot.attach(toprightwhiteimg)
  1328. msgRoot.attach(topleftblueimg)
  1329. msgRoot.attach(toprightblueimg)
  1330. msgRoot.attach(reportimg)
  1331. msgRoot.attach(bottomleftblueimg)
  1332. msgRoot.attach(bottomrightblueimg)
  1333. msgRoot.attach(bottomleftbrownimg)
  1334. msgRoot.attach(bottomrightbrownimg)
  1335. msgRoot.attach(fbimg)
  1336. msgRoot.attach(twimg)
  1337. msgRoot.attach(lknimg)
  1338. msgRoot.attach(topgreenleft)
  1339. msgRoot.attach(topgreenright)
  1340. msgRoot.attach(bottomleftgreen)
  1341. msgRoot.attach(bottomrightgreen)
  1342.  
  1343. TOADDR = [msgRoot['To']]
  1344. CCADDR = [msgRoot['Bcc']]
  1345.  
  1346. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  1347.  
  1348.  
  1349. s.quit()
  1350. logger.info("Successfully send summary mail to user %s"%str(user_id))
  1351. return True
  1352. except Exception as e:
  1353. logger.error("Error while sending email %s"%str(e))
  1354. return False
  1355.  
  1356. # Send detail report email to user
  1357. def sendDetailReportMail(report_id,user_id,fbid,fbname,email,totalImageAlert,totalImageCount,config,mailFlag,uuid):
  1358. if mailFlag == False:
  1359. return True
  1360. logger = logging.getLogger("repnuplogger")
  1361. logger.info("Sending detail report to user %s",user_id)
  1362. host_name = config.get('smtp_settings','host')
  1363. user_name = config.get('smtp_settings','username')
  1364. password = config.get('smtp_settings','password')
  1365. mail_template_path = config.get('smtp_settings','mail_template_path')
  1366. siteurl = config.get('admin_settings','site_url')
  1367. sender = config.get('smtp_settings','sender')
  1368. cc_address = config.get('admin_settings','cc_email')
  1369. uuid = str(uuid)
  1370. #email = 'marcomtl@gmail.com'
  1371. if totalImageCount < totalImageAlert:
  1372. totalImageCount = totalImageAlert
  1373. try:
  1374. fo = open(os.path.join(mail_template_path,'detailed_report.html'))
  1375. filecontent = fo.read()
  1376. fo.close()
  1377. messagebody = filecontent.replace("site_url",siteurl)
  1378. fbuser_name = fbname.encode('utf-8')
  1379. messagebody = messagebody.replace("app_user_name",fbuser_name)
  1380. messagebody = messagebody.replace("image_alert_count",str(totalImageAlert))
  1381. messagebody = messagebody.replace("total_image_count",str(totalImageCount))
  1382. messagebody = messagebody.replace("user_uuid",uuid)
  1383.  
  1384. msgRoot = MIMEMultipart('related')
  1385. msgRoot['Subject'] = "Your social media detailed report from RepNup"
  1386. msgRoot['From'] = "RepNup <reports@repnup.com>"
  1387. msgRoot['To'] = email
  1388. msgRoot['Bcc'] = cc_address
  1389. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  1390.  
  1391. s = smtplib.SMTP(host_name)
  1392. s.set_debuglevel(0)
  1393. s.ehlo()
  1394. if s.has_extn('STARTTLS'):
  1395. s.starttls()
  1396. s.ehlo()
  1397. s.login(user_name,password)
  1398. html_body = MIMEText(messagebody,'html')
  1399. msg= MIMEMultipart('alternative')
  1400. msgRoot.attach(msg)
  1401. msg.attach(html_body)
  1402.  
  1403. attachment = os.path.join(mail_template_path,'logo.jpg')
  1404. fp = open(attachment,'rb')
  1405. img = MIMEImage(fp.read())
  1406. fp.close()
  1407. img.add_header('Content-ID',"<logo-image>")
  1408. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  1409.  
  1410. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  1411. fp1 = open(attachment2,'rb')
  1412. topleftwhiteimg = MIMEImage(fp1.read())
  1413. fp1.close()
  1414. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  1415. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  1416.  
  1417. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  1418. fp2 = open(attachment3,'rb')
  1419. toprightwhiteimg = MIMEImage(fp2.read())
  1420. fp2.close()
  1421. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  1422. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  1423.  
  1424. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  1425. fp3 = open(attachment4,'rb')
  1426. topleftblueimg = MIMEImage(fp3.read())
  1427. fp3.close()
  1428. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  1429. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  1430.  
  1431. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  1432. fp4 = open(attachment5,'rb')
  1433. toprightblueimg = MIMEImage(fp4.read())
  1434. fp4.close()
  1435. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  1436. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  1437.  
  1438. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  1439. fp5 = open(attachment6,'rb')
  1440. reportimg = MIMEImage(fp5.read())
  1441. fp5.close()
  1442. reportimg.add_header('Content-ID',"<report-ready-image>")
  1443. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  1444.  
  1445. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  1446. fp6 = open(attachment7,'rb')
  1447. bottomleftblueimg = MIMEImage(fp6.read())
  1448. fp6.close()
  1449. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  1450. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  1451.  
  1452. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  1453. fp7 = open(attachment8,'rb')
  1454. bottomrightblueimg = MIMEImage(fp7.read())
  1455. fp7.close()
  1456. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  1457. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  1458.  
  1459. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  1460. fp8 = open(attachment9,'rb')
  1461. bottomleftbrownimg = MIMEImage(fp8.read())
  1462. fp8.close()
  1463. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  1464. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  1465.  
  1466. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  1467. fp9 = open(attachment10,'rb')
  1468. bottomrightbrownimg = MIMEImage(fp9.read())
  1469. fp9.close()
  1470. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  1471. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  1472.  
  1473. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  1474. fp10 = open(attachment11,'rb')
  1475. fbimg = MIMEImage(fp10.read())
  1476. fp10.close()
  1477. fbimg.add_header('Content-ID',"<fb-image>")
  1478. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  1479.  
  1480. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  1481. fp11 = open(attachment12,'rb')
  1482. twimg = MIMEImage(fp11.read())
  1483. fp11.close()
  1484. twimg.add_header('Content-ID',"<tw-image>")
  1485. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  1486.  
  1487. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  1488. fp12 = open(attachment13,'rb')
  1489. lknimg = MIMEImage(fp12.read())
  1490. fp12.close()
  1491. lknimg.add_header('Content-ID',"<link-image>")
  1492. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  1493.  
  1494. msgRoot.attach(img)
  1495. msgRoot.attach(topleftwhiteimg)
  1496. msgRoot.attach(toprightwhiteimg)
  1497. msgRoot.attach(topleftblueimg)
  1498. msgRoot.attach(toprightblueimg)
  1499. msgRoot.attach(reportimg)
  1500. msgRoot.attach(bottomleftblueimg)
  1501. msgRoot.attach(bottomrightblueimg)
  1502. msgRoot.attach(bottomleftbrownimg)
  1503. msgRoot.attach(bottomrightbrownimg)
  1504. msgRoot.attach(fbimg)
  1505. msgRoot.attach(twimg)
  1506. msgRoot.attach(lknimg)
  1507.  
  1508.  
  1509. TOADDR = [msgRoot['To']]
  1510. CCADDR = [msgRoot['Bcc']]
  1511.  
  1512. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  1513.  
  1514. s.quit()
  1515. logger.info("Successfully send detail report mail to user %s"%str(user_id))
  1516. return True
  1517. except Exception as e:
  1518. logger.error("Error while sending email %s"%str(e))
  1519. return False
  1520.  
  1521.  
  1522. # Send detail report email to user
  1523. def sendDailyReportMail(report_id, user_id, fbid, fbname, email, config, mailFlag, uuid, pro=False):
  1524. if mailFlag == False:
  1525. return True
  1526. logger = logging.getLogger("repnuplogger")
  1527. logger.info("Sending detail report to user %s",user_id)
  1528. host_name = config.get('smtp_settings','host')
  1529. user_name = config.get('smtp_settings','username')
  1530. password = config.get('smtp_settings','password')
  1531. mail_template_path = config.get('smtp_settings','mail_template_path')
  1532. siteurl = config.get('admin_settings','site_url')
  1533. sender = config.get('smtp_settings','sender')
  1534. cc_address = config.get('admin_settings','cc_email')
  1535. uuid = str(uuid)
  1536. fbuser_name = fbname.encode('utf-8')
  1537.  
  1538. #email = 'marcomtl@gmail.com'
  1539. # if totalImageCount < totalImageAlert:
  1540. # totalImageCount = totalImageAlert
  1541. try:
  1542. if pro:
  1543. fo = open(os.path.join(mail_template_path, '24h_report_pro.html'))
  1544. else:
  1545. fo = open(os.path.join(mail_template_path, '24h_report.html'))
  1546. filecontent = fo.read()
  1547. fo.close()
  1548. messagebody = filecontent.replace("site_url",siteurl)
  1549. fbuser_name = fbname.encode('utf-8')
  1550. messagebody = messagebody.replace("app_user_name",fbuser_name)
  1551. messagebody = messagebody.replace("user_uuid",uuid)
  1552. #messagebody = messagebody.replace("image_alert_count", str(totalImageAlert))
  1553. #messagebody = messagebody.replace("total_image_count", str(totalImageCount))
  1554.  
  1555. msgRoot = MIMEMultipart('related')
  1556. msgRoot['Subject'] = "24hour report from RepNup"
  1557. msgRoot['From'] = "RepNup <reports@repnup.com>"
  1558. msgRoot['To'] = email
  1559. msgRoot['Bcc'] = cc_address
  1560. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  1561.  
  1562. s = smtplib.SMTP(host_name)
  1563. s.set_debuglevel(0)
  1564. s.ehlo()
  1565. if s.has_extn('STARTTLS'):
  1566. s.starttls()
  1567. s.ehlo()
  1568. s.login(user_name,password)
  1569. html_body = MIMEText(messagebody,'html')
  1570. msg= MIMEMultipart('alternative')
  1571. msgRoot.attach(msg)
  1572. msg.attach(html_body)
  1573.  
  1574. attachment = os.path.join(mail_template_path,'logo.jpg')
  1575. fp = open(attachment,'rb')
  1576. img = MIMEImage(fp.read())
  1577. fp.close()
  1578. img.add_header('Content-ID',"<logo-image>")
  1579. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  1580.  
  1581. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  1582. fp1 = open(attachment2,'rb')
  1583. topleftwhiteimg = MIMEImage(fp1.read())
  1584. fp1.close()
  1585. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  1586. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  1587.  
  1588. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  1589. fp2 = open(attachment3,'rb')
  1590. toprightwhiteimg = MIMEImage(fp2.read())
  1591. fp2.close()
  1592. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  1593. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  1594.  
  1595. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  1596. fp3 = open(attachment4,'rb')
  1597. topleftblueimg = MIMEImage(fp3.read())
  1598. fp3.close()
  1599. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  1600. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  1601.  
  1602. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  1603. fp4 = open(attachment5,'rb')
  1604. toprightblueimg = MIMEImage(fp4.read())
  1605. fp4.close()
  1606. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  1607. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  1608.  
  1609. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  1610. fp5 = open(attachment6,'rb')
  1611. reportimg = MIMEImage(fp5.read())
  1612. fp5.close()
  1613. reportimg.add_header('Content-ID',"<report-ready-image>")
  1614. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  1615.  
  1616. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  1617. fp6 = open(attachment7,'rb')
  1618. bottomleftblueimg = MIMEImage(fp6.read())
  1619. fp6.close()
  1620. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  1621. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  1622.  
  1623. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  1624. fp7 = open(attachment8,'rb')
  1625. bottomrightblueimg = MIMEImage(fp7.read())
  1626. fp7.close()
  1627. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  1628. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  1629.  
  1630. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  1631. fp8 = open(attachment9,'rb')
  1632. bottomleftbrownimg = MIMEImage(fp8.read())
  1633. fp8.close()
  1634. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  1635. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  1636.  
  1637. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  1638. fp9 = open(attachment10,'rb')
  1639. bottomrightbrownimg = MIMEImage(fp9.read())
  1640. fp9.close()
  1641. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  1642. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  1643.  
  1644. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  1645. fp10 = open(attachment11,'rb')
  1646. fbimg = MIMEImage(fp10.read())
  1647. fp10.close()
  1648. fbimg.add_header('Content-ID',"<fb-image>")
  1649. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  1650.  
  1651. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  1652. fp11 = open(attachment12,'rb')
  1653. twimg = MIMEImage(fp11.read())
  1654. fp11.close()
  1655. twimg.add_header('Content-ID',"<tw-image>")
  1656. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  1657.  
  1658. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  1659. fp12 = open(attachment13,'rb')
  1660. lknimg = MIMEImage(fp12.read())
  1661. fp12.close()
  1662. lknimg.add_header('Content-ID',"<link-image>")
  1663. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  1664.  
  1665. attachment14 = os.path.join(mail_template_path,'top-left-green-corner.gif')
  1666. fp13 = open(attachment14,'rb')
  1667. topgreenleft = MIMEImage(fp13.read())
  1668. fp13.close()
  1669. topgreenleft.add_header('Content-ID',"top-left-green-image>")
  1670. topgreenleft.add_header('Content-Disposition', 'inline', filename='top-left-green-corner.gif')
  1671.  
  1672. attachment15 = os.path.join(mail_template_path,'top-right-green-corner.gif')
  1673. fp14 = open(attachment15,'rb')
  1674. topgreenright = MIMEImage(fp14.read())
  1675. fp14.close()
  1676. topgreenright.add_header('Content-ID',"top-right-green-image>")
  1677. topgreenright.add_header('Content-Disposition', 'inline', filename='top-right-green-corner.gif')
  1678.  
  1679. attachment16 = os.path.join(mail_template_path,'bottom-left-green-corner.gif')
  1680. fp15 = open(attachment16,'rb')
  1681. bottomleftgreen = MIMEImage(fp15.read())
  1682. fp15.close()
  1683. bottomleftgreen.add_header('Content-ID',"bottom-left-green-image>")
  1684. bottomleftgreen.add_header('Content-Disposition', 'inline', filename='bottom-left-green-corner.gif')
  1685.  
  1686. attachment17 = os.path.join(mail_template_path,'bottom-right-green-corner.gif')
  1687. fp16 = open(attachment17,'rb')
  1688. bottomrightgreen = MIMEImage(fp16.read())
  1689. fp16.close()
  1690. bottomrightgreen.add_header('Content-ID',"bottom-right-green-image>")
  1691. bottomrightgreen.add_header('Content-Disposition', 'inline', filename='bottom-right-green-corner.gif')
  1692.  
  1693. msgRoot.attach(img)
  1694. msgRoot.attach(topleftwhiteimg)
  1695. msgRoot.attach(toprightwhiteimg)
  1696. msgRoot.attach(topleftblueimg)
  1697. msgRoot.attach(toprightblueimg)
  1698. msgRoot.attach(reportimg)
  1699. msgRoot.attach(bottomleftblueimg)
  1700. msgRoot.attach(bottomrightblueimg)
  1701. msgRoot.attach(bottomleftbrownimg)
  1702. msgRoot.attach(bottomrightbrownimg)
  1703. msgRoot.attach(fbimg)
  1704. msgRoot.attach(twimg)
  1705. msgRoot.attach(lknimg)
  1706. msgRoot.attach(topgreenleft)
  1707. msgRoot.attach(topgreenright)
  1708. msgRoot.attach(bottomleftgreen)
  1709. msgRoot.attach(bottomrightgreen)
  1710.  
  1711.  
  1712. TOADDR = [msgRoot['To']]
  1713. CCADDR = [msgRoot['Bcc']]
  1714.  
  1715. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  1716.  
  1717. s.quit()
  1718. logger.info("Successfully send 24hour report mail to user %s"%str(user_id))
  1719. return True
  1720. except Exception as e:
  1721. logger.error("Error while sending 24hour email %s"%str(e))
  1722. return False
  1723.  
  1724.  
  1725. def sendNoAlertMail(report_id, user_id, fbid, fbname, email, totalFeedCount, totalImageCount, totalPost, totalComment, totalShare, totalLike, config, mailFlag, uuid):
  1726. if mailFlag == False:
  1727. return True
  1728. logger = logging.getLogger("repnuplogger")
  1729. logger.info("Sending no alert email to user %s",user_id)
  1730. host_name = config.get('smtp_settings','host')
  1731. user_name = config.get('smtp_settings','username')
  1732. password = config.get('smtp_settings','password')
  1733. mail_template_path = config.get('smtp_settings','mail_template_path')
  1734. siteurl = config.get('admin_settings','site_url')
  1735. sender = config.get('smtp_settings','sender')
  1736. cc_address = config.get('admin_settings','cc_email')
  1737. #email = 'marcomtl@gmail.com'
  1738. uuid = str(uuid)
  1739. try:
  1740. fo = open(os.path.join(mail_template_path,'no_alert_report.html'))
  1741. filecontent = fo.read()
  1742. fo.close()
  1743. messagebody = filecontent.replace("site_url",siteurl)
  1744. fbuser_name = fbname.encode('utf-8')
  1745. messagebody = messagebody.replace("app_user_name",fbuser_name)
  1746. messagebody = messagebody.replace("user_total_feed",str(totalFeedCount))
  1747. messagebody = messagebody.replace("user_total_images",str(totalImageCount))
  1748. messagebody = messagebody.replace("total_feed_post",str(totalPost))
  1749. messagebody = messagebody.replace("total_comments",str(totalComment))
  1750. messagebody = messagebody.replace("total_share",str(totalShare))
  1751. messagebody = messagebody.replace("total_likes",str(totalLike))
  1752. messagebody = messagebody.replace("user_uuid",uuid)
  1753.  
  1754. msgRoot = MIMEMultipart('related')
  1755. msgRoot['Subject'] = "Your social media report from RepNup"
  1756. msgRoot['From'] = "RepNup <reports@repnup.com>"
  1757. msgRoot['To'] = email
  1758. msgRoot['Bcc'] = cc_address
  1759. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  1760.  
  1761. s = smtplib.SMTP(host_name)
  1762. s.set_debuglevel(0)
  1763. s.ehlo()
  1764. if s.has_extn('STARTTLS'):
  1765. s.starttls()
  1766. s.ehlo()
  1767. s.login(user_name,password)
  1768. html_body = MIMEText(messagebody,'html')
  1769. msg= MIMEMultipart('alternative')
  1770. msgRoot.attach(msg)
  1771. msg.attach(html_body)
  1772.  
  1773. attachment = os.path.join(mail_template_path,'logo.jpg')
  1774. fp = open(attachment,'rb')
  1775. img = MIMEImage(fp.read())
  1776. fp.close()
  1777. img.add_header('Content-ID',"<logo-image>")
  1778. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  1779.  
  1780. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  1781. fp1 = open(attachment2,'rb')
  1782. topleftwhiteimg = MIMEImage(fp1.read())
  1783. fp1.close()
  1784. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  1785. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  1786.  
  1787. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  1788. fp2 = open(attachment3,'rb')
  1789. toprightwhiteimg = MIMEImage(fp2.read())
  1790. fp2.close()
  1791. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  1792. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  1793.  
  1794. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  1795. fp3 = open(attachment4,'rb')
  1796. topleftblueimg = MIMEImage(fp3.read())
  1797. fp3.close()
  1798. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  1799. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  1800.  
  1801. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  1802. fp4 = open(attachment5,'rb')
  1803. toprightblueimg = MIMEImage(fp4.read())
  1804. fp4.close()
  1805. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  1806. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  1807.  
  1808. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  1809. fp5 = open(attachment6,'rb')
  1810. reportimg = MIMEImage(fp5.read())
  1811. fp5.close()
  1812. reportimg.add_header('Content-ID',"<report-ready-image>")
  1813. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  1814.  
  1815. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  1816. fp6 = open(attachment7,'rb')
  1817. bottomleftblueimg = MIMEImage(fp6.read())
  1818. fp6.close()
  1819. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  1820. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  1821.  
  1822. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  1823. fp7 = open(attachment8,'rb')
  1824. bottomrightblueimg = MIMEImage(fp7.read())
  1825. fp7.close()
  1826. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  1827. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  1828.  
  1829. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  1830. fp8 = open(attachment9,'rb')
  1831. bottomleftbrownimg = MIMEImage(fp8.read())
  1832. fp8.close()
  1833. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  1834. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  1835.  
  1836. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  1837. fp9 = open(attachment10,'rb')
  1838. bottomrightbrownimg = MIMEImage(fp9.read())
  1839. fp9.close()
  1840. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  1841. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  1842.  
  1843. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  1844. fp10 = open(attachment11,'rb')
  1845. fbimg = MIMEImage(fp10.read())
  1846. fp10.close()
  1847. fbimg.add_header('Content-ID',"<fb-image>")
  1848. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  1849.  
  1850. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  1851. fp11 = open(attachment12,'rb')
  1852. twimg = MIMEImage(fp11.read())
  1853. fp11.close()
  1854. twimg.add_header('Content-ID',"<tw-image>")
  1855. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  1856.  
  1857. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  1858. fp12 = open(attachment13,'rb')
  1859. lknimg = MIMEImage(fp12.read())
  1860. fp12.close()
  1861. lknimg.add_header('Content-ID',"<link-image>")
  1862. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  1863.  
  1864.  
  1865. msgRoot.attach(img)
  1866. msgRoot.attach(topleftwhiteimg)
  1867. msgRoot.attach(toprightwhiteimg)
  1868. msgRoot.attach(topleftblueimg)
  1869. msgRoot.attach(toprightblueimg)
  1870. msgRoot.attach(reportimg)
  1871. msgRoot.attach(bottomleftblueimg)
  1872. msgRoot.attach(bottomrightblueimg)
  1873. msgRoot.attach(bottomleftbrownimg)
  1874. msgRoot.attach(bottomrightbrownimg)
  1875. msgRoot.attach(fbimg)
  1876. msgRoot.attach(twimg)
  1877. msgRoot.attach(lknimg)
  1878.  
  1879. TOADDR = [msgRoot['To']]
  1880. CCADDR = [msgRoot['Bcc']]
  1881.  
  1882. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  1883.  
  1884. s.quit()
  1885. logger.info("Successfully send no alert mail to user %s"%str(user_id))
  1886. return True
  1887. except Exception as e:
  1888. logger.error("Error while sending email %s"%str(e))
  1889. return False
  1890. # Send no alert email to user
  1891. def sendNoDetailAlertMail(report_id,user_id,fbid,fbname,email,totalFeedCount,totalImageCount,totalPost,totalComment,totalShare,totalLike,config,mailFlag,uuid):
  1892. if mailFlag == False:
  1893. return True
  1894. logger = logging.getLogger("repnuplogger")
  1895. logger.info("Sending no detail alert mail to user %s",user_id)
  1896. host_name = config.get('smtp_settings','host')
  1897.  
  1898. user_name = config.get('smtp_settings','username')
  1899. password = config.get('smtp_settings','password')
  1900. mail_template_path = config.get('smtp_settings','mail_template_path')
  1901. siteurl = config.get('admin_settings','site_url')
  1902. sender = config.get('smtp_settings','sender')
  1903. cc_address = config.get('admin_settings','cc_email')
  1904. #email = 'marcomtl@gmail.com'
  1905. uuid = str(uuid)
  1906. try:
  1907. fo = open(os.path.join(mail_template_path,'no_alert_detail_report.html'))
  1908. filecontent = fo.read()
  1909. fo.close()
  1910. messagebody = filecontent.replace("site_url",siteurl)
  1911. fbuser_name = fbname.encode('utf-8')
  1912. messagebody = messagebody.replace("app_user_name",fbuser_name)
  1913. messagebody = messagebody.replace("user_total_feed",str(totalFeedCount))
  1914. messagebody = messagebody.replace("user_total_images",str(totalImageCount))
  1915. messagebody = messagebody.replace("total_feed_post",str(totalPost))
  1916. messagebody = messagebody.replace("total_comments",str(totalComment))
  1917. messagebody = messagebody.replace("total_share",str(totalShare))
  1918. messagebody = messagebody.replace("total_likes",str(totalLike))
  1919. messagebody = messagebody.replace("user_uuid",uuid)
  1920.  
  1921. msgRoot = MIMEMultipart('related')
  1922. msgRoot['Subject'] = "Your social media report from RepNup"
  1923. msgRoot['From'] = "RepNup <reports@repnup.com>"
  1924. msgRoot['To'] = email
  1925. msgRoot['Bcc'] = cc_address
  1926. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  1927.  
  1928. s = smtplib.SMTP(host_name)
  1929. s.set_debuglevel(0)
  1930. s.ehlo()
  1931. if s.has_extn('STARTTLS'):
  1932. s.starttls()
  1933. s.ehlo()
  1934. s.login(user_name,password)
  1935. html_body = MIMEText(messagebody,'html')
  1936. msg= MIMEMultipart('alternative')
  1937. msgRoot.attach(msg)
  1938. msg.attach(html_body)
  1939.  
  1940. attachment = os.path.join(mail_template_path,'logo.jpg')
  1941. fp = open(attachment,'rb')
  1942. img = MIMEImage(fp.read())
  1943. fp.close()
  1944. img.add_header('Content-ID',"<logo-image>")
  1945. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  1946.  
  1947. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  1948. fp1 = open(attachment2,'rb')
  1949. topleftwhiteimg = MIMEImage(fp1.read())
  1950. fp1.close()
  1951. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  1952. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  1953.  
  1954. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  1955. fp2 = open(attachment3,'rb')
  1956. toprightwhiteimg = MIMEImage(fp2.read())
  1957. fp2.close()
  1958. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  1959. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  1960.  
  1961. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  1962. fp3 = open(attachment4,'rb')
  1963. topleftblueimg = MIMEImage(fp3.read())
  1964. fp3.close()
  1965. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  1966. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  1967.  
  1968. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  1969. fp4 = open(attachment5,'rb')
  1970. toprightblueimg = MIMEImage(fp4.read())
  1971. fp4.close()
  1972. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  1973. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  1974.  
  1975. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  1976. fp5 = open(attachment6,'rb')
  1977. reportimg = MIMEImage(fp5.read())
  1978. fp5.close()
  1979. reportimg.add_header('Content-ID',"<report-ready-image>")
  1980. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  1981.  
  1982. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  1983. fp6 = open(attachment7,'rb')
  1984. bottomleftblueimg = MIMEImage(fp6.read())
  1985. fp6.close()
  1986. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  1987. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  1988.  
  1989. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  1990. fp7 = open(attachment8,'rb')
  1991. bottomrightblueimg = MIMEImage(fp7.read())
  1992. fp7.close()
  1993. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  1994. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  1995.  
  1996. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  1997. fp8 = open(attachment9,'rb')
  1998. bottomleftbrownimg = MIMEImage(fp8.read())
  1999. fp8.close()
  2000. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  2001. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  2002.  
  2003. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  2004. fp9 = open(attachment10,'rb')
  2005. bottomrightbrownimg = MIMEImage(fp9.read())
  2006. fp9.close()
  2007. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  2008. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  2009.  
  2010. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  2011. fp10 = open(attachment11,'rb')
  2012. fbimg = MIMEImage(fp10.read())
  2013. fp10.close()
  2014. fbimg.add_header('Content-ID',"<fb-image>")
  2015. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  2016.  
  2017. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  2018. fp11 = open(attachment12,'rb')
  2019. twimg = MIMEImage(fp11.read())
  2020. fp11.close()
  2021. twimg.add_header('Content-ID',"<tw-image>")
  2022. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  2023.  
  2024. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  2025. fp12 = open(attachment13,'rb')
  2026. lknimg = MIMEImage(fp12.read())
  2027. fp12.close()
  2028. lknimg.add_header('Content-ID',"<link-image>")
  2029. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  2030.  
  2031. msgRoot.attach(img)
  2032. msgRoot.attach(topleftwhiteimg)
  2033. msgRoot.attach(toprightwhiteimg)
  2034. msgRoot.attach(topleftblueimg)
  2035. msgRoot.attach(toprightblueimg)
  2036. msgRoot.attach(reportimg)
  2037. msgRoot.attach(bottomleftblueimg)
  2038. msgRoot.attach(bottomrightblueimg)
  2039. msgRoot.attach(bottomleftbrownimg)
  2040. msgRoot.attach(bottomrightbrownimg)
  2041. msgRoot.attach(fbimg)
  2042. msgRoot.attach(twimg)
  2043. msgRoot.attach(lknimg)
  2044.  
  2045. TOADDR = [msgRoot['To']]
  2046. CCADDR = [msgRoot['Bcc']]
  2047.  
  2048. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  2049.  
  2050. s.quit()
  2051. logger.info("Successfully send no detailed alert mail to user %s"%str(user_id))
  2052. return True
  2053. except Exception as e:
  2054. logger.error("Error while sending email %s"%str(e))
  2055. return False
  2056.  
  2057. # Send no alert email to user
  2058. def sendNoTwAlertMail(report_id,user_id,twid,twname,email,totalFeed,totalTweetCount,totalImageCount,config,mailFlag,uuid):
  2059. if mailFlag == False:
  2060. return True
  2061. logger = logging.getLogger("repnuplogger")
  2062. logger.info("Sending no alert email to user %s",user_id)
  2063. host_name = config.get('smtp_settings','host')
  2064. user_name = config.get('smtp_settings','username')
  2065. password = config.get('smtp_settings','password')
  2066. mail_template_path = config.get('smtp_settings','mail_template_path')
  2067. siteurl = config.get('admin_settings','site_url')
  2068. sender = config.get('smtp_settings','sender')
  2069. cc_address = config.get('admin_settings','cc_email')
  2070. #email = 'marcomtl@gmail.com'
  2071. uuid = str(uuid)
  2072. try:
  2073. fo = open(os.path.join(mail_template_path,'no_alert_tw_report.html'))
  2074. filecontent = fo.read()
  2075. fo.close()
  2076. messagebody = filecontent.replace("site_url",siteurl)
  2077. twname = twname.encode('utf-8')
  2078. messagebody = messagebody.replace("app_user_name",twname)
  2079. messagebody = messagebody.replace("user_total_feed ",str(totalTweetCount))
  2080. messagebody = messagebody.replace("total_tweet",str(totalTweetCount))
  2081. messagebody = messagebody.replace("total_photos",str(totalImageCount))
  2082. messagebody = messagebody.replace("user_uuid",uuid)
  2083.  
  2084. msgRoot = MIMEMultipart('related')
  2085. msgRoot['Subject'] = "Your social media report for Twitter from RepNup"
  2086. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2087. msgRoot['To'] = email
  2088. msgRoot['Bcc'] = cc_address
  2089. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2090.  
  2091. s = smtplib.SMTP(host_name)
  2092. s.set_debuglevel(0)
  2093. s.ehlo()
  2094. if s.has_extn('STARTTLS'):
  2095. s.starttls()
  2096. s.ehlo()
  2097. s.login(user_name,password)
  2098. html_body = MIMEText(messagebody,'html')
  2099. msg= MIMEMultipart('alternative')
  2100. msgRoot.attach(msg)
  2101. msg.attach(html_body)
  2102.  
  2103. attachment = os.path.join(mail_template_path,'logo.jpg')
  2104. fp = open(attachment,'rb')
  2105. img = MIMEImage(fp.read())
  2106. fp.close()
  2107. img.add_header('Content-ID',"<logo-image>")
  2108. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  2109.  
  2110. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  2111. fp1 = open(attachment2,'rb')
  2112. topleftwhiteimg = MIMEImage(fp1.read())
  2113. fp1.close()
  2114. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  2115. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  2116.  
  2117. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  2118. fp2 = open(attachment3,'rb')
  2119. toprightwhiteimg = MIMEImage(fp2.read())
  2120. fp2.close()
  2121. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  2122. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  2123.  
  2124. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  2125. fp3 = open(attachment4,'rb')
  2126. topleftblueimg = MIMEImage(fp3.read())
  2127. fp3.close()
  2128. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  2129. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  2130.  
  2131. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  2132. fp4 = open(attachment5,'rb')
  2133. toprightblueimg = MIMEImage(fp4.read())
  2134. fp4.close()
  2135. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  2136. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  2137.  
  2138. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  2139. fp5 = open(attachment6,'rb')
  2140. reportimg = MIMEImage(fp5.read())
  2141. fp5.close()
  2142. reportimg.add_header('Content-ID',"<report-ready-image>")
  2143. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  2144.  
  2145. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  2146. fp6 = open(attachment7,'rb')
  2147. bottomleftblueimg = MIMEImage(fp6.read())
  2148. fp6.close()
  2149. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  2150. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  2151.  
  2152. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  2153. fp7 = open(attachment8,'rb')
  2154. bottomrightblueimg = MIMEImage(fp7.read())
  2155. fp7.close()
  2156. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  2157. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  2158.  
  2159. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  2160. fp8 = open(attachment9,'rb')
  2161. bottomleftbrownimg = MIMEImage(fp8.read())
  2162. fp8.close()
  2163. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  2164. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  2165.  
  2166. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  2167. fp9 = open(attachment10,'rb')
  2168. bottomrightbrownimg = MIMEImage(fp9.read())
  2169. fp9.close()
  2170. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  2171. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  2172.  
  2173. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  2174. fp10 = open(attachment11,'rb')
  2175. fbimg = MIMEImage(fp10.read())
  2176. fp10.close()
  2177. fbimg.add_header('Content-ID',"<fb-image>")
  2178. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  2179.  
  2180. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  2181. fp11 = open(attachment12,'rb')
  2182. twimg = MIMEImage(fp11.read())
  2183. fp11.close()
  2184. twimg.add_header('Content-ID',"<tw-image>")
  2185. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  2186.  
  2187. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  2188. fp12 = open(attachment13,'rb')
  2189. lknimg = MIMEImage(fp12.read())
  2190. fp12.close()
  2191. lknimg.add_header('Content-ID',"<link-image>")
  2192. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  2193.  
  2194. msgRoot.attach(img)
  2195. msgRoot.attach(topleftwhiteimg)
  2196. msgRoot.attach(toprightwhiteimg)
  2197. msgRoot.attach(topleftblueimg)
  2198. msgRoot.attach(toprightblueimg)
  2199. msgRoot.attach(reportimg)
  2200. msgRoot.attach(bottomleftblueimg)
  2201. msgRoot.attach(bottomrightblueimg)
  2202. msgRoot.attach(bottomleftbrownimg)
  2203. msgRoot.attach(bottomrightbrownimg)
  2204. msgRoot.attach(fbimg)
  2205. msgRoot.attach(twimg)
  2206. msgRoot.attach(lknimg)
  2207.  
  2208. TOADDR = [msgRoot['To']]
  2209. CCADDR = [msgRoot['Bcc']]
  2210.  
  2211. msgRoot.attach(img)
  2212. msgRoot.attach(fbimg)
  2213. msgRoot.attach(twimg)
  2214.  
  2215. TOADDR = [msgRoot['To']]
  2216. CCADDR = [msgRoot['Bcc']]
  2217.  
  2218. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  2219.  
  2220. s.quit()
  2221. logger.info("Successfully send no alert mail to user %s"%str(user_id))
  2222. return True
  2223. except Exception as e:
  2224. logger.error("Error while sending email %s"%str(e))
  2225. return False
  2226. # Send no alert email to user
  2227.  
  2228. #Send initial report email to user
  2229. def sendInitialTwMail(report_id,user_id,twid,twname,email,totalFeedCount,totalAlert,config,mailFlag,uuid):
  2230. if mailFlag == False:
  2231. return True
  2232. logger = logging.getLogger("repnuplogger")
  2233. logger.info("Sending initial summary mail to user %s",user_id)
  2234. host_name = config.get('smtp_settings','host')
  2235. user_name = config.get('smtp_settings','username')
  2236. password = config.get('smtp_settings','password')
  2237. mail_template_path = config.get('smtp_settings','mail_template_path')
  2238. siteurl = config.get('admin_settings','site_url')
  2239. sender = config.get('smtp_settings','sender')
  2240. cc_address = config.get('admin_settings','cc_email')
  2241. #email = 'marcomtl@gmail.com';
  2242. uuid = str(uuid)
  2243. try:
  2244. fo = open(os.path.join(mail_template_path,'initial_tw_report.html'))
  2245. filecontent = fo.read()
  2246. fo.close()
  2247. messagebody = filecontent.replace("site_url",siteurl)
  2248. twname = twname.encode('utf-8')
  2249. messagebody = messagebody.replace("app_user_name",twname)
  2250.  
  2251. messagebody = messagebody.replace("user_total_feed ",str(totalFeedCount))
  2252. messagebody = messagebody.replace("user_total_alerts ",str(totalAlert))
  2253. messagebody = messagebody.replace("user_uuid",uuid)
  2254.  
  2255. msgRoot = MIMEMultipart('related')
  2256. msgRoot['Subject'] = "Your social media initial report for Twitter from RepNup"
  2257. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2258. msgRoot['To'] = email
  2259. msgRoot['Bcc'] = cc_address
  2260. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2261.  
  2262. s = smtplib.SMTP(host_name)
  2263. s.set_debuglevel(0)
  2264. s.ehlo()
  2265. if s.has_extn('STARTTLS'):
  2266. s.starttls()
  2267. s.ehlo()
  2268. s.login(user_name,password)
  2269. html_body = MIMEText(messagebody,'html')
  2270. msg= MIMEMultipart('alternative')
  2271. msgRoot.attach(msg)
  2272. msg.attach(html_body)
  2273.  
  2274. attachment = os.path.join(mail_template_path,'logo.jpg')
  2275. fp = open(attachment,'rb')
  2276. img = MIMEImage(fp.read())
  2277. fp.close()
  2278. img.add_header('Content-ID',"<logo-image>")
  2279. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  2280.  
  2281. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  2282. fp1 = open(attachment2,'rb')
  2283. topleftwhiteimg = MIMEImage(fp1.read())
  2284. fp1.close()
  2285. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  2286. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  2287.  
  2288. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  2289. fp2 = open(attachment3,'rb')
  2290. toprightwhiteimg = MIMEImage(fp2.read())
  2291. fp2.close()
  2292. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  2293. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  2294.  
  2295. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  2296. fp3 = open(attachment4,'rb')
  2297. topleftblueimg = MIMEImage(fp3.read())
  2298. fp3.close()
  2299. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  2300. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  2301.  
  2302. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  2303. fp4 = open(attachment5,'rb')
  2304. toprightblueimg = MIMEImage(fp4.read())
  2305. fp4.close()
  2306. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  2307. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  2308.  
  2309. attachment6 = os.path.join(mail_template_path,'report_in_process.jpg')
  2310. fp5 = open(attachment6,'rb')
  2311. reportimg = MIMEImage(fp5.read())
  2312. fp5.close()
  2313. reportimg.add_header('Content-ID',"<report-image>")
  2314. reportimg.add_header('Content-Disposition', 'inline', filename='report_in_process.jpg')
  2315.  
  2316. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  2317. fp6 = open(attachment7,'rb')
  2318. bottomleftblueimg = MIMEImage(fp6.read())
  2319. fp6.close()
  2320. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  2321. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  2322.  
  2323. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  2324. fp7 = open(attachment8,'rb')
  2325. bottomrightblueimg = MIMEImage(fp7.read())
  2326. fp7.close()
  2327. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  2328. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  2329.  
  2330. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  2331. fp8 = open(attachment9,'rb')
  2332. bottomleftbrownimg = MIMEImage(fp8.read())
  2333. fp8.close()
  2334. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  2335. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  2336.  
  2337. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  2338. fp9 = open(attachment10,'rb')
  2339. bottomrightbrownimg = MIMEImage(fp9.read())
  2340. fp9.close()
  2341. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  2342. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  2343.  
  2344. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  2345. fp10 = open(attachment11,'rb')
  2346. fbimg = MIMEImage(fp10.read())
  2347. fp10.close()
  2348. fbimg.add_header('Content-ID',"<fb-image>")
  2349. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  2350.  
  2351. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  2352. fp11 = open(attachment12,'rb')
  2353. twimg = MIMEImage(fp11.read())
  2354. fp11.close()
  2355. twimg.add_header('Content-ID',"<tw-image>")
  2356. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  2357.  
  2358. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  2359. fp12 = open(attachment13,'rb')
  2360. lknimg = MIMEImage(fp12.read())
  2361. fp12.close()
  2362. lknimg.add_header('Content-ID',"<link-image>")
  2363. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  2364.  
  2365. msgRoot.attach(img)
  2366. msgRoot.attach(topleftwhiteimg)
  2367. msgRoot.attach(toprightwhiteimg)
  2368. msgRoot.attach(topleftblueimg)
  2369. msgRoot.attach(toprightblueimg)
  2370. msgRoot.attach(reportimg)
  2371. msgRoot.attach(bottomleftblueimg)
  2372. msgRoot.attach(bottomrightblueimg)
  2373. msgRoot.attach(bottomleftbrownimg)
  2374. msgRoot.attach(bottomrightbrownimg)
  2375. msgRoot.attach(fbimg)
  2376. msgRoot.attach(twimg)
  2377. msgRoot.attach(lknimg)
  2378.  
  2379. TOADDR = [msgRoot['To']]
  2380. CCADDR = [msgRoot['Bcc']]
  2381.  
  2382. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  2383. s.quit()
  2384. logger.info("Successfully send summary mail to user %s"%str(user_id))
  2385. return True
  2386. except Exception as e:
  2387. logger.error("Error while sending email %s"%str(e))
  2388. return False
  2389.  
  2390. # Send summary email to user
  2391. def sendTwSummaryMail(report_id,user_id,twid,twname,email,totalFeedCount,totalAlert,config,mailFlag,uuid):
  2392. if mailFlag == False:
  2393. return True
  2394. logger = logging.getLogger("repnuplogger")
  2395. logger.info("Sending summary mail to user %s with report id %s"%(user_id,str(report_id)))
  2396. host_name = config.get('smtp_settings','host')
  2397. user_name = config.get('smtp_settings','username')
  2398. password = config.get('smtp_settings','password')
  2399. mail_template_path = config.get('smtp_settings','mail_template_path')
  2400. siteurl = config.get('admin_settings','site_url')
  2401. sender = config.get('smtp_settings','sender')
  2402. cc_address = config.get('admin_settings','cc_email')
  2403. #email = 'margomtl@gmail.com'
  2404. uuid = str(uuid)
  2405. try:
  2406. fo = open(os.path.join(mail_template_path,'summary_tw_report.html'))
  2407. filecontent = fo.read()
  2408. fo.close()
  2409. messagebody = filecontent.replace("site_url",siteurl)
  2410.  
  2411. twname = twname.encode('utf-8')
  2412. messagebody = messagebody.replace("app_user_name",twname)
  2413. messagebody = messagebody.replace("user_total_feed",str(totalFeedCount))
  2414. messagebody = messagebody.replace("user_total_alerts",str(totalAlert))
  2415. messagebody = messagebody.replace("user_uuid",uuid)
  2416.  
  2417. msgRoot = MIMEMultipart('related')
  2418. msgRoot['Subject'] = "Your social media summary report for Twitter from RepNup"
  2419. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2420. msgRoot['To'] = email
  2421. msgRoot['Bcc'] = cc_address
  2422. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2423.  
  2424. s = smtplib.SMTP(host_name)
  2425. s.set_debuglevel(0)
  2426. s.ehlo()
  2427. if s.has_extn('STARTTLS'):
  2428. s.starttls()
  2429. s.ehlo()
  2430. s.login(user_name,password)
  2431. html_body = MIMEText(messagebody,'html')
  2432. msg= MIMEMultipart('alternative')
  2433. msgRoot.attach(msg)
  2434. msg.attach(html_body)
  2435.  
  2436. attachment = os.path.join(mail_template_path,'logo.jpg')
  2437. fp = open(attachment,'rb')
  2438. img = MIMEImage(fp.read())
  2439. fp.close()
  2440. img.add_header('Content-ID',"<logo-image>")
  2441. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  2442.  
  2443. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  2444. fp1 = open(attachment2,'rb')
  2445. topleftwhiteimg = MIMEImage(fp1.read())
  2446. fp1.close()
  2447. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  2448. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  2449.  
  2450. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  2451. fp2 = open(attachment3,'rb')
  2452. toprightwhiteimg = MIMEImage(fp2.read())
  2453. fp2.close()
  2454. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  2455. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  2456.  
  2457. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  2458. fp3 = open(attachment4,'rb')
  2459. topleftblueimg = MIMEImage(fp3.read())
  2460. fp3.close()
  2461. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  2462. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  2463.  
  2464. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  2465. fp4 = open(attachment5,'rb')
  2466. toprightblueimg = MIMEImage(fp4.read())
  2467. fp4.close()
  2468. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  2469. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  2470.  
  2471. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  2472. fp5 = open(attachment6,'rb')
  2473. reportimg = MIMEImage(fp5.read())
  2474. fp5.close()
  2475. reportimg.add_header('Content-ID',"<report-ready-image>")
  2476. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  2477.  
  2478. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  2479. fp6 = open(attachment7,'rb')
  2480. bottomleftblueimg = MIMEImage(fp6.read())
  2481. fp6.close()
  2482. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  2483. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  2484.  
  2485. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  2486. fp7 = open(attachment8,'rb')
  2487. bottomrightblueimg = MIMEImage(fp7.read())
  2488. fp7.close()
  2489. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  2490. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  2491.  
  2492. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  2493. fp8 = open(attachment9,'rb')
  2494. bottomleftbrownimg = MIMEImage(fp8.read())
  2495. fp8.close()
  2496. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  2497. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  2498.  
  2499. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  2500. fp9 = open(attachment10,'rb')
  2501. bottomrightbrownimg = MIMEImage(fp9.read())
  2502. fp9.close()
  2503. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  2504. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  2505.  
  2506. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  2507. fp10 = open(attachment11,'rb')
  2508. fbimg = MIMEImage(fp10.read())
  2509. fp10.close()
  2510. fbimg.add_header('Content-ID',"<fb-image>")
  2511. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  2512.  
  2513. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  2514. fp11 = open(attachment12,'rb')
  2515. twimg = MIMEImage(fp11.read())
  2516. fp11.close()
  2517. twimg.add_header('Content-ID',"<tw-image>")
  2518. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  2519.  
  2520. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  2521. fp12 = open(attachment13,'rb')
  2522. lknimg = MIMEImage(fp12.read())
  2523. fp12.close()
  2524. lknimg.add_header('Content-ID',"<link-image>")
  2525. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  2526.  
  2527. msgRoot.attach(img)
  2528. msgRoot.attach(topleftwhiteimg)
  2529. msgRoot.attach(toprightwhiteimg)
  2530. msgRoot.attach(topleftblueimg)
  2531. msgRoot.attach(toprightblueimg)
  2532. msgRoot.attach(reportimg)
  2533. msgRoot.attach(bottomleftblueimg)
  2534. msgRoot.attach(bottomrightblueimg)
  2535. msgRoot.attach(bottomleftbrownimg)
  2536. msgRoot.attach(bottomrightbrownimg)
  2537. msgRoot.attach(fbimg)
  2538. msgRoot.attach(twimg)
  2539. msgRoot.attach(lknimg)
  2540.  
  2541.  
  2542. TOADDR = [msgRoot['To']]
  2543. CCADDR = [msgRoot['Bcc']]
  2544.  
  2545. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  2546.  
  2547. s.quit()
  2548. logger.info("Successfully send summary mail to user %s"%str(user_id))
  2549. return True
  2550. except Exception as e:
  2551. logger.error("Error while sending email %s"%str(e))
  2552. return False
  2553.  
  2554. # process to send email to admins that user is ready to review
  2555. def tw_notify_admin(type,user_id,db_conn,g_admin_emails,config):
  2556.  
  2557. logger = logging.getLogger("repnuplogger")
  2558. twuser = db_conn.selectOne('tbl_twitter_users',"user_id='"+str(user_id)+"'",'twitterId','screenName','text_status','media_status')
  2559.  
  2560. logger.info("Sending mail to admin to review user for twitter %s"%str(twuser[0]))
  2561. host_name = config.get('smtp_settings','host')
  2562. user_name = config.get('smtp_settings','username')
  2563. password = config.get('smtp_settings','password')
  2564. mail_template_path = config.get('smtp_settings','mail_template_path')
  2565. siteurl = config.get('admin_settings','site_url')
  2566. sender = config.get('smtp_settings','sender')
  2567. cc_address = config.get('admin_settings','cc_email')
  2568.  
  2569. if twuser:
  2570. if str(twuser[2]) =="3":
  2571. try:
  2572. fo = open(os.path.join(mail_template_path,'admin_report_ready.html'))
  2573. filecontent = fo.read()
  2574. fo.close()
  2575. messagebody = filecontent.replace("site_url",siteurl)
  2576. name = twuser[1].encode('utf-8')
  2577. messagebody = messagebody.replace("fb_user_id",str(user_id))
  2578. messagebody = messagebody.replace("fb_user_name",name)
  2579.  
  2580. msgRoot = MIMEMultipart('related')
  2581. if type ==1:
  2582. msgRoot['Subject'] = "Please review text for " + name
  2583. else :
  2584. msgRoot['Subject'] = "Please review images for " + name
  2585.  
  2586.  
  2587. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2588. msgRoot['To'] = g_admin_emails
  2589.  
  2590. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2591.  
  2592. s = smtplib.SMTP(host_name)
  2593. s.set_debuglevel(0)
  2594. s.ehlo()
  2595. if s.has_extn('STARTTLS'):
  2596. s.starttls()
  2597. s.ehlo()
  2598. s.login(user_name,password)
  2599. html_body = MIMEText(messagebody,'html')
  2600. msg= MIMEMultipart('alternative')
  2601. msgRoot.attach(msg)
  2602. msg.attach(html_body)
  2603.  
  2604. attachment = os.path.join(mail_template_path,'rep.png')
  2605. fp = open(attachment,'rb')
  2606. img = MIMEImage(fp.read())
  2607. fp.close()
  2608. img.add_header('Content-ID',"<header-image>")
  2609. img.add_header('Content-Disposition', 'inline', filename='rep.png')
  2610.  
  2611. attachment2 = os.path.join(mail_template_path,'fb.png')
  2612. fp1 = open(attachment2,'rb')
  2613. fbimg = MIMEImage(fp1.read())
  2614. fp1.close()
  2615. fbimg.add_header('Content-ID',"<fb-icon-image>")
  2616. fbimg.add_header('Content-Disposition', 'inline', filename='fb.png')
  2617.  
  2618. attachment3 = os.path.join(mail_template_path,'tw.png')
  2619. fp2 = open(attachment3,'rb')
  2620. twimg = MIMEImage(fp2.read())
  2621. fp2.close()
  2622. twimg.add_header('Content-ID',"<tw-icon-image>")
  2623. twimg.add_header('Content-Disposition', 'inline', filename='tw.png')
  2624.  
  2625. msgRoot.attach(img)
  2626. msgRoot.attach(fbimg)
  2627. msgRoot.attach(twimg)
  2628.  
  2629. TOADDR = [msgRoot['To']]
  2630.  
  2631.  
  2632. s.sendmail(sender,TOADDR,msgRoot.as_string())
  2633. s.quit()
  2634. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  2635. if type ==1:
  2636. db_conn.updateRec('tbl_users',"id='"+str(user_id)+"'",mail_status="admin_review_mail",twitterStatus=4,modified=rectime)
  2637. else:
  2638. db_conn.updateRec('tbl_users',"id='"+str(user_id)+"'",mail_status="admin_review_mail",twitterStatus=8,modified=rectime)
  2639. logger.info("Successfully send mail to admin to review user %s"%str(twuser[0]))
  2640. return True
  2641. except Exception as e:
  2642. logger.error("Error while sending email to admin %s"%str(e))
  2643. return False
  2644. else:
  2645. return False
  2646.  
  2647.  
  2648. def sendNoDetailTwAlertMail(report_id,user_id,twid,twname,email,totalTweetCount,totalTweetMediaCount,config,mailFlag,uuid):
  2649. if mailFlag == False:
  2650. return True
  2651. logger = logging.getLogger("repnuplogger")
  2652. logger.info("Sending no detail alert mail to user %s",user_id)
  2653. host_name = config.get('smtp_settings','host')
  2654.  
  2655. user_name = config.get('smtp_settings','username')
  2656. password = config.get('smtp_settings','password')
  2657. mail_template_path = config.get('smtp_settings','mail_template_path')
  2658. siteurl = config.get('admin_settings','site_url')
  2659. sender = config.get('smtp_settings','sender')
  2660. cc_address = config.get('admin_settings','cc_email')
  2661. uuid = str(uuid)
  2662. #email = 'marcomtl@gmail.com'
  2663. try:
  2664. fo = open(os.path.join(mail_template_path,'no_alert_detail_tw_report.html'))
  2665. filecontent = fo.read()
  2666. fo.close()
  2667. messagebody = filecontent.replace("site_url",siteurl)
  2668. twname = twname.encode('utf-8')
  2669. messagebody = messagebody.replace("app_user_name",twname)
  2670.  
  2671. messagebody = messagebody.replace("total_tweet ",str(totalTweetCount))
  2672. messagebody = messagebody.replace("total_photos ",str(totalTweetMediaCount))
  2673. messagebody = messagebody.replace("user_uuid",uuid)
  2674.  
  2675. msgRoot = MIMEMultipart('related')
  2676. msgRoot['Subject'] = "Your social media report for Twitter from RepNup"
  2677. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2678. msgRoot['To'] = email
  2679. msgRoot['Bcc'] = cc_address
  2680. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2681.  
  2682. s = smtplib.SMTP(host_name)
  2683. s.set_debuglevel(0)
  2684. s.ehlo()
  2685. if s.has_extn('STARTTLS'):
  2686. s.starttls()
  2687. s.ehlo()
  2688. s.login(user_name,password)
  2689. html_body = MIMEText(messagebody,'html')
  2690. msg= MIMEMultipart('alternative')
  2691. msgRoot.attach(msg)
  2692. msg.attach(html_body)
  2693.  
  2694. attachment = os.path.join(mail_template_path,'logo.jpg')
  2695. fp = open(attachment,'rb')
  2696. img = MIMEImage(fp.read())
  2697. fp.close()
  2698. img.add_header('Content-ID',"<logo-image>")
  2699. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  2700.  
  2701. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  2702. fp1 = open(attachment2,'rb')
  2703. topleftwhiteimg = MIMEImage(fp1.read())
  2704. fp1.close()
  2705. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  2706. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  2707.  
  2708. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  2709. fp2 = open(attachment3,'rb')
  2710. toprightwhiteimg = MIMEImage(fp2.read())
  2711. fp2.close()
  2712. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  2713. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  2714.  
  2715. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  2716. fp3 = open(attachment4,'rb')
  2717. topleftblueimg = MIMEImage(fp3.read())
  2718. fp3.close()
  2719. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  2720. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  2721.  
  2722. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  2723. fp4 = open(attachment5,'rb')
  2724. toprightblueimg = MIMEImage(fp4.read())
  2725. fp4.close()
  2726. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  2727. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  2728.  
  2729. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  2730. fp5 = open(attachment6,'rb')
  2731. reportimg = MIMEImage(fp5.read())
  2732. fp5.close()
  2733. reportimg.add_header('Content-ID',"<report-ready-image>")
  2734. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  2735.  
  2736. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  2737. fp6 = open(attachment7,'rb')
  2738. bottomleftblueimg = MIMEImage(fp6.read())
  2739. fp6.close()
  2740. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  2741. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  2742.  
  2743. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  2744. fp7 = open(attachment8,'rb')
  2745. bottomrightblueimg = MIMEImage(fp7.read())
  2746. fp7.close()
  2747. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  2748. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  2749.  
  2750. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  2751. fp8 = open(attachment9,'rb')
  2752. bottomleftbrownimg = MIMEImage(fp8.read())
  2753. fp8.close()
  2754. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  2755. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  2756.  
  2757. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  2758. fp9 = open(attachment10,'rb')
  2759. bottomrightbrownimg = MIMEImage(fp9.read())
  2760. fp9.close()
  2761. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  2762. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  2763.  
  2764. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  2765. fp10 = open(attachment11,'rb')
  2766. fbimg = MIMEImage(fp10.read())
  2767. fp10.close()
  2768. fbimg.add_header('Content-ID',"<fb-image>")
  2769. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  2770.  
  2771. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  2772. fp11 = open(attachment12,'rb')
  2773. twimg = MIMEImage(fp11.read())
  2774. fp11.close()
  2775. twimg.add_header('Content-ID',"<tw-image>")
  2776. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  2777.  
  2778. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  2779. fp12 = open(attachment13,'rb')
  2780. lknimg = MIMEImage(fp12.read())
  2781. fp12.close()
  2782. lknimg.add_header('Content-ID',"<link-image>")
  2783. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  2784.  
  2785. msgRoot.attach(img)
  2786. msgRoot.attach(topleftwhiteimg)
  2787. msgRoot.attach(toprightwhiteimg)
  2788. msgRoot.attach(topleftblueimg)
  2789. msgRoot.attach(toprightblueimg)
  2790. msgRoot.attach(reportimg)
  2791. msgRoot.attach(bottomleftblueimg)
  2792. msgRoot.attach(bottomrightblueimg)
  2793. msgRoot.attach(bottomleftbrownimg)
  2794. msgRoot.attach(bottomrightbrownimg)
  2795. msgRoot.attach(fbimg)
  2796. msgRoot.attach(twimg)
  2797. msgRoot.attach(lknimg)
  2798.  
  2799. TOADDR = [msgRoot['To']]
  2800. CCADDR = [msgRoot['Bcc']]
  2801.  
  2802. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  2803.  
  2804. s.quit()
  2805. logger.info("Successfully send no detailed alert mail to user %s"%str(user_id))
  2806. return True
  2807. except Exception as e:
  2808. logger.error("Error while sending email %s"%str(e))
  2809. return False
  2810.  
  2811. # Send detail report email to user
  2812. def sendDetailTwReportMail(report_id,user_id,twid,twname,email,config,mailFlag,uuid):
  2813. if mailFlag == False:
  2814. return True
  2815. logger = logging.getLogger("repnuplogger")
  2816. logger.info("Sending detail report to user %s",user_id)
  2817. host_name = config.get('smtp_settings','host')
  2818. user_name = config.get('smtp_settings','username')
  2819. password = config.get('smtp_settings','password')
  2820. mail_template_path = config.get('smtp_settings','mail_template_path')
  2821. siteurl = config.get('admin_settings','site_url')
  2822. sender = config.get('smtp_settings','sender')
  2823. cc_address = config.get('admin_settings','cc_email')
  2824. #email = 'marcomtl@gmail.com'
  2825. uuid = str(uuid)
  2826.  
  2827. try:
  2828. fo = open(os.path.join(mail_template_path,'detailed_tw_report.html'))
  2829. filecontent = fo.read()
  2830. fo.close()
  2831. messagebody = filecontent.replace("site_url",siteurl)
  2832. twname = twname.encode('utf-8')
  2833. messagebody = messagebody.replace("app_user_name",twname)
  2834. messagebody = messagebody.replace("user_uuid",uuid)
  2835.  
  2836. msgRoot = MIMEMultipart('related')
  2837. msgRoot['Subject'] = "Your social media detailed report for Twitter from RepNup"
  2838. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2839. msgRoot['To'] = email
  2840. msgRoot['Bcc'] = cc_address
  2841. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2842.  
  2843. s = smtplib.SMTP(host_name)
  2844. s.set_debuglevel(0)
  2845. s.ehlo()
  2846. if s.has_extn('STARTTLS'):
  2847. s.starttls()
  2848. s.ehlo()
  2849. s.login(user_name,password)
  2850. html_body = MIMEText(messagebody,'html')
  2851. msg= MIMEMultipart('alternative')
  2852. msgRoot.attach(msg)
  2853. msg.attach(html_body)
  2854.  
  2855. attachment = os.path.join(mail_template_path,'logo.jpg')
  2856. fp = open(attachment,'rb')
  2857. img = MIMEImage(fp.read())
  2858. fp.close()
  2859. img.add_header('Content-ID',"<logo-image>")
  2860. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  2861.  
  2862. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  2863. fp1 = open(attachment2,'rb')
  2864. topleftwhiteimg = MIMEImage(fp1.read())
  2865. fp1.close()
  2866. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  2867. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  2868.  
  2869. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  2870. fp2 = open(attachment3,'rb')
  2871. toprightwhiteimg = MIMEImage(fp2.read())
  2872. fp2.close()
  2873. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  2874. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  2875.  
  2876. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  2877. fp3 = open(attachment4,'rb')
  2878. topleftblueimg = MIMEImage(fp3.read())
  2879. fp3.close()
  2880. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  2881. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  2882.  
  2883. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  2884. fp4 = open(attachment5,'rb')
  2885. toprightblueimg = MIMEImage(fp4.read())
  2886. fp4.close()
  2887. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  2888. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  2889.  
  2890. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  2891. fp5 = open(attachment6,'rb')
  2892. reportimg = MIMEImage(fp5.read())
  2893. fp5.close()
  2894. reportimg.add_header('Content-ID',"<report-ready-image>")
  2895. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  2896.  
  2897. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  2898. fp6 = open(attachment7,'rb')
  2899. bottomleftblueimg = MIMEImage(fp6.read())
  2900. fp6.close()
  2901. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  2902. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  2903.  
  2904. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  2905. fp7 = open(attachment8,'rb')
  2906. bottomrightblueimg = MIMEImage(fp7.read())
  2907. fp7.close()
  2908. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  2909. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  2910.  
  2911. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  2912. fp8 = open(attachment9,'rb')
  2913. bottomleftbrownimg = MIMEImage(fp8.read())
  2914. fp8.close()
  2915. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  2916. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  2917.  
  2918. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  2919. fp9 = open(attachment10,'rb')
  2920. bottomrightbrownimg = MIMEImage(fp9.read())
  2921. fp9.close()
  2922. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  2923. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  2924.  
  2925. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  2926. fp10 = open(attachment11,'rb')
  2927. fbimg = MIMEImage(fp10.read())
  2928. fp10.close()
  2929. fbimg.add_header('Content-ID',"<fb-image>")
  2930. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  2931.  
  2932. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  2933. fp11 = open(attachment12,'rb')
  2934. twimg = MIMEImage(fp11.read())
  2935. fp11.close()
  2936. twimg.add_header('Content-ID',"<tw-image>")
  2937. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  2938.  
  2939. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  2940. fp12 = open(attachment13,'rb')
  2941. lknimg = MIMEImage(fp12.read())
  2942. fp12.close()
  2943. lknimg.add_header('Content-ID',"<link-image>")
  2944. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  2945.  
  2946. msgRoot.attach(img)
  2947. msgRoot.attach(topleftwhiteimg)
  2948. msgRoot.attach(toprightwhiteimg)
  2949. msgRoot.attach(topleftblueimg)
  2950. msgRoot.attach(toprightblueimg)
  2951. msgRoot.attach(reportimg)
  2952. msgRoot.attach(bottomleftblueimg)
  2953. msgRoot.attach(bottomrightblueimg)
  2954. msgRoot.attach(bottomleftbrownimg)
  2955. msgRoot.attach(bottomrightbrownimg)
  2956. msgRoot.attach(fbimg)
  2957. msgRoot.attach(twimg)
  2958. msgRoot.attach(lknimg)
  2959.  
  2960. TOADDR = [msgRoot['To']]
  2961. CCADDR = [msgRoot['Bcc']]
  2962.  
  2963. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  2964.  
  2965. s.quit()
  2966. logger.info("Successfully send detail report mail to user %s"%str(user_id))
  2967. return True
  2968. except Exception as e:
  2969. logger.error("Error while sending email %s"%str(e))
  2970. return False
  2971.  
  2972. # notify user for upgrade
  2973. def notify_user_to_upgrade(fbuser_name,email,user_id, user_image_path, config):
  2974.  
  2975. logger = logging.getLogger("repnuplogger")
  2976. logger.info("Sending mail to user %s for upgrading account "%str(user_id))
  2977. host_name = config.get('smtp_settings','host')
  2978. user_name = config.get('smtp_settings','username')
  2979. password = config.get('smtp_settings','password')
  2980. mail_template_path = config.get('smtp_settings','mail_template_path')
  2981. siteurl = config.get('admin_settings','site_url')
  2982. sender = config.get('smtp_settings','sender')
  2983.  
  2984. try:
  2985. fo = open(os.path.join(mail_template_path,'upgrade_account.html'))
  2986. filecontent = fo.read()
  2987. fo.close()
  2988. messagebody = filecontent.replace("site_url",siteurl)
  2989. fbuser_name = fbuser_name.encode('utf-8')
  2990. messagebody = messagebody.replace("app_user_name",fbuser_name)
  2991.  
  2992. msgRoot = MIMEMultipart('related')
  2993. msgRoot['Subject'] = "Please upgrade your RepNup account"
  2994. msgRoot['From'] = "RepNup <reports@repnup.com>"
  2995. msgRoot['To'] = email
  2996.  
  2997. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  2998.  
  2999. s = smtplib.SMTP(host_name)
  3000. s.set_debuglevel(0)
  3001. s.ehlo()
  3002. if s.has_extn('STARTTLS'):
  3003. s.starttls()
  3004. s.ehlo()
  3005. s.login(user_name,password)
  3006. html_body = MIMEText(messagebody, 'html')
  3007. msg= MIMEMultipart('alternative')
  3008. msgRoot.attach(msg)
  3009. msg.attach(html_body)
  3010.  
  3011. attachment = os.path.join(mail_template_path,'rep.png')
  3012. fp = open(attachment,'rb')
  3013. img = MIMEImage(fp.read())
  3014. fp.close()
  3015. img.add_header('Content-ID',"<header-image>")
  3016. img.add_header('Content-Disposition', 'inline', filename='rep.png')
  3017.  
  3018. attachment2 = os.path.join(mail_template_path,'fb.png')
  3019. fp1 = open(attachment2,'rb')
  3020. fbimg = MIMEImage(fp1.read())
  3021. fp1.close()
  3022. fbimg.add_header('Content-ID',"<fb-icon-image>")
  3023. fbimg.add_header('Content-Disposition', 'inline', filename='fb.png')
  3024.  
  3025. attachment3 = os.path.join(mail_template_path,'tw.png')
  3026. fp2 = open(attachment3,'rb')
  3027. twimg = MIMEImage(fp2.read())
  3028. fp2.close()
  3029. twimg.add_header('Content-ID',"<tw-icon-image>")
  3030. twimg.add_header('Content-Disposition', 'inline', filename='tw.png')
  3031.  
  3032. fp3 = open(user_image_path,'rv')
  3033. userimg = MIMEImage(fp3.read(), "JPG")
  3034. fp3.close()
  3035. userimg.add_header('Content-ID', "<user-image>")
  3036.  
  3037.  
  3038. msgRoot.attach(img)
  3039. msgRoot.attach(fbimg)
  3040. msgRoot.attach(twimg)
  3041. msgRoot.attach(userimg)
  3042.  
  3043. msgRoot['To'] = email
  3044.  
  3045. TOADDR = [msgRoot['To']]
  3046.  
  3047.  
  3048. s.sendmail(sender,TOADDR,msgRoot.as_string())
  3049. s.quit()
  3050. logger.info("Successfully send summary mail to user %s"%str(user_id))
  3051. return True
  3052. except Exception as e:
  3053. logger.error("Error while sending email %s"%str(e))
  3054. return False
  3055.  
  3056.  
  3057. def notify_upgraded_to_pro_user(fbuser_name, email, user_id, config, uuid):
  3058. logger = logging.getLogger("repnuplogger")
  3059. logger.info("Sending mail to user %s for pro account " % str(user_id))
  3060. host_name = config.get('smtp_settings', 'host')
  3061. user_name = config.get('smtp_settings', 'username')
  3062. password = config.get('smtp_settings', 'password')
  3063. mail_template_path = config.get('smtp_settings', 'mail_template_path')
  3064. siteurl = config.get('admin_settings', 'site_url')
  3065. sender = config.get('smtp_settings', 'sender')
  3066.  
  3067. try:
  3068. fo = open(os.path.join(mail_template_path, 'upgraded_to_pro.html'))
  3069. filecontent = fo.read()
  3070. fo.close()
  3071. messagebody = filecontent.replace("site_url",siteurl)
  3072. fbuser_name = fbuser_name.encode('utf-8')
  3073. messagebody = messagebody.replace("app_user_name", fbuser_name)
  3074. messagebody = messagebody.replace("user_uuid", str(uuid))
  3075.  
  3076. msgRoot = MIMEMultipart('related')
  3077. msgRoot['Subject'] = "Your Rep'nUp account upgraded to pro"
  3078. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3079. msgRoot['To'] = email
  3080.  
  3081. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3082.  
  3083. s = smtplib.SMTP(host_name)
  3084. s.set_debuglevel(0)
  3085. s.ehlo()
  3086. if s.has_extn('STARTTLS'):
  3087. s.starttls()
  3088. s.ehlo()
  3089. s.login(user_name, password)
  3090. html_body = MIMEText(messagebody, 'html')
  3091. msg= MIMEMultipart('alternative')
  3092. msgRoot.attach(msg)
  3093. msg.attach(html_body)
  3094.  
  3095. attachment = os.path.join(mail_template_path,'rep.png')
  3096. fp = open(attachment,'rb')
  3097. img = MIMEImage(fp.read())
  3098. fp.close()
  3099. img.add_header('Content-ID',"<header-image>")
  3100. img.add_header('Content-Disposition', 'inline', filename='rep.png')
  3101.  
  3102. attachment2 = os.path.join(mail_template_path,'fb.png')
  3103. fp1 = open(attachment2,'rb')
  3104. fbimg = MIMEImage(fp1.read())
  3105. fp1.close()
  3106. fbimg.add_header('Content-ID',"<fb-icon-image>")
  3107. fbimg.add_header('Content-Disposition', 'inline', filename='fb.png')
  3108.  
  3109. attachment3 = os.path.join(mail_template_path,'tw.png')
  3110. fp2 = open(attachment3,'rb')
  3111. twimg = MIMEImage(fp2.read())
  3112. fp2.close()
  3113. twimg.add_header('Content-ID',"<tw-icon-image>")
  3114. twimg.add_header('Content-Disposition', 'inline', filename='tw.png')
  3115.  
  3116. msgRoot.attach(img)
  3117. msgRoot.attach(fbimg)
  3118. msgRoot.attach(twimg)
  3119. msgRoot['To'] = email
  3120.  
  3121. TOADDR = [msgRoot['To']]
  3122.  
  3123. s.sendmail(sender, TOADDR, msgRoot.as_string())
  3124. s.quit()
  3125. logger.info("Successfully send upgraded_to_pro mail to user %s"%str(user_id))
  3126. return True
  3127. except Exception as e:
  3128. logger.error("Error while sending email %s"%str(e))
  3129. return False
  3130.  
  3131.  
  3132. # Send no alert email to user
  3133. def sendNoInAlertMail(report_id,user_id,instaid,instaname,email,totalFeed,totalImageCount,config,mailFlag,uuid):
  3134. if mailFlag == False:
  3135. return True
  3136. logger = logging.getLogger("repnuplogger")
  3137. logger.info("Sending no alert email to user %s",user_id)
  3138. host_name = config.get('smtp_settings','host')
  3139. user_name = config.get('smtp_settings','username')
  3140. password = config.get('smtp_settings','password')
  3141. mail_template_path = config.get('smtp_settings','mail_template_path')
  3142. siteurl = config.get('admin_settings','site_url')
  3143. sender = config.get('smtp_settings','sender')
  3144. cc_address = config.get('admin_settings','cc_email')
  3145. #email = 'marcomtl@gmail.com'
  3146. uuid = str(uuid)
  3147. try:
  3148. fo = open(os.path.join(mail_template_path,'no_alert_in_report.html'))
  3149. filecontent = fo.read()
  3150. fo.close()
  3151. messagebody = filecontent.replace("site_url",siteurl)
  3152. instaname = instaname.encode('utf-8')
  3153. messagebody = messagebody.replace("app_user_name",instaname)
  3154. messagebody = messagebody.replace("user_total_feed ",str(totalImageCount))
  3155. messagebody = messagebody.replace("total_media",str(totalImageCount))
  3156. messagebody = messagebody.replace("total_text",str(totalFeed))
  3157. messagebody = messagebody.replace("user_uuid", uuid)
  3158.  
  3159. msgRoot = MIMEMultipart('related')
  3160. msgRoot['Subject'] = "Your social media report for Instagram from RepNup"
  3161. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3162. msgRoot['To'] = email
  3163. msgRoot['Bcc'] = cc_address
  3164. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3165.  
  3166. s = smtplib.SMTP(host_name)
  3167. s.set_debuglevel(0)
  3168. s.ehlo()
  3169. if s.has_extn('STARTTLS'):
  3170. s.starttls()
  3171. s.ehlo()
  3172. s.login(user_name,password)
  3173. html_body = MIMEText(messagebody,'html')
  3174. msg= MIMEMultipart('alternative')
  3175. msgRoot.attach(msg)
  3176. msg.attach(html_body)
  3177.  
  3178. attachment = os.path.join(mail_template_path,'logo.jpg')
  3179. fp = open(attachment,'rb')
  3180. img = MIMEImage(fp.read())
  3181. fp.close()
  3182. img.add_header('Content-ID',"<logo-image>")
  3183. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  3184.  
  3185. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  3186. fp1 = open(attachment2,'rb')
  3187. topleftwhiteimg = MIMEImage(fp1.read())
  3188. fp1.close()
  3189. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  3190. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  3191.  
  3192. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  3193. fp2 = open(attachment3,'rb')
  3194. toprightwhiteimg = MIMEImage(fp2.read())
  3195. fp2.close()
  3196. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  3197. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  3198.  
  3199. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  3200. fp3 = open(attachment4,'rb')
  3201. topleftblueimg = MIMEImage(fp3.read())
  3202. fp3.close()
  3203. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  3204. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  3205.  
  3206. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  3207. fp4 = open(attachment5,'rb')
  3208. toprightblueimg = MIMEImage(fp4.read())
  3209. fp4.close()
  3210. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  3211. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  3212.  
  3213. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  3214. fp5 = open(attachment6,'rb')
  3215. reportimg = MIMEImage(fp5.read())
  3216. fp5.close()
  3217. reportimg.add_header('Content-ID',"<report-ready-image>")
  3218. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  3219.  
  3220. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  3221. fp6 = open(attachment7,'rb')
  3222. bottomleftblueimg = MIMEImage(fp6.read())
  3223. fp6.close()
  3224. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  3225. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  3226.  
  3227. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  3228. fp7 = open(attachment8,'rb')
  3229. bottomrightblueimg = MIMEImage(fp7.read())
  3230. fp7.close()
  3231. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  3232. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  3233.  
  3234. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  3235. fp8 = open(attachment9,'rb')
  3236. bottomleftbrownimg = MIMEImage(fp8.read())
  3237. fp8.close()
  3238. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  3239. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  3240.  
  3241. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  3242. fp9 = open(attachment10,'rb')
  3243. bottomrightbrownimg = MIMEImage(fp9.read())
  3244. fp9.close()
  3245. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  3246. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  3247.  
  3248. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  3249. fp10 = open(attachment11,'rb')
  3250. fbimg = MIMEImage(fp10.read())
  3251. fp10.close()
  3252. fbimg.add_header('Content-ID',"<fb-image>")
  3253. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  3254.  
  3255. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  3256. fp11 = open(attachment12,'rb')
  3257. twimg = MIMEImage(fp11.read())
  3258. fp11.close()
  3259. twimg.add_header('Content-ID',"<tw-image>")
  3260. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  3261.  
  3262. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  3263. fp12 = open(attachment13,'rb')
  3264. lknimg = MIMEImage(fp12.read())
  3265. fp12.close()
  3266. lknimg.add_header('Content-ID',"<link-image>")
  3267. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  3268.  
  3269.  
  3270.  
  3271. msgRoot.attach(img)
  3272. msgRoot.attach(topleftwhiteimg)
  3273. msgRoot.attach(toprightwhiteimg)
  3274. msgRoot.attach(topleftblueimg)
  3275. msgRoot.attach(toprightblueimg)
  3276. msgRoot.attach(reportimg)
  3277. msgRoot.attach(bottomleftblueimg)
  3278. msgRoot.attach(bottomrightblueimg)
  3279. msgRoot.attach(bottomleftbrownimg)
  3280. msgRoot.attach(bottomrightbrownimg)
  3281. msgRoot.attach(fbimg)
  3282. msgRoot.attach(twimg)
  3283. msgRoot.attach(lknimg)
  3284.  
  3285.  
  3286. TOADDR = [msgRoot['To']]
  3287. CCADDR = [msgRoot['Bcc']]
  3288.  
  3289. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  3290.  
  3291. s.quit()
  3292. logger.info("Successfully send no alert mail to user %s"%str(user_id))
  3293. return True
  3294. except Exception as e:
  3295. logger.error("Error while sending email %s"%str(e))
  3296. return False
  3297. # Send no alert email to user
  3298.  
  3299. #Send initial report email to user
  3300. def sendInitialInMail(report_id,user_id,instaname,email,config,mailFlag,uuid):
  3301. if mailFlag == False:
  3302. return True
  3303. logger = logging.getLogger("repnuplogger")
  3304. logger.info("Sending initial summary mail to user %s",user_id)
  3305. host_name = config.get('smtp_settings','host')
  3306. user_name = config.get('smtp_settings','username')
  3307. password = config.get('smtp_settings','password')
  3308. mail_template_path = config.get('smtp_settings','mail_template_path')
  3309. siteurl = config.get('admin_settings','site_url')
  3310. sender = config.get('smtp_settings','sender')
  3311. cc_address = config.get('admin_settings','cc_email')
  3312. #email = 'marcomtl@gmail.com';
  3313. uuid = str(uuid)
  3314. try:
  3315. fo = open(os.path.join(mail_template_path,'initial_in_report.html'))
  3316. filecontent = fo.read()
  3317. fo.close()
  3318. messagebody = filecontent.replace("site_url",siteurl)
  3319. messagebody = messagebody.replace("user_uuid",uuid)
  3320. instaname = instaname.encode('utf-8')
  3321. messagebody = messagebody.replace("app_user_name",instaname)
  3322. msgRoot = MIMEMultipart('related')
  3323. msgRoot['Subject'] = "Your social media initial report for Instagram from RepNup"
  3324. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3325. msgRoot['To'] = email
  3326. msgRoot['Bcc'] = cc_address
  3327. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3328.  
  3329.  
  3330. s = smtplib.SMTP(host_name)
  3331. s.set_debuglevel(0)
  3332. s.ehlo()
  3333. if s.has_extn('STARTTLS'):
  3334. s.starttls()
  3335. s.ehlo()
  3336. s.login(user_name,password)
  3337. html_body = MIMEText(messagebody,'html')
  3338. msg= MIMEMultipart('alternative')
  3339. msgRoot.attach(msg)
  3340. msg.attach(html_body)
  3341.  
  3342. attachment = os.path.join(mail_template_path,'logo.jpg')
  3343. fp = open(attachment,'rb')
  3344. img = MIMEImage(fp.read())
  3345. fp.close()
  3346. img.add_header('Content-ID',"<logo-image>")
  3347. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  3348.  
  3349. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  3350. fp1 = open(attachment2,'rb')
  3351. topleftwhiteimg = MIMEImage(fp1.read())
  3352. fp1.close()
  3353. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  3354. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  3355.  
  3356. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  3357. fp2 = open(attachment3,'rb')
  3358. toprightwhiteimg = MIMEImage(fp2.read())
  3359. fp2.close()
  3360. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  3361. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  3362.  
  3363. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  3364. fp3 = open(attachment4,'rb')
  3365. topleftblueimg = MIMEImage(fp3.read())
  3366. fp3.close()
  3367. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  3368. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  3369.  
  3370. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  3371. fp4 = open(attachment5,'rb')
  3372. toprightblueimg = MIMEImage(fp4.read())
  3373. fp4.close()
  3374. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  3375. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  3376.  
  3377. attachment6 = os.path.join(mail_template_path, 'report_in_process.jpg')
  3378. fp5 = open(attachment6,'rb')
  3379. reportimg = MIMEImage(fp5.read())
  3380. fp5.close()
  3381. reportimg.add_header('Content-ID',"<report-image>")
  3382. reportimg.add_header('Content-Disposition', 'inline', filename='report_in_process.jpg')
  3383.  
  3384. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  3385. fp6 = open(attachment7,'rb')
  3386. bottomleftblueimg = MIMEImage(fp6.read())
  3387. fp6.close()
  3388. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  3389. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  3390.  
  3391. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  3392. fp7 = open(attachment8,'rb')
  3393. bottomrightblueimg = MIMEImage(fp7.read())
  3394. fp7.close()
  3395. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  3396. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  3397.  
  3398. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  3399. fp8 = open(attachment9,'rb')
  3400. bottomleftbrownimg = MIMEImage(fp8.read())
  3401. fp8.close()
  3402. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  3403. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  3404.  
  3405. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  3406. fp9 = open(attachment10,'rb')
  3407. bottomrightbrownimg = MIMEImage(fp9.read())
  3408. fp9.close()
  3409. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  3410. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  3411.  
  3412. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  3413. fp10 = open(attachment11,'rb')
  3414. fbimg = MIMEImage(fp10.read())
  3415. fp10.close()
  3416. fbimg.add_header('Content-ID',"<fb-image>")
  3417. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  3418.  
  3419. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  3420. fp11 = open(attachment12,'rb')
  3421. twimg = MIMEImage(fp11.read())
  3422. fp11.close()
  3423. twimg.add_header('Content-ID',"<tw-image>")
  3424. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  3425.  
  3426. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  3427. fp12 = open(attachment13,'rb')
  3428. lknimg = MIMEImage(fp12.read())
  3429. fp12.close()
  3430. lknimg.add_header('Content-ID',"<link-image>")
  3431. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  3432.  
  3433.  
  3434. msgRoot.attach(img)
  3435. msgRoot.attach(topleftwhiteimg)
  3436. msgRoot.attach(toprightwhiteimg)
  3437. msgRoot.attach(topleftblueimg)
  3438. msgRoot.attach(toprightblueimg)
  3439. msgRoot.attach(reportimg)
  3440. msgRoot.attach(bottomleftblueimg)
  3441. msgRoot.attach(bottomrightblueimg)
  3442. msgRoot.attach(bottomleftbrownimg)
  3443. msgRoot.attach(bottomrightbrownimg)
  3444. msgRoot.attach(fbimg)
  3445. msgRoot.attach(twimg)
  3446. msgRoot.attach(lknimg)
  3447.  
  3448. TOADDR = [msgRoot['To']]
  3449. CCADDR = [msgRoot['Bcc']]
  3450.  
  3451. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  3452. s.quit()
  3453. logger.info("Successfully send summary mail to user %s"%str(user_id))
  3454. return True
  3455. except Exception as e:
  3456. logger.error("Error while sending email %s"%str(e))
  3457. return False
  3458.  
  3459. # Send summary email to user
  3460. def sendInSummaryMail(report_id,user_id,instaname,email,totalFeedCount,totalAlert,config,mailFlag,uuid):
  3461. if mailFlag == False:
  3462. return True
  3463. logger = logging.getLogger("repnuplogger")
  3464. logger.info("Sending summary mail to user %s with report id %s"%(user_id,str(report_id)))
  3465. host_name = config.get('smtp_settings','host')
  3466. user_name = config.get('smtp_settings','username')
  3467. password = config.get('smtp_settings','password')
  3468. mail_template_path = config.get('smtp_settings','mail_template_path')
  3469. siteurl = config.get('admin_settings','site_url')
  3470. sender = config.get('smtp_settings','sender')
  3471. cc_address = config.get('admin_settings','cc_email')
  3472. #email = 'margomtl@gmail.com'
  3473. uuid = str(uuid)
  3474. try:
  3475. fo = open(os.path.join(mail_template_path,'summary_in_report.html'))
  3476. filecontent = fo.read()
  3477. fo.close()
  3478. messagebody = filecontent.replace("site_url",siteurl)
  3479.  
  3480. instaname = instaname.encode('utf-8')
  3481. messagebody = messagebody.replace("app_user_name",instaname)
  3482. messagebody = messagebody.replace("user_total_feed",str(totalFeedCount))
  3483. messagebody = messagebody.replace("user_total_alerts",str(totalAlert))
  3484. messagebody = messagebody.replace("user_uuid",uuid)
  3485.  
  3486. msgRoot = MIMEMultipart('related')
  3487. msgRoot['Subject'] = "Your social media summary report for Instagram from RepNup"
  3488. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3489. msgRoot['To'] = email
  3490. msgRoot['Bcc'] = cc_address
  3491. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3492.  
  3493. s = smtplib.SMTP(host_name)
  3494. s.set_debuglevel(0)
  3495. s.ehlo()
  3496. if s.has_extn('STARTTLS'):
  3497. s.starttls()
  3498. s.ehlo()
  3499. s.login(user_name,password)
  3500. html_body = MIMEText(messagebody,'html')
  3501. msg= MIMEMultipart('alternative')
  3502. msgRoot.attach(msg)
  3503. msg.attach(html_body)
  3504.  
  3505. attachment = os.path.join(mail_template_path,'logo.jpg')
  3506. fp = open(attachment,'rb')
  3507. img = MIMEImage(fp.read())
  3508. fp.close()
  3509. img.add_header('Content-ID',"<logo-image>")
  3510. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  3511.  
  3512. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  3513. fp1 = open(attachment2,'rb')
  3514. topleftwhiteimg = MIMEImage(fp1.read())
  3515. fp1.close()
  3516. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  3517. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  3518.  
  3519. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  3520. fp2 = open(attachment3,'rb')
  3521. toprightwhiteimg = MIMEImage(fp2.read())
  3522. fp2.close()
  3523. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  3524. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  3525.  
  3526. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  3527. fp3 = open(attachment4,'rb')
  3528. topleftblueimg = MIMEImage(fp3.read())
  3529. fp3.close()
  3530. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  3531. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  3532.  
  3533. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  3534. fp4 = open(attachment5,'rb')
  3535. toprightblueimg = MIMEImage(fp4.read())
  3536. fp4.close()
  3537. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  3538. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  3539.  
  3540. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  3541. fp5 = open(attachment6,'rb')
  3542. reportimg = MIMEImage(fp5.read())
  3543. fp5.close()
  3544. reportimg.add_header('Content-ID',"<report-ready-image>")
  3545. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  3546.  
  3547. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  3548. fp6 = open(attachment7,'rb')
  3549. bottomleftblueimg = MIMEImage(fp6.read())
  3550. fp6.close()
  3551. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  3552. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  3553.  
  3554. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  3555. fp7 = open(attachment8,'rb')
  3556. bottomrightblueimg = MIMEImage(fp7.read())
  3557. fp7.close()
  3558. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  3559. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  3560.  
  3561. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  3562. fp8 = open(attachment9,'rb')
  3563. bottomleftbrownimg = MIMEImage(fp8.read())
  3564. fp8.close()
  3565. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  3566. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  3567.  
  3568. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  3569. fp9 = open(attachment10,'rb')
  3570. bottomrightbrownimg = MIMEImage(fp9.read())
  3571. fp9.close()
  3572. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  3573. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  3574.  
  3575. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  3576. fp10 = open(attachment11,'rb')
  3577. fbimg = MIMEImage(fp10.read())
  3578. fp10.close()
  3579. fbimg.add_header('Content-ID',"<fb-image>")
  3580. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  3581.  
  3582. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  3583. fp11 = open(attachment12,'rb')
  3584. twimg = MIMEImage(fp11.read())
  3585. fp11.close()
  3586. twimg.add_header('Content-ID',"<tw-image>")
  3587. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  3588.  
  3589. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  3590. fp12 = open(attachment13,'rb')
  3591. lknimg = MIMEImage(fp12.read())
  3592. fp12.close()
  3593. lknimg.add_header('Content-ID',"<link-image>")
  3594. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  3595.  
  3596.  
  3597. msgRoot.attach(img)
  3598. msgRoot.attach(topleftwhiteimg)
  3599. msgRoot.attach(toprightwhiteimg)
  3600. msgRoot.attach(topleftblueimg)
  3601. msgRoot.attach(toprightblueimg)
  3602. msgRoot.attach(reportimg)
  3603. msgRoot.attach(bottomleftblueimg)
  3604. msgRoot.attach(bottomrightblueimg)
  3605. msgRoot.attach(bottomleftbrownimg)
  3606. msgRoot.attach(bottomrightbrownimg)
  3607. msgRoot.attach(fbimg)
  3608. msgRoot.attach(twimg)
  3609. msgRoot.attach(lknimg)
  3610.  
  3611. TOADDR = [msgRoot['To']]
  3612. CCADDR = [msgRoot['Bcc']]
  3613.  
  3614. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  3615.  
  3616.  
  3617. s.quit()
  3618. logger.info("Successfully send summary mail to user %s"%str(user_id))
  3619. return True
  3620. except Exception as e:
  3621. logger.error("Error while sending email %s"%str(e))
  3622. return False
  3623.  
  3624. # process to send email to admins that instagram user is ready to review
  3625. def in_notify_admin(type,user_id,db_conn,g_admin_emails,config):
  3626.  
  3627. logger = logging.getLogger("repnuplogger")
  3628. inuser = db_conn.selectOne('tbl_instagram_users',"user_id='"+str(user_id)+"'",'instagramId','fullName','text_status','media_status')
  3629.  
  3630. logger.info("Sending mail to admin to review user for instagram %s"%str(inuser[0]))
  3631. host_name = config.get('smtp_settings','host')
  3632. user_name = config.get('smtp_settings','username')
  3633. password = config.get('smtp_settings','password')
  3634. mail_template_path = config.get('smtp_settings','mail_template_path')
  3635. siteurl = config.get('admin_settings','site_url')
  3636. sender = config.get('smtp_settings','sender')
  3637. cc_address = config.get('admin_settings','cc_email')
  3638.  
  3639. if inuser:
  3640. if str(inuser[2]) =="3":
  3641. try:
  3642. fo = open(os.path.join(mail_template_path,'admin_report_ready.html'))
  3643. filecontent = fo.read()
  3644. fo.close()
  3645. messagebody = filecontent.replace("site_url",siteurl)
  3646.  
  3647. name = inuser[1].encode('utf-8')
  3648.  
  3649. messagebody = messagebody.replace("fb_user_id",str(user_id))
  3650. messagebody = messagebody.replace("fb_user_name",name)
  3651.  
  3652. msgRoot = MIMEMultipart('related')
  3653. if type ==1:
  3654. msgRoot['Subject'] = "Please review text for " + name
  3655. else :
  3656. msgRoot['Subject'] = "Please review images for " + name
  3657.  
  3658. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3659. msgRoot['To'] = g_admin_emails
  3660.  
  3661. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3662.  
  3663. s = smtplib.SMTP(host_name)
  3664. s.set_debuglevel(0)
  3665. s.ehlo()
  3666. if s.has_extn('STARTTLS'):
  3667. s.starttls()
  3668. s.ehlo()
  3669. s.login(user_name,password)
  3670. html_body = MIMEText(messagebody,'html')
  3671. msg= MIMEMultipart('alternative')
  3672. msgRoot.attach(msg)
  3673. msg.attach(html_body)
  3674.  
  3675. attachment = os.path.join(mail_template_path,'rep.png')
  3676. fp = open(attachment,'rb')
  3677. img = MIMEImage(fp.read())
  3678. fp.close()
  3679. img.add_header('Content-ID',"<header-image>")
  3680. img.add_header('Content-Disposition', 'inline', filename='rep.png')
  3681.  
  3682. attachment2 = os.path.join(mail_template_path,'fb.png')
  3683. fp1 = open(attachment2,'rb')
  3684. fbimg = MIMEImage(fp1.read())
  3685. fp1.close()
  3686. fbimg.add_header('Content-ID',"<fb-icon-image>")
  3687. fbimg.add_header('Content-Disposition', 'inline', filename='fb.png')
  3688.  
  3689. attachment3 = os.path.join(mail_template_path,'tw.png')
  3690. fp2 = open(attachment3,'rb')
  3691. twimg = MIMEImage(fp2.read())
  3692. fp2.close()
  3693. twimg.add_header('Content-ID',"<tw-icon-image>")
  3694. twimg.add_header('Content-Disposition', 'inline', filename='tw.png')
  3695.  
  3696. msgRoot.attach(img)
  3697.  
  3698.  
  3699. TOADDR = [msgRoot['To']]
  3700.  
  3701.  
  3702. s.sendmail(sender,TOADDR,msgRoot.as_string())
  3703. s.quit()
  3704. rectime = time.strftime('%Y-%m-%d %H:%M:%S')
  3705. if type ==1:
  3706. db_conn.updateRec('tbl_users',"id='"+str(user_id)+"'",mail_status="admin_review_mail",instagramStatus=4,modified=rectime)
  3707. else:
  3708. db_conn.updateRec('tbl_users',"id='"+str(user_id)+"'",mail_status="admin_review_mail",instagramStatus=8,modified=rectime)
  3709. logger.info("Successfully send mail to admin to review user %s"%str(inuser[0]))
  3710. return True
  3711. except Exception as e:
  3712. logger.error("Error while sending email to admin %s"%str(e))
  3713. return False
  3714. else:
  3715. return False
  3716.  
  3717. def sendNoDetailInAlertMail(report_id,user_id,instaname,email,totalTextCount,totalMediaCount,config,mailFlag,uuid):
  3718. if mailFlag == False:
  3719. return True
  3720. logger = logging.getLogger("repnuplogger")
  3721. logger.info("Sending no detail alert mail to user %s", user_id)
  3722. host_name = config.get('smtp_settings', 'host')
  3723.  
  3724. user_name = config.get('smtp_settings', 'username')
  3725. password = config.get('smtp_settings', 'password')
  3726. mail_template_path = config.get('smtp_settings', 'mail_template_path')
  3727. siteurl = config.get('admin_settings', 'site_url')
  3728. sender = config.get('smtp_settings', 'sender')
  3729. cc_address = config.get('admin_settings', 'cc_email')
  3730. #email = 'marcomtl@gmail.com'
  3731. uuid = str(uuid)
  3732. try:
  3733. fo = open(os.path.join(mail_template_path, 'no_alert_detail_in_report.html'))
  3734. filecontent = fo.read()
  3735. fo.close()
  3736. messagebody = filecontent.replace("site_url", siteurl)
  3737. instaname = instaname.encode('utf-8')
  3738. messagebody = messagebody.replace("app_user_name", instaname)
  3739.  
  3740. messagebody = messagebody.replace("total_text", str(totalTextCount))
  3741. messagebody = messagebody.replace("total_media", str(totalMediaCount))
  3742. messagebody = messagebody.replace("user_uuid", uuid)
  3743.  
  3744. msgRoot = MIMEMultipart('related')
  3745. msgRoot['Subject'] = "Your social media report for Instagram from RepNup"
  3746. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3747. msgRoot['To'] = email
  3748. msgRoot['Bcc'] = cc_address
  3749. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3750.  
  3751. s = smtplib.SMTP(host_name)
  3752. s.set_debuglevel(0)
  3753. s.ehlo()
  3754. if s.has_extn('STARTTLS'):
  3755. s.starttls()
  3756. s.ehlo()
  3757. s.login(user_name,password)
  3758. html_body = MIMEText(messagebody,'html')
  3759. msg= MIMEMultipart('alternative')
  3760. msgRoot.attach(msg)
  3761. msg.attach(html_body)
  3762.  
  3763. attachment = os.path.join(mail_template_path,'logo.jpg')
  3764. fp = open(attachment,'rb')
  3765. img = MIMEImage(fp.read())
  3766. fp.close()
  3767. img.add_header('Content-ID',"<logo-image>")
  3768. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  3769.  
  3770. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  3771. fp1 = open(attachment2,'rb')
  3772. topleftwhiteimg = MIMEImage(fp1.read())
  3773. fp1.close()
  3774. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  3775. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  3776.  
  3777. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  3778. fp2 = open(attachment3,'rb')
  3779. toprightwhiteimg = MIMEImage(fp2.read())
  3780. fp2.close()
  3781. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  3782. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  3783.  
  3784. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  3785. fp3 = open(attachment4,'rb')
  3786. topleftblueimg = MIMEImage(fp3.read())
  3787. fp3.close()
  3788. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  3789. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  3790.  
  3791. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  3792. fp4 = open(attachment5,'rb')
  3793. toprightblueimg = MIMEImage(fp4.read())
  3794. fp4.close()
  3795. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  3796. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  3797.  
  3798. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  3799. fp5 = open(attachment6,'rb')
  3800. reportimg = MIMEImage(fp5.read())
  3801. fp5.close()
  3802. reportimg.add_header('Content-ID',"<report-ready-image>")
  3803. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  3804.  
  3805. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  3806. fp6 = open(attachment7,'rb')
  3807. bottomleftblueimg = MIMEImage(fp6.read())
  3808. fp6.close()
  3809. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  3810. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  3811.  
  3812. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  3813. fp7 = open(attachment8,'rb')
  3814. bottomrightblueimg = MIMEImage(fp7.read())
  3815. fp7.close()
  3816. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  3817. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  3818.  
  3819. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  3820. fp8 = open(attachment9,'rb')
  3821. bottomleftbrownimg = MIMEImage(fp8.read())
  3822. fp8.close()
  3823. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  3824. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  3825.  
  3826. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  3827. fp9 = open(attachment10,'rb')
  3828. bottomrightbrownimg = MIMEImage(fp9.read())
  3829. fp9.close()
  3830. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  3831. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  3832.  
  3833. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  3834. fp10 = open(attachment11,'rb')
  3835. fbimg = MIMEImage(fp10.read())
  3836. fp10.close()
  3837. fbimg.add_header('Content-ID',"<fb-image>")
  3838. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  3839.  
  3840. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  3841. fp11 = open(attachment12,'rb')
  3842. twimg = MIMEImage(fp11.read())
  3843. fp11.close()
  3844. twimg.add_header('Content-ID',"<tw-image>")
  3845. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  3846.  
  3847. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  3848. fp12 = open(attachment13,'rb')
  3849. lknimg = MIMEImage(fp12.read())
  3850. fp12.close()
  3851. lknimg.add_header('Content-ID',"<link-image>")
  3852. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  3853.  
  3854. msgRoot.attach(img)
  3855. msgRoot.attach(topleftwhiteimg)
  3856. msgRoot.attach(toprightwhiteimg)
  3857. msgRoot.attach(topleftblueimg)
  3858. msgRoot.attach(toprightblueimg)
  3859. msgRoot.attach(reportimg)
  3860. msgRoot.attach(bottomleftblueimg)
  3861. msgRoot.attach(bottomrightblueimg)
  3862. msgRoot.attach(bottomleftbrownimg)
  3863. msgRoot.attach(bottomrightbrownimg)
  3864. msgRoot.attach(fbimg)
  3865. msgRoot.attach(twimg)
  3866. msgRoot.attach(lknimg)
  3867.  
  3868.  
  3869. TOADDR = [msgRoot['To']]
  3870. CCADDR = [msgRoot['Bcc']]
  3871.  
  3872. msgRoot.attach(img)
  3873. msgRoot.attach(fbimg)
  3874. msgRoot.attach(twimg)
  3875.  
  3876. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  3877.  
  3878. s.quit()
  3879. logger.info("Successfully send no detailed alert mail to user %s"%str(user_id))
  3880. return True
  3881. except Exception as e:
  3882. logger.error("Error while sending email %s"%str(e))
  3883. return False
  3884.  
  3885.  
  3886. # Send detail report email to user
  3887. def sendDetailInReportMail(report_id,user_id,instaname,email,config,mailFlag,uuid):
  3888. if mailFlag == False:
  3889. return True
  3890. logger = logging.getLogger("repnuplogger")
  3891. logger.info("Sending detail report to user %s",user_id)
  3892. host_name = config.get('smtp_settings','host')
  3893. user_name = config.get('smtp_settings','username')
  3894. password = config.get('smtp_settings','password')
  3895. mail_template_path = config.get('smtp_settings','mail_template_path')
  3896. siteurl = config.get('admin_settings','site_url')
  3897. sender = config.get('smtp_settings','sender')
  3898. cc_address = config.get('admin_settings','cc_email')
  3899. #email = 'marcomtl@gmail.com'
  3900. uuid = str(uuid)
  3901. try:
  3902. fo = open(os.path.join(mail_template_path,'detailed_in_report.html'))
  3903. filecontent = fo.read()
  3904. fo.close()
  3905. messagebody = filecontent.replace("site_url",siteurl)
  3906. instaname = instaname.encode('utf-8')
  3907. messagebody = messagebody.replace("app_user_name",instaname)
  3908. messagebody = messagebody.replace("user_uuid",uuid)
  3909.  
  3910. msgRoot = MIMEMultipart('related')
  3911. msgRoot['Subject'] = "Your social media detailed report for Instagram from RepNup"
  3912. msgRoot['From'] = "RepNup <reports@repnup.com>"
  3913. msgRoot['To'] = email
  3914. msgRoot['Bcc'] = cc_address
  3915. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  3916.  
  3917. s = smtplib.SMTP(host_name)
  3918. s.set_debuglevel(0)
  3919. s.ehlo()
  3920. if s.has_extn('STARTTLS'):
  3921. s.starttls()
  3922. s.ehlo()
  3923. s.login(user_name,password)
  3924. html_body = MIMEText(messagebody,'html')
  3925. msg= MIMEMultipart('alternative')
  3926. msgRoot.attach(msg)
  3927. msg.attach(html_body)
  3928.  
  3929. attachment = os.path.join(mail_template_path,'logo.jpg')
  3930. fp = open(attachment,'rb')
  3931. img = MIMEImage(fp.read())
  3932. fp.close()
  3933. img.add_header('Content-ID',"<logo-image>")
  3934. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  3935.  
  3936. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  3937. fp1 = open(attachment2,'rb')
  3938. topleftwhiteimg = MIMEImage(fp1.read())
  3939. fp1.close()
  3940. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  3941. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  3942.  
  3943. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  3944. fp2 = open(attachment3,'rb')
  3945. toprightwhiteimg = MIMEImage(fp2.read())
  3946. fp2.close()
  3947. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  3948. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  3949.  
  3950. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  3951. fp3 = open(attachment4,'rb')
  3952. topleftblueimg = MIMEImage(fp3.read())
  3953. fp3.close()
  3954. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  3955. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  3956.  
  3957. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  3958. fp4 = open(attachment5,'rb')
  3959. toprightblueimg = MIMEImage(fp4.read())
  3960. fp4.close()
  3961. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  3962. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  3963.  
  3964. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  3965. fp5 = open(attachment6,'rb')
  3966. reportimg = MIMEImage(fp5.read())
  3967. fp5.close()
  3968. reportimg.add_header('Content-ID',"<report-ready-image>")
  3969. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  3970.  
  3971. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  3972. fp6 = open(attachment7,'rb')
  3973. bottomleftblueimg = MIMEImage(fp6.read())
  3974. fp6.close()
  3975. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  3976. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  3977.  
  3978. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  3979. fp7 = open(attachment8,'rb')
  3980. bottomrightblueimg = MIMEImage(fp7.read())
  3981. fp7.close()
  3982. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  3983. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  3984.  
  3985. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  3986. fp8 = open(attachment9,'rb')
  3987. bottomleftbrownimg = MIMEImage(fp8.read())
  3988. fp8.close()
  3989. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  3990. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  3991.  
  3992. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  3993. fp9 = open(attachment10,'rb')
  3994. bottomrightbrownimg = MIMEImage(fp9.read())
  3995. fp9.close()
  3996. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  3997. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  3998.  
  3999. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  4000. fp10 = open(attachment11,'rb')
  4001. fbimg = MIMEImage(fp10.read())
  4002. fp10.close()
  4003. fbimg.add_header('Content-ID',"<fb-image>")
  4004. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  4005.  
  4006. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  4007. fp11 = open(attachment12,'rb')
  4008. twimg = MIMEImage(fp11.read())
  4009. fp11.close()
  4010. twimg.add_header('Content-ID',"<tw-image>")
  4011. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  4012.  
  4013. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  4014. fp12 = open(attachment13,'rb')
  4015. lknimg = MIMEImage(fp12.read())
  4016. fp12.close()
  4017. lknimg.add_header('Content-ID',"<link-image>")
  4018. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  4019.  
  4020. msgRoot.attach(img)
  4021. msgRoot.attach(topleftwhiteimg)
  4022. msgRoot.attach(toprightwhiteimg)
  4023. msgRoot.attach(topleftblueimg)
  4024. msgRoot.attach(toprightblueimg)
  4025. msgRoot.attach(reportimg)
  4026. msgRoot.attach(bottomleftblueimg)
  4027. msgRoot.attach(bottomrightblueimg)
  4028. msgRoot.attach(bottomleftbrownimg)
  4029. msgRoot.attach(bottomrightbrownimg)
  4030. msgRoot.attach(fbimg)
  4031. msgRoot.attach(twimg)
  4032. msgRoot.attach(lknimg)
  4033.  
  4034.  
  4035. TOADDR = [msgRoot['To']]
  4036. CCADDR = [msgRoot['Bcc']]
  4037.  
  4038. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  4039.  
  4040. s.quit()
  4041. logger.info("Successfully send detail report mail to user %s"%str(user_id))
  4042. return True
  4043. except Exception as e:
  4044. logger.error("Error while sending email %s"%str(e))
  4045. return False
  4046.  
  4047.  
  4048. def sendContiniousReportMail(report_id, user_id, fbid, fbname, email, totalImageAlert, totalImageCount, config, mailFlag, uuid):
  4049. if mailFlag == False:
  4050. return True
  4051. logger = logging.getLogger("repnuplogger")
  4052. logger.info("Sending detail report to user %s",user_id)
  4053. host_name = config.get('smtp_settings','host')
  4054. user_name = config.get('smtp_settings','username')
  4055. password = config.get('smtp_settings','password')
  4056. mail_template_path = config.get('smtp_settings','mail_template_path')
  4057. siteurl = config.get('admin_settings','site_url')
  4058. sender = config.get('smtp_settings','sender')
  4059. cc_address = config.get('admin_settings','cc_email')
  4060. uuid = str(uuid)
  4061. #email = 'marcomtl@gmail.com'
  4062. if totalImageCount < totalImageAlert:
  4063. totalImageCount = totalImageAlert
  4064. try:
  4065. fo = open(os.path.join(mail_template_path,'detailed_report.html'))
  4066. filecontent = fo.read()
  4067. fo.close()
  4068. messagebody = filecontent.replace("site_url",siteurl)
  4069. fbuser_name = fbname.encode('utf-8')
  4070. messagebody = messagebody.replace("app_user_name",fbuser_name)
  4071. messagebody = messagebody.replace("image_alert_count",str(totalImageAlert))
  4072. messagebody = messagebody.replace("total_image_count",str(totalImageCount))
  4073. messagebody = messagebody.replace("user_uuid",uuid)
  4074.  
  4075. msgRoot = MIMEMultipart('related')
  4076. msgRoot['Subject'] = "Your social media detailed report from RepNup"
  4077. msgRoot['From'] = "RepNup <reports@repnup.com>"
  4078. msgRoot['To'] = email
  4079. msgRoot['Bcc'] = cc_address
  4080. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  4081.  
  4082. s = smtplib.SMTP(host_name)
  4083. s.set_debuglevel(0)
  4084. s.ehlo()
  4085. if s.has_extn('STARTTLS'):
  4086. s.starttls()
  4087. s.ehlo()
  4088. s.login(user_name,password)
  4089. html_body = MIMEText(messagebody,'html')
  4090. msg= MIMEMultipart('alternative')
  4091. msgRoot.attach(msg)
  4092. msg.attach(html_body)
  4093.  
  4094. attachment = os.path.join(mail_template_path,'logo.jpg')
  4095. fp = open(attachment,'rb')
  4096. img = MIMEImage(fp.read())
  4097. fp.close()
  4098. img.add_header('Content-ID',"<logo-image>")
  4099. img.add_header('Content-Disposition', 'inline', filename='logo.jpg')
  4100.  
  4101. attachment2 = os.path.join(mail_template_path,'top-left-white-corner.gif')
  4102. fp1 = open(attachment2,'rb')
  4103. topleftwhiteimg = MIMEImage(fp1.read())
  4104. fp1.close()
  4105. topleftwhiteimg.add_header('Content-ID',"<top-left-white-image>")
  4106. topleftwhiteimg.add_header('Content-Disposition', 'inline', filename='top-left-white-corner.gif')
  4107.  
  4108. attachment3 = os.path.join(mail_template_path,'top-right-white-corner.gif')
  4109. fp2 = open(attachment3,'rb')
  4110. toprightwhiteimg = MIMEImage(fp2.read())
  4111. fp2.close()
  4112. toprightwhiteimg.add_header('Content-ID',"<top-right-white-image>")
  4113. toprightwhiteimg.add_header('Content-Disposition', 'inline', filename='top-right-white-corner.gif')
  4114.  
  4115. attachment4 = os.path.join(mail_template_path,'top-left-blue-corner.gif')
  4116. fp3 = open(attachment4,'rb')
  4117. topleftblueimg = MIMEImage(fp3.read())
  4118. fp3.close()
  4119. topleftblueimg.add_header('Content-ID',"<top-left-blue-image>")
  4120. topleftblueimg.add_header('Content-Disposition', 'inline', filename='top-left-blue-corner.gif')
  4121.  
  4122. attachment5 = os.path.join(mail_template_path,'top-right-blue-corner.gif')
  4123. fp4 = open(attachment5,'rb')
  4124. toprightblueimg = MIMEImage(fp4.read())
  4125. fp4.close()
  4126. toprightblueimg.add_header('Content-ID',"<top-right-blue-image>")
  4127. toprightblueimg.add_header('Content-Disposition', 'inline', filename='top-right-blue-corner.gif')
  4128.  
  4129. attachment6 = os.path.join(mail_template_path,'report_ready.jpg')
  4130. fp5 = open(attachment6,'rb')
  4131. reportimg = MIMEImage(fp5.read())
  4132. fp5.close()
  4133. reportimg.add_header('Content-ID',"<report-ready-image>")
  4134. reportimg.add_header('Content-Disposition', 'inline', filename='report_ready.jpg')
  4135.  
  4136. attachment7 = os.path.join(mail_template_path,'bottom-left-blue-corner.gif')
  4137. fp6 = open(attachment7,'rb')
  4138. bottomleftblueimg = MIMEImage(fp6.read())
  4139. fp6.close()
  4140. bottomleftblueimg.add_header('Content-ID',"<bottom-left-blue-image>")
  4141. bottomleftblueimg.add_header('Content-Disposition', 'inline', filename='bottom-left-blue-corner.gif')
  4142.  
  4143. attachment8 = os.path.join(mail_template_path,'bottom-right-blue-corner.gif')
  4144. fp7 = open(attachment8,'rb')
  4145. bottomrightblueimg = MIMEImage(fp7.read())
  4146. fp7.close()
  4147. bottomrightblueimg.add_header('Content-ID',"<bottom-right-blue-image>")
  4148. bottomrightblueimg.add_header('Content-Disposition', 'inline', filename='bottom-right-blue-corner.gif')
  4149.  
  4150. attachment9 = os.path.join(mail_template_path,'bottom-left-brown-corner.gif')
  4151. fp8 = open(attachment9,'rb')
  4152. bottomleftbrownimg = MIMEImage(fp8.read())
  4153. fp8.close()
  4154. bottomleftbrownimg.add_header('Content-ID',"<bottom-left-brown-image>")
  4155. bottomleftbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-left-brown-corner.gif')
  4156.  
  4157. attachment10 = os.path.join(mail_template_path,'bottom-right-brown-corner.gif')
  4158. fp9 = open(attachment10,'rb')
  4159. bottomrightbrownimg = MIMEImage(fp9.read())
  4160. fp9.close()
  4161. bottomrightbrownimg.add_header('Content-ID',"<bottom-right-brown-image>")
  4162. bottomrightbrownimg.add_header('Content-Disposition', 'inline', filename='bottom-right-brown-corner.gif')
  4163.  
  4164. attachment11 = os.path.join(mail_template_path,'fb.jpg')
  4165. fp10 = open(attachment11,'rb')
  4166. fbimg = MIMEImage(fp10.read())
  4167. fp10.close()
  4168. fbimg.add_header('Content-ID',"<fb-image>")
  4169. fbimg.add_header('Content-Disposition', 'inline', filename='fb.jpg')
  4170.  
  4171. attachment12 = os.path.join(mail_template_path,'tw.jpg')
  4172. fp11 = open(attachment12,'rb')
  4173. twimg = MIMEImage(fp11.read())
  4174. fp11.close()
  4175. twimg.add_header('Content-ID',"<tw-image>")
  4176. twimg.add_header('Content-Disposition', 'inline', filename='tw.jpg')
  4177.  
  4178. attachment13 = os.path.join(mail_template_path,'lnk.jpg')
  4179. fp12 = open(attachment13,'rb')
  4180. lknimg = MIMEImage(fp12.read())
  4181. fp12.close()
  4182. lknimg.add_header('Content-ID',"<link-image>")
  4183. lknimg.add_header('Content-Disposition', 'inline', filename='lnk.jpg')
  4184.  
  4185. msgRoot.attach(img)
  4186. msgRoot.attach(topleftwhiteimg)
  4187. msgRoot.attach(toprightwhiteimg)
  4188. msgRoot.attach(topleftblueimg)
  4189. msgRoot.attach(toprightblueimg)
  4190. msgRoot.attach(reportimg)
  4191. msgRoot.attach(bottomleftblueimg)
  4192. msgRoot.attach(bottomrightblueimg)
  4193. msgRoot.attach(bottomleftbrownimg)
  4194. msgRoot.attach(bottomrightbrownimg)
  4195. msgRoot.attach(fbimg)
  4196. msgRoot.attach(twimg)
  4197. msgRoot.attach(lknimg)
  4198.  
  4199.  
  4200. TOADDR = [msgRoot['To']]
  4201. CCADDR = [msgRoot['Bcc']]
  4202.  
  4203. s.sendmail(sender,TOADDR+CCADDR,msgRoot.as_string())
  4204.  
  4205. s.quit()
  4206. logger.info("Successfully send detail report mail to user %s"%str(user_id))
  4207. return True
  4208. except Exception as e:
  4209. logger.error("Error while sending email %s"%str(e))
  4210. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement