def unsubscribe(request, list_id, campaign_id): email = request.GET.get("email", "") key = request.GET.get("key", "") if email and key: cmpgn = Campaign.query(Campaign.list_id == list_id, Campaign.campaign_id == campaign_id).get() if cmpgn: if key == emailer_key(email, list_id, campaign_id, cmpgn.salt): if ( Unsubscribe.query( Unsubscribe.email == email.lower(), Unsubscribe.list_id == list_id, Unsubscribe.campaign_id == campaign_id, ).count() == 0 ): unsub = Unsubscribe(email=email.lower(), list_id=list_id, campaign_id=campaign_id) unsub.put() form_data = {"email": email, "list_id": list_id, "campaign_id": campaign_id} form_data.update(settings.REPORT_PAYLOAD) form_data = urllib.urlencode(form_data) result = urlfetch.fetch( url=settings.REPORT_UNSUBSCRIBE_URL, payload=form_data, method=urlfetch.POST, headers={"Content-Type": "application/x-www-form-urlencoded"}, ) rdata = json.loads(result.content) return http.HttpResponseRedirect(rdata["url"]) raise http.Http404
def checkmail(email): while True: regex = r'^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$' if (re.search(regex, email)): return email.lower() else: email = input('Introduce un email valido:')
def validate_email(email: str) -> bool: """ Checks correctness of a username Throws exception if the syntax does not fit, the domain is blacklistet or is already in use :email name: Users email (Case sensitive) :return: true if no error occured """ # check for correct email syntax if not EMAIL_REGEX.match(email): raise ApiException([Error(ErrorCode.INVALID_EMAIL, email)]) # check for blacklisted email domains (we don't like disposable email) with db.connection: cursor = db.connection.cursor() cursor.execute("SELECT lower(domain) FROM email_domain_blacklist") rows = cursor.fetchall() # Get list of blacklisted domains (so we can suffix-match incoming emails # in sublinear time) blacklisted_email_domains = marisa_trie.Trie(map(lambda x: x[0], rows)) domain = email.split("@")[1].lower() if domain in blacklisted_email_domains: raise ApiException([Error(ErrorCode.BLACKLISTED_EMAIL, email)]) # ensue that email adress is unique cursor.execute("SELECT id FROM `login` WHERE LOWER(`email`) = %s", (email.lower(),)) if cursor.fetchone() is not None: raise ApiException([Error(ErrorCode.EMAIL_REGISTERED, email)]) return True
def process_line(line): try: line = line.strip() if line.startswith('#'): pass elif line.startswith('@'): pos, email = line[1:].split('\t', 1) pos = int(pos, 36) while len(self.EMAILS) < pos+1: self.EMAILS.append('') self.EMAILS[pos] = email self.EMAIL_IDS[email.lower()] = pos elif line: words = line.split('\t') if len(words) == 10: # This is an old index file, reorder to match new hotness pos, p, unused, msgid, d, f, s, t, r, c = words ptrs = ','.join(['0'+ptr for ptr in p.split(',')]) line = '\t'.join([pos, ptrs, msgid, d, f, '', s, '', t, r, c]) else: pos, ptrs, msgid = words[:3] pos = int(pos, 36) while len(self.INDEX) < pos+1: self.INDEX.append('') self.INDEX[pos] = line self.MSGIDS[msgid] = pos for msg_ptr in ptrs.split(','): self.PTRS[msg_ptr] = pos except ValueError: pass
def checkMail(email): while True: regex = r'^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$' if(re.search(regex,email)): return email.lower() else: email=input('Please input a valid email:')
def unsubscribe( self, email, REQUEST=None, parent=0 ): # -> none; redirects; depends on self, folder; modifies self, folder, catalog """ Remove email from this page's subscriber list. email may be an email address or CMF username, we try to convert usernames to email addresses as needed. If parent flag is true, remove it from the parent folder's subscriber list instead. """ subscriber = email.lower() if self.isSubscriber(subscriber, parent): sl = self._getSubscribers(parent) for s in sl: if (self.emailAddressFrom(s) == self.emailAddressFrom( subscriber)): BLATHER('unsubscribed', subscriber, 'from', self.id()) sl.remove(s) self._setSubscribers(sl, parent) if not parent: self.index_object() if REQUEST: REQUEST.RESPONSE.redirect( REQUEST.get( 'redirectURL', REQUEST['URL1'] + '/subscribeform?email=' + subscriber))
def check_mail(email): while True: regex = r'^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$' if (re.search(regex, email)): return email.lower() else: email = input('Enter a valid email address: ')
def _add_email(self, email, name=None, eid=None): if eid is None: eid = len(self.EMAILS) self.EMAILS.append('') self.EMAILS[eid] = '%s (%s)' % (email, name or email) self.EMAIL_IDS[email.lower()] = eid # FIXME: This needs to get written out... return eid
def compact_to_list(self, msg_to): eids = [] for email in msg_to: eid = self.EMAIL_IDS.get(email.lower()) if eid is None: eid = self._add_email(email) eids.append(eid) return ','.join([b36(e) for e in set(eids)])
def add(self, email, armored_key): # check key and email _inspector = KeyInspector(armored_key, GPG_TEMP) if _inspector.is_key is False: raise ValueError _email, _localpart, _domain = _inspector.get_address_info() if _email.lower() != email.lower(): raise ValueError self.delete(email) zb32filename = HKPTools.localpart2zbase32(_localpart) with open(os.path.join(self.path, _domain, zb32filename), "wb+") as f: _key = _inspector.gpg.export_keys(email.lower(), armor=False) f.write(_key)
def worker(conf): while not emails_q.empty(): email_pwd = emails_q.get() emails_q.task_done() email = email_pwd.split(":")[0] epass = email_pwd.split(":")[1] proxy = None if conf['use_proxies']: proxy = email_pwd.split(":")[2] + ":" + email_pwd.split(":")[3] verify(email.lower(), epass, proxy, conf)
def get_to(self): to = parseaddr(self.msg['To'])[1] try: email = parseaddr(self.msg['Resent-To'])[1] if email is None or len(email) == 0: email = to except Exception: email = to finally: return email.lower()
def get_to(self): to = parseaddr(self.msg['To'])[1] try: email = parseaddr(self.msg['Resent-To'])[1] if email is None or len(email) == 0: email = to except: email = to finally: return email.lower()
def unsubscribe (request, list_id, campaign_id): email = request.GET.get('email', '') key = request.GET.get('key', '') if email and key: cmpgn = Campaign.query(Campaign.list_id == list_id, Campaign.campaign_id == campaign_id).get() if cmpgn: if key == emailer_key(email, list_id, campaign_id, cmpgn.salt): if Unsubscribe.query(Unsubscribe.email == email.lower(), Unsubscribe.list_id == list_id, Unsubscribe.campaign_id == campaign_id).count() == 0: unsub = Unsubscribe(email=email.lower(), list_id=list_id, campaign_id=campaign_id) unsub.put() form_data = {'email': email, 'list_id': list_id, 'campaign_id': campaign_id} form_data.update(settings.REPORT_PAYLOAD) form_data = urllib.urlencode(form_data) result = urlfetch.fetch(url=settings.REPORT_UNSUBSCRIBE_URL, payload=form_data, method=urlfetch.POST, headers={'Content-Type': 'application/x-www-form-urlencoded'}) rdata = json.loads(result.content) return http.HttpResponseRedirect(rdata['url']) raise http.Http404
def _get_public_keys_from(self, from_user=None): self.debug("_GPG._get_public_keys") if from_user == None: self.parent._GPGkeys = list() keys = self.parent._GPGkeys keyhome = self._keyhome.replace("%user", self._recipient) else: self._localGPGkeys = list() self._local_from_user = from_user self._local_gpg_dir = os.path.join(self._keyhome, clean_filename(from_user)) keys = self._localGPGkeys keyhome = self._local_gpg_dir if not os.path.exists(keyhome): os.makedirs(keyhome) os.chmod(keyhome, 0o700) self.debug("_GPG.public_keys homedirectory '%s' created" % keyhome) cmd = "%s --homedir %s --list-keys --with-colons" % (self.parent._GPGCMD, keyhome) self.debug("_GPG.public_keys command: '%s'" % cmd) try: p = subprocess.Popen(cmd.split(" "), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() for line in p.stdout.readlines(): res = line.decode(self.parent._encoding, unicodeerror).split(":") if res[0] == "pub" or res[0] == "uid": email = res[9] mail_id = res[4] try: found = re.search("[-a-zA-Z0-9_%\+\.]+@[-_0-9a-zA-Z\.]+\.[-_0-9a-zA-Z\.]+", email) except: self.log_traceback() if found != None: try: email = email[found.start() : found.end()] except: self.log("splitting email address (%s) " "didn't work" % email, "w") email = "" email = email.lower() if len(email) > 0 and keys.count(email) == 0: keys.append(email) except: self.log("Error opening keyring (Perhaps wrong " "directory '%s'?)" % keyhome, "e") self.log_traceback()
def check_address(address, valides: Union[str, List[str]]) -> bool: if isinstance(address, str): address = [address] for a in address: _, email = parseaddr(a) email = email.lower() # We only need to check if it looks like a email not if it's a valid address if not re.match(r"[^@]+@[^@]+\.[^@]+", email): continue if email in valides: return True return False
def extract_names(txt): txt = clean(txt) contacts = [] for block in txt.strip(' ,').split(','): res = contacts_prog.search(block) if res: name = res.group('realname') email = res.group('email') if email: email = email.lower() contacts.append((name, email)) return contacts
def _get_private_keys(self): self.debug("_GPG._get_private_keys") self.parent._GPGprivatekeys = list() cmd = '%s --homedir %s --list-secret-keys --with-colons' % ( self.parent._GPGCMD, self._keyhome.replace("%user", self._recipient)) self.debug("_GPG.private_keys command: '%s'" % cmd) try: p = subprocess.Popen(cmd.split(' '), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() for line in p.stdout.readlines(): res = line.decode(self.parent._encoding, unicodeerror).split(":") if res[0] == "pub" or res[0] == "uid": email = res[9] try: found = re.search( "[-a-zA-Z0-9_%\+\.]+@[-_0-9a-zA-Z\.]+" "\.[-_0-9a-zA-Z\.]+", email) except: self.log_traceback() if found != None: try: email = email[found.start():found.end()] except: self.log( "splitting email address (%s) " "didn't work" % email, "w") email = "" email = email.lower() if (len(email) > 0 and self.parent._GPGprivatekeys.count(email) == 0): self.parent._GPGprivatekeys.append(email) except: self.log( "Error opening keyring (Perhaps wrong " "directory '%s'?)" % self._keyhome, "e") self.log_traceback()
def extract_names(self, txt): txt = self.clean(txt) emails = set() contacts = [] for block in txt.strip(' ,').split(','): res = Email.contacts_prog.search(block) if res: name = res.group('realname') or '' email = res.group('email') if email: email = email.lower() if email in emails: continue emails.add(email) contacts.append((name.lower(), email)) return contacts
def find_emails(self): emails = [] ts = [ self.headers().raw() ] if self.content_type() in ['text/plain', 'text/html']: ts.append(self.body_text()) rx = re.compile(r"(?i)(\b" + parsemail.re.RXS_email + r"\b)") for t in ts: for part in rx.split(t): if parsemail.re.RX_email.fullmatch(part): m = re.fullmatch('<(.+)>', part) email = m.group(1) if m else part if email.lower() not in (e.lower() for e in emails): emails.append(email) return emails
def _user_key(self, email): """Returns the GPG key for the given email address""" logging.info("Trying to encrypt for %s", email) # Explicit matching of email and uid.email necessary. # Otherwise gpg.keylist will return a list of keys # for searches like "n" for key in self.gpg.keylist(email): for uid in key.uids: if uid.email.lower() == email.lower(): return key.subkeys[0].keyid # Strip sub addressing tag submail = re.sub(r'(\+[^@]+)', '', email) if submail != email: return self._user_key(submail) return None
def check_blacklist (self, email: str, company_id: Optional[int] = None) -> None: if not self.blacklists: self.setup (company_id = company_id) if self.blacklists: email = email.lower () company_ids = [0] if company_id is not None and company_id > 0: company_ids.append (company_id) for cid in company_ids: if cid in self.blacklists: where = 'global' if cid == 0 else 'local' blacklist = self.blacklists[cid] if email in blacklist.plain: raise error ('matches plain in %s blacklist' % where) for (wildcard, pattern) in blacklist.wildcards: if pattern.match (email) is not None: raise error ('matches wildcard %s in %s blacklist' % (wildcard, where)) for custom_blacklist in self.custom_blacklists: if email in custom_blacklist: raise error ('matches %s blacklist' % custom_blacklist.name)
def _get_private_keys(self): self.debug("_GPG._get_private_keys") self.parent._GPGprivatekeys = list() cmd = "%s --homedir %s --list-secret-keys --with-colons" % ( self.parent._GPGCMD, self._keyhome.replace("%user", self._recipient), ) self.debug("_GPG.private_keys command: '%s'" % cmd) try: p = subprocess.Popen(cmd.split(" "), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() for line in p.stdout.readlines(): res = line.decode(self.parent._encoding, unicodeerror).split(":") if res[0] == "pub" or res[0] == "uid": email = res[9] mail_id = res[4] try: found = re.search("[-a-zA-Z0-9_%\+\.]+@[-_0-9a-zA-Z\.]+" "\.[-_0-9a-zA-Z\.]+", email) except: self.log_traceback() if found != None: try: email = email[found.start() : found.end()] except: self.log("splitting email address (%s) " "didn't work" % email, "w") email = "" email = email.lower() if len(email) > 0 and self.parent._GPGprivatekeys.count(email) == 0: self.parent._GPGprivatekeys.append(email) except: self.log("Error opening keyring (Perhaps wrong " "directory '%s'?)" % self._keyhome, "e") self.log_traceback()
def emailAddressFrom(self, subscriber): # -> string; depends on cmf/plone site """ Convert a zwiki subscriber list entry to an email address. A zwiki subscriber list entry can be: an email address, or a CMF member id (if we are in a CMF/Plone site), or either of those with ':edits' appended. We figure out the bare email address and return it (lower-cased), or if we can't, return None. """ if not subscriber or type(subscriber) != StringType: return None subscriber = re.sub(r':edits$', '', subscriber) if isEmailAddress(subscriber): email = subscriber elif self.inCMF(): #and not self.portal_membership.isAnonymousUser() # don't look up member email addresses if user is anonymous ? # No I think it's better to minimise confusion due to # authenticated vs. unauthenticated (un)subscriptions, even if # it allows an anonymous visitor to unsubscribe a member whose # address they know from Products.CMFCore.utils import getToolByName membership = getToolByName(self, 'portal_membership') memberdata = getToolByName(self, 'portal_memberdata') member = membership.getMemberById(subscriber) if not member: # also check for a pseudo-member (a user acquired from above) # NB doesn't work with CMFMember if safe_hasattr(memberdata, '_members'): member = memberdata._members.get(subscriber, None) # dumb robust fix for http://zwiki.org/1400 try: email = member.getProperty('email', getattr(member, 'email', '')) except AttributeError: email = getattr(member, 'email', '') else: email = '' return email.lower() or None
def string_transformation(email): """ Input: Email file (as txt) file path Output: list of words in the email Extracts words from an email file filtering all the characters such as digits punctuation and space """ first_stage=email.lower() second_stage=first_stage.strip() third_stage=second_stage.split() for word in third_stage: word=''.join(char for char in word if char.isalpha()) filters=0 while filters<10: for word in third_stage: word_index=third_stage.index(word) for char in word: if char in "1234567890.!@?$_-()[]{},+│/*=:;<>\"": new_word=word.replace(char,"") third_stage[word_index]=new_word filters+=1 return third_stage
def train(): """Gets the data from the train set, counts how many spam and ham occurences and gets top 5000 words""" num_spam=0 num_ham=0 spam_words=() ham_words=() pullData = open("labels", "r").read() dataArray= pullData.split('\n') #print(dataArray) dataArrayTrain=dataArray[0:21300] #opens training set from folder 000-070 for eachLine in dataArrayTrain: kind,file = eachLine.split(' ') file=file.strip('../') #print(kind) #print(file) fileDir = os.path.dirname(os.path.realpath('__file__')) filepath = os.path.join(fileDir,file) print(filepath) email="" fh = open(filepath, encoding="ascii", errors="ignore") for line in fh: email += line fh.close() email= email.lower() #print(email) email_words = processText(contentEmail(email)) #print(email_words) email_words = tuple(list(set(email_words))) #converted it into a set to avoid repetition of words in every email #print(email_words) if (kind == "spam"): num_spam+=1 #counts how many spam emails spam_words= spam_words + tuple(email_words) #adds every word to a spam tuple elif (kind=="ham"): num_ham+=1 #counts how many ham emails ham_words= ham_words + tuple(email_words) #adds every word to a ham tuple spam_words= tuple(spam_words) ham_words= tuple(ham_words) count_spam = collections.Counter(spam_words) #counts how many times a words appears in all spam emails count_ham = collections.Counter(ham_words) #counts how many times a words appears in all ham emails total_count = (count_spam + count_ham).most_common(5000) #adds the total occurences of the words and gets top 5000 #print(total_count) #print(num_ham, num_spam) top_words = [] for everyword in total_count: top_words.append(everyword[0]) for everyword in list(count_spam): if everyword not in top_words: del count_spam[everyword] #deletes words in spam emails not included in top 5000 for everyword in list(count_ham): if everyword not in top_words: del count_ham[everyword] #deletes words in ham emails not included in top 5000 #print(words, count_ham, count_spam) file_encoder = open("top_word_count.txt", "w+", encoding = 'utf-8', errors = 'ignore') file_encoder.write("HERE ARE YOUR TOP 5000 WORDS: "+"\n"+str(total_count)+"\n"+"\n"+"SPAM WORDS: "+"\n"+str(count_spam)+"\n"+"\n"+"HAM WORDS: "+"\n"+str(count_ham)) file_encoder.close() print("Counting and getting top 5000 words successful!") probabilityGet(num_spam, num_ham, count_spam, count_ham)
def toLowerCase(self, email): return email.lower()
def get_imapConfig(email): try: hoster = email.lower().split('@')[1] return ImapConfig[hoster] except BaseException: return False
def get_email_header(self, header): email = parseaddr(self.msg[header])[1] if len(email) == 0: return None return email.lower()
def update_email(self, email, name=None): eid = self.EMAIL_IDS.get(email.lower()) return self._add_email(email, name=name, eid=eid)
def _add_email(self, email): eid = len(self.EMAILS) self.EMAILS.append(email) self.EMAIL_IDS[email.lower()] = eid # FIXME: This needs to get written out... return eid
def addEmail(self, email): self.emails.add(email.lower())
def testingPhase(SP, HP): """Gets data/words from testing set and applies Bayes Theorem using the values from Laplacian/Lambda Smoothing""" classification= {} TP, TN, FP, FN = 0,0,0,0 pullData = open("labels", "r").read() dataArray= pullData.split('\n') dataArrayTest=dataArray[21301:-1] #opens files from folder 070 onwards for eachLine in dataArrayTest: kind,file = eachLine.split(' ') print(file,kind) if (kind == "spam"): SO = 1 #initially stating that it is a spam not a ham HO = 0 elif (kind== "ham"): HO = 1 SO = 0 file=file.strip('../') fileDir = os.path.dirname(os.path.realpath('__file__')) filepath = os.path.join(fileDir,file) email="" fh = open(filepath, encoding="ascii", errors="ignore") for line in fh: email += line fh.close() email= email.lower() email_words = processText(contentEmail(email)) email_words = tuple(email_words) spam_ba= math.log(PS,10) #initially contains value of Spam Probability ham_ba= math.log(PH, 10) #initially contains value of Ham Probability """BAYES THEOREM""" for word, value in SP.items(): if word in email_words: x = math.log(value, 10) spam_ba += x else: x = math.log(1-value, 10) #print(x) spam_ba += x if ham_ba > spam_ba: label="ham" elif ham_ba < spam_ba: label="spam" for word,value in HP.items(): if word in email_words: x = math.log(value, 10) #print(x) ham_ba += x else: x = math.log(1-value, 10) #print(x) ham_ba += x if ham_ba > spam_ba: label="ham" elif ham_ba < spam_ba: label="spam" print("Spam Prob: " ,spam_ba, "Ham Prob: " ,ham_ba) #This part determines if the emails are ham or spam depending on the calculations if HO == 1 and label == "ham": TN +=1 if HO == 1 and label == "spam": FP +=1 if SO == 1 and label == "spam": TP +=1 if SO == 1 and label == "ham": FN +=1 #print(classification) print(TP, TN, FP, FN) print(spam_ba) print(ham_ba) """COMPUTES PRECISION AND RECALL""" Precision = TP/(TP+FP) Recall = TP/(TP+FN) print("Precision: ", Precision, " ", "Recall: ", Recall)
def _get_public_keys_from(self, from_user=None): self.debug("_GPG._get_public_keys") if from_user != None and not (maildomain(from_user) in self.parent._HOMEDOMAINS): return if from_user == None: self.parent._GPGkeys = list() keys = self.parent._GPGkeys keyhome = self._keyhome.replace("%user", self._recipient) else: self._localGPGkeys = list() self._local_from_user = from_user self._local_gpg_dir = os.path.join(self._keyhome, clean_filename(from_user)) keys = self._localGPGkeys keyhome = self._local_gpg_dir if not os.path.exists(keyhome): os.makedirs(keyhome) os.chmod(keyhome, 0o700) self.debug("_GPG.public_keys homedirectory '%s' created" % keyhome) cmd = '%s --homedir %s --list-keys --with-colons' % ( self.parent._GPGCMD, keyhome) self.debug("_GPG.public_keys command: '%s'" % cmd) try: p = subprocess.Popen(cmd.split(' '), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() for line in p.stdout.readlines(): res = line.decode(self.parent._encoding, unicodeerror).split(":") if (res[0] == "pub" or res[0] == "uid"): email = res[9] try: found = re.search( "[-a-zA-Z0-9_%\+\.]+@[-_0-9a-zA-Z\.]+\.[-_0-9a-zA-Z\.]+", email) except: self.log_traceback() if found != None: try: email = email[found.start():found.end()] except: self.log( "splitting email address (%s) " "didn't work" % email, "w") email = "" email = email.lower() if (len(email) > 0 and keys.count(email) == 0): keys.append(email) except: self.log( "Error opening keyring (Perhaps wrong " "directory '%s'?)" % keyhome, "e") self.log_traceback()
def is_admin(cls, sender): """Return True if sender is an admin, otherwise return False.""" name, mail = email.Utils.parseaddr(sender) return mail.lower() in (email.lower() for email in cls.admins)
def get_imapConfig(email): try: hoster = email.lower().split('@')[1] return ImapConfig[hoster] except: return False
process_mailbox(M) M.close() M.logout() # Processing the raw input. toProduce = messages[len(messages) - 1] parsed = toProduce.split(" ") email = "" i = 1 for i in range(len(parsed)): if (i < len(parsed) - 1): email = email + parsed[i] + " " else: email = email + parsed[i] i += 1 # Parsing the email: Step 1 email_To_Split = email.lower() emailuser = parsed[0] splittedList = email_To_Split.split(" ") splittedList.pop(0) splittedList2 = splittedList # Input/output tables for emails using if/then statements. if ("file" in splittedList and "send" in splittedList): sendEmail(emailuser, "Will do soon.\nSincerely, Jacob Myers", "File") elif ("hi" in splittedList and "jacob!" in splittedList): sendEmail(emailuser, "Hi!\nSincerely, Jacob Myers", "Hi!") elif ("how's" in splittedList and "going?" in splittedList and "it" in splittedList): sendEmail(emailuser, "I'm fine, thanks.\nSincerely, Jacob Myers", "Hi!") elif ("you" in splittedList and "ok?" in splittedList): sendEmail(emailuser, "I'm fine, thanks.\nSincerely, Jacob Myers", "Hi") elif ("i" in splittedList and "miss" in splittedList):