From 0b5bc9fed2bdf257ba57d09e520375405e7aeae0 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Sun, 7 Jun 2020 17:53:11 +0500 Subject: [PATCH] improvments to email verification --- backup/static/backup/backup.js | 11 -- emailMarketing/emailMarketing.py | 65 +++++-- emailMarketing/emailMarketingManager.py | 70 ++++++- emailMarketing/models.py | 9 +- .../static/emailMarketing/emailMarketing.js | 52 +++++ .../emailMarketing/configureVerify.html | 4 +- .../templates/emailMarketing/manageLists.html | 181 ++++++++++++++---- emailMarketing/urls.py | 1 + emailMarketing/views.py | 8 + static/backup/backup.js | 11 -- static/emailMarketing/emailMarketing.js | 135 +++++++++++++ 11 files changed, 465 insertions(+), 82 deletions(-) diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index 59c654f87..e233a8636 100755 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -237,10 +237,8 @@ app.controller('backupWebsiteControl', function ($scope, $http, $timeout) { ///** Backup site ends **/// - ///** Restore site ***// - app.controller('restoreWebsiteControl', function ($scope, $http, $timeout) { $scope.restoreLoading = true; @@ -415,13 +413,10 @@ app.controller('restoreWebsiteControl', function ($scope, $http, $timeout) { }); - //*** Restore site ends here ***/// - ///** Backup Destination ***// - app.controller('backupDestinations', function ($scope, $http, $timeout) { $scope.destinationLoading = true; @@ -666,13 +661,10 @@ app.controller('backupDestinations', function ($scope, $http, $timeout) { }); - //*** Backup destination ***/// - ///** Schedule Backup ***// - app.controller('scheduleBackup', function ($scope, $http, $timeout) { $scope.scheduleBackupLoading = true; @@ -951,10 +943,8 @@ app.controller('scheduleBackup', function ($scope, $http, $timeout) { }); - //*** Schedule Backup ***/// - //*** Remote Backup site ****// app.controller('remoteBackupControl', function ($scope, $http, $timeout) { @@ -1543,7 +1533,6 @@ app.controller('remoteBackupControl', function ($scope, $http, $timeout) { ///** Backup site ends **/// - //*** Remote Backup site ****// app.controller('backupLogsScheduled', function ($scope, $http, $timeout) { diff --git a/emailMarketing/emailMarketing.py b/emailMarketing/emailMarketing.py index 67a59bb19..9a62e25e5 100755 --- a/emailMarketing/emailMarketing.py +++ b/emailMarketing/emailMarketing.py @@ -5,7 +5,8 @@ import time import csv import re import plogical.CyberCPLogFileWriter as logging -from .models import EmailMarketing, EmailLists, EmailsInList, EmailTemplate, EmailJobs, SMTPHosts +from .models import EmailMarketing, EmailLists, EmailsInList, EmailTemplate, EmailJobs, SMTPHosts, ValidationLog +from plogical.backupSchedule import backupSchedule from websiteFunctions.models import Websites import threading as multi import socket, smtplib @@ -130,7 +131,6 @@ class emailMarketing(multi.Thread): verificationList = EmailLists.objects.get(listName=self.extraArgs['listName']) domain = verificationList.owner.domain - if not os.path.exists('/home/cyberpanel/' + domain): os.mkdir('/home/cyberpanel/' + domain) @@ -138,12 +138,13 @@ class emailMarketing(multi.Thread): logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Starting verification job..') counter = 1 + counterGlobal = 0 + allEmailsInList = verificationList.emailsinlist_set.all() configureVerifyPath = '/home/cyberpanel/configureVerify' finalPath = '%s/%s' % (configureVerifyPath, domain) - counterGlobal = 0 import json if os.path.exists(finalPath): @@ -151,33 +152,43 @@ class emailMarketing(multi.Thread): self.currentIP = '' + ValidationLog(owner=verificationList, status=backupSchedule.INFO, message='Starting email verification..').save() + for items in allEmailsInList: if items.verificationStatus != 'Verified': try: email = items.email + self.currentEmail = email domainName = email.split('@')[1] records = DNS.dnslookup(domainName, 'MX') + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='Trying to verify %s ..' % (email)).save() + for mxRecord in records: # Get local server hostname host = socket.gethostname() + ## Only fetching smtp object + if os.path.exists(finalPath): try: - logging.CyberCPLogFileWriter.writeToFile('Checking if delay is enabled for verification..') + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='Checking if delay is enabled for verification..').save() delay = self.delayData['delay'] if delay == 'Enable': - logging.CyberCPLogFileWriter.writeToFile( - 'It seems delay is enabled...') + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='It seems delay is enabled...').save() if counterGlobal == int(self.delayData['delayAfter']): - logging.CyberCPLogFileWriter.writeToFile( - 'Sleeping for %s seconds...' % (self.delayData['delayTime'])) + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='Sleeping for %s seconds...' % (self.delayData['delayTime'])).save() + time.sleep(int(self.delayData['delayTime'])) counterGlobal = 0 self.currentIP = self.findNextIP() - logging.CyberCPLogFileWriter.writeToFile( - 'IP in use: %s.' % (str(self.currentIP))) + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='IP being used for validation until next sleep: %s.' % (str(self.currentIP))).save() if self.currentIP == None: server = smtplib.SMTP() @@ -188,8 +199,9 @@ class emailMarketing(multi.Thread): if self.currentIP == '': self.currentIP = self.findNextIP() - logging.CyberCPLogFileWriter.writeToFile( - 'IP in use: %s.' % (str(self.currentIP))) + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='IP being used for validation until next sleep: %s.' % ( + str(self.currentIP))).save() if self.currentIP == None: server = smtplib.SMTP() @@ -198,16 +210,23 @@ class emailMarketing(multi.Thread): else: logging.CyberCPLogFileWriter.writeToFile( 'Delay not configured..') + + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='Delay not configured..').save() + server = smtplib.SMTP() except BaseException as msg: - logging.CyberCPLogFileWriter.writeToFile( - 'Delay not configured.. Error: %s' % (str(msg))) + + ValidationLog(owner=verificationList, status=backupSchedule.ERROR, + message='Delay not configured. Error message: %s' % (str(msg))).save() + server = smtplib.SMTP() else: - logging.CyberCPLogFileWriter.writeToFile( - 'Delay not configured..') + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='Delay not configured..').save() server = smtplib.SMTP() + ### server.set_debuglevel(0) @@ -220,12 +239,15 @@ class emailMarketing(multi.Thread): # Assume 250 as Success if code == 250: + ValidationLog(owner=verificationList, status=backupSchedule.INFO, + message='Verified %s successfully.' % (email)).save() items.verificationStatus = 'Verified' items.save() break else: + ValidationLog(owner=verificationList, status=backupSchedule.ERROR, + message='Failed to verify %s. Error message %s' % (email, message.decode())).save() items.verificationStatus = 'Verification Failed' - logging.CyberCPLogFileWriter.writeToFile(email + " verification failed with error: " + message.decode()) items.save() logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, str(counter) + ' emails verified so far..') @@ -235,17 +257,24 @@ class emailMarketing(multi.Thread): items.save() counter = counter + 1 logging.CyberCPLogFileWriter.writeToFile(str(msg)) + ValidationLog(owner=verificationList, status=backupSchedule.ERROR, + message='Failed to verify %s. Error message %s' % ( + self.currentEmail , str(msg))).save() counterGlobal = counterGlobal + 1 + verificationList.notVerified = verificationList.emailsinlist_set.filter(verificationStatus='Verification Failed').count() + verificationList.verified = verificationList.emailsinlist_set.filter(verificationStatus='Verified').count() + verificationList.save() + logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, str(counter) + ' emails successfully verified. [200]') except BaseException as msg: verificationList = EmailLists.objects.get(listName=self.extraArgs['listName']) domain = verificationList.owner.domain tempStatusPath = '/home/cyberpanel/' + domain + "/" + self.extraArgs['listName'] logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, str(msg) +'. [404]') - logging.CyberCPLogFileWriter.writeToFile('your error') + logging.CyberCPLogFileWriter.writeToFile(str(msg)) return 0 def startEmailJob(self): diff --git a/emailMarketing/emailMarketingManager.py b/emailMarketing/emailMarketingManager.py index 389f0dfc8..6bef90ef7 100755 --- a/emailMarketing/emailMarketingManager.py +++ b/emailMarketing/emailMarketingManager.py @@ -12,6 +12,7 @@ import smtplib from .models import SMTPHosts, EmailTemplate from loginSystem.models import Administrator from .emACL import emACL +from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging class EmailMarketingManager: @@ -199,6 +200,74 @@ class EmailMarketingManager: except KeyError as msg: return redirect(loadLoginPage) + def fetchVerifyLogs(self): + try: + + userID = self.request.session['userID'] + currentACL = ACLManager.loadedACL(userID) + admin = Administrator.objects.get(pk=userID) + + data = json.loads(self.request.body) + + self.listName = data['listName'] + recordsToShow = int(data['recordsToShow']) + page = int(str(data['page']).strip('\n')) + + emailList = EmailLists.objects.get(listName=self.listName) + + if ACLManager.checkOwnership(emailList.owner.domain, admin, currentACL) == 1: + pass + else: + return ACLManager.loadErrorJson('status', 0) + + logs = emailList.validationlog_set.all() + + from s3Backups.s3Backups import S3Backups + + pagination = S3Backups.getPagination(len(logs), recordsToShow) + endPageNumber, finalPageNumber = S3Backups.recordsPointer(page, recordsToShow) + finalLogs = logs[finalPageNumber:endPageNumber] + + json_data = "[" + checker = 0 + counter = 0 + + from plogical.backupSchedule import backupSchedule + + for log in finalLogs: + if log.status == backupSchedule.INFO: + status = 'INFO' + else: + status = 'ERROR' + + dic = { + 'status': status, "message": log.message + } + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + counter = counter + 1 + + json_data = json_data + ']' + + totalEmail = emailList.emailsinlist_set.all().count() + verified = emailList.verified + notVerified = emailList.notVerified + + data_ret = {'status': 1, 'logs': json_data, 'pagination': pagination, 'totalEmails': totalEmail, 'verified': verified, 'notVerified': notVerified} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + + except BaseException as msg: + data_ret = {'status': 0, 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + def saveConfigureVerify(self): try: @@ -791,7 +860,6 @@ class EmailMarketingManager: final_json = json.dumps(final_dic) return HttpResponse(final_json) - def remove(self, listName, emailAddress): try: eList = EmailLists.objects.get(listName=listName) diff --git a/emailMarketing/models.py b/emailMarketing/models.py index 23acba869..f0b7e991d 100755 --- a/emailMarketing/models.py +++ b/emailMarketing/models.py @@ -14,7 +14,8 @@ class EmailLists(models.Model): owner = models.ForeignKey(Websites, on_delete=models.PROTECT) listName = models.CharField(max_length=50, unique=True) dateCreated = models.CharField(max_length=200) - + verified = models.IntegerField(default=0) + notVerified = models.IntegerField(default=0) class EmailsInList(models.Model): owner = models.ForeignKey(EmailLists, on_delete=models.CASCADE) @@ -31,7 +32,6 @@ class SMTPHosts(models.Model): userName = models.CharField(max_length=50) password = models.CharField(max_length=50) - class EmailTemplate(models.Model): owner = models.ForeignKey(Administrator, on_delete=models.CASCADE) name = models.CharField(unique=True, max_length=100) @@ -49,3 +49,8 @@ class EmailJobs(models.Model): sent = models.IntegerField() failed = models.IntegerField() +class ValidationLog(models.Model): + owner = models.ForeignKey(EmailLists, on_delete=models.CASCADE) + status = models.IntegerField() + message = models.TextField() + diff --git a/emailMarketing/static/emailMarketing/emailMarketing.js b/emailMarketing/static/emailMarketing/emailMarketing.js index 726addba8..69ed07493 100755 --- a/emailMarketing/static/emailMarketing/emailMarketing.js +++ b/emailMarketing/static/emailMarketing/emailMarketing.js @@ -774,6 +774,58 @@ app.controller('manageEmailLists', function ($scope, $http, $timeout) { }; + $scope.currentPageLogs = 1; + $scope.recordsToShowLogs = 10; + + $scope.fetchLogs = function () { + + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + var data = { + listName: $scope.listName, + page: $scope.currentPageLogs, + recordsToShow: $scope.recordsToShowLogs + }; + + url = "/emailMarketing/fetchVerifyLogs"; + + $http.post(url, data, config).then(ListInitialData, cantLoadInitialData); + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + $scope.recordsLogs = JSON.parse(response.data.logs); + $scope.paginationLogs = response.data.pagination; + $scope.totalEmails = response.data.totalEmails; + $scope.verified = response.data.verified; + $scope.notVerified = response.data.notVerified; + } else { + new PNotify({ + title: 'Error!', + text: response.data.error_message, + type: 'error' + }); + } + } + function cantLoadInitialData(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + + + }; + + }); app.controller('manageSMTPHostsCTRL', function ($scope, $http) { diff --git a/emailMarketing/templates/emailMarketing/configureVerify.html b/emailMarketing/templates/emailMarketing/configureVerify.html index 1423293ac..25d4fd002 100755 --- a/emailMarketing/templates/emailMarketing/configureVerify.html +++ b/emailMarketing/templates/emailMarketing/configureVerify.html @@ -64,14 +64,14 @@
- +
- +
diff --git a/emailMarketing/templates/emailMarketing/manageLists.html b/emailMarketing/templates/emailMarketing/manageLists.html index ef03aa58b..7234fcddb 100755 --- a/emailMarketing/templates/emailMarketing/manageLists.html +++ b/emailMarketing/templates/emailMarketing/manageLists.html @@ -37,48 +37,155 @@
-
- - -