def command(input, output): error_count = 0 try: inbox = mailbox.mbox(input) except IOError: inbox = mailbox.Maildir(input, factory=None) outbox = mailbox.Maildir(output) for msg in inbox: try: mail = encoding.from_message(msg) outbox.add(encoding.to_string(mail)) except encoding.EncodingError, exc: print "ERROR: ", exc error_count += 1
def create_mailbox(self, dirname): directory = "maildir/" + dirname + "/" for folder in ["tmp", "new", "cur"]: if not os.path.exists(directory + folder): os.makedirs(directory + folder) self.mbox = mailbox.Maildir(directory, factory=self.mail_parser.parse, create=True)
def read_mail(mail_dir): mbox = mailbox.Maildir(mail_dir) for message_id, message in mbox.iteritems(): if "*****@*****.**" in message["from"]: content = get_message(message) pr_url = get_pull_request_url(content) if pr_url: add_pr_state(pr_url)
def maildir_to_mbox(src, dst): md = mailbox.Maildir(src) mbox = mailbox.mbox(dst) for key, msg in md.iteritems(): mbox.add(msg) mbox.close() md.close()
def Folders(hidden=False): Root = mailbox.Maildir(MAIL_ROOT, factory=False).list_folders() Root.insert(0, '') ret = [] for F in Root: if ((hidden) and (F in ['admin', 'trash'])): continue ret.append(LGFolder(F)) return ret
def create_maildir(maildir): mbox = mailbox.Maildir(maildir) try: mbox.lock() yield mbox finally: mbox.unlock()
def maildir2mailbox(maildir_path, mbox_path): # type: (Path, Path) -> int if not maildir_path.exists(): error('maildir directory %s does not exist' % maildir_path) return 1 if not ((maildir_path / 'cur').exists() and (maildir_path / 'new').exists()): error( 'Missing `new` and/or `cur` subdirectories in path %s, aborting conversion' % maildir_path) return 1 mboxdir_path = Path('%s.sbd' % mbox_path) if not mboxdir_path.exists(): mboxdir_path.mkdir(parents=True, exist_ok=True) elif mbox_path.is_dir(): error('%s exists but is not a directory!' % mbox_path) return 1 info('%s -> %s' % (maildir_path, mbox_path)) maildir = None # type: Optional[mailbox.Maildir] mbox = None # type: Optional[mailbox.mbox] try: maildir = mailbox.Maildir(str(maildir_path)) mails = len(maildir) if not mails: maildir.close() return 0 if mbox_path.exists(): info('Using existing mbox file and adding the messages to it.') mbox = mailbox.mbox(str(mbox_path)) mbox.lock() # iterate over messages in the maildir and add to the mbox info('Processing %d messages in %s' % (mails, maildir_path)) for i, v in enumerate(maildir.iteritems()): key, msg = v if (i % 100) == 99: info('Progress: msg %d of %d' % (i + 1, mails)) try: mbox.add(msg) except Exception: error('Exception while processing msg with key: %s' % key) #traceback.print_exc() raise finally: # close and unlock if mbox: mbox.close() if maildir: maildir.close() return 0
def get_mail_body(self, user, which, top=None): box = mailbox.Maildir(self._path, create=True) item = box.get(user.to_list[which - 1].mail.file_name) item = item.as_string().splitlines(keepends=False) item.insert(0, '+OK core mail') if top is None: return item else: return item[:top + 1]
def test_nonempty_maildir_new(self): self.createMessage("new") self.mbox = mailbox.Maildir(test_support.TESTFN) self.assertTrue(len(self.mbox) == 1) msg = self.mbox.next() self.assertTrue(msg is not None) msg.fp.close() self.assertTrue(self.mbox.next() is None) self.assertTrue(self.mbox.next() is None)
def test_nonempty_maildir_both(self): self.createMessage("cur") self.createMessage("new") self.mbox = mailbox.Maildir(test_support.TESTFN) self.assert_(len(self.mbox.boxes) == 2) self.assert_msg_exists() self.assert_msg_exists() self.assert_(self.mbox.next() is None) self.assert_(self.mbox.next() is None)
def test_nonempty_maildir_both(self): self.createMessage("cur") self.createMessage("new") self.mbox = mailbox.Maildir(support.TESTFN) self.assertTrue(len(self.mbox) == 2) self.assert_and_close(next(self.mbox)) self.assert_and_close(next(self.mbox)) self.assertTrue(next(self.mbox) is None) self.assertTrue(next(self.mbox) is None)
def test_nonempty_maildir_both(self): self.createMessage("cur") self.createMessage("new") self.mbox = mailbox.Maildir(test_support.TESTFN) self.assert_(len(self.mbox) == 2) self.assert_(self.mbox_has_next()) self.assert_(self.mbox_has_next()) self.assert_(not self.mbox_has_next()) self.assert_(not self.mbox_has_next())
def check_emails(useremail=None): emails = [] mbox = mailbox.Maildir('/home/mailuser/Maildir') for msg in mbox.values(): parsed_sender = parseaddr(msg['From'])[1].split('@')[0] if parsed_sender == useremail: emails.append(parsed_email.Email(msg)) emails.sort(key=lambda x: x.date, reverse=True) return emails
def test_nonempty_maildir_both(self): self.createMessage("cur") self.createMessage("new") self.mbox = mailbox.Maildir(test_support.TESTFN) self.assertTrue(len(self.mbox) == 2) self.assertTrue(self.mbox.next() is not None) self.assertTrue(self.mbox.next() is not None) self.assertTrue(self.mbox.next() is None) self.assertTrue(self.mbox.next() is None)
def email_ids(self, address): """Get list of email IDs for address, sorted by age""" mail_dir = self.mail_dir_for(address) if not os.path.exists(mail_dir): return [] mbox = mailbox.Maildir(mail_dir) return list(sorted(mbox.keys()))
def test_empty_maildir(self): """Test an empty maildir mailbox""" # Test for regression on bug #117490: # Make sure the boxes attribute actually gets set. self.mbox = mailbox.Maildir(test_support.TESTFN) self.assert_(hasattr(self.mbox, "boxes")) self.assert_(len(self.mbox.boxes) == 0) self.assert_(self.mbox.next() is None) self.assert_(self.mbox.next() is None)
def send_mail(args: configargparse.Namespace) -> None: if not (args.maildir / 'cur').exists(): raise FileNotFoundError( f'"{args.maildir}" does not appear to be a valid mail directory.') mail = create_mail(args) destination = mailbox.Maildir(args.maildir) destination.add(mail) destination.flush()
def get_emails(): msgs = [] try: inbox = mailbox.Maildir(INBOX, factory=None, create=False) except mailbox.NoSuchMailboxError: logger.critical("You must run 'offlineimap.py' to sync email.") return msgs pybossa = mailbox.Maildir(PROCESSED_MESSAGES_DIR, factory=None, create=True) problems = mailbox.Maildir(PROBLEMS_MESSAGES_DIR, factory=None, create=True) # If ever mbox is desired... # pybossa_mbox = mailbox.mbox(PROCESSED_MESSAGES_DIR + '_mbox', # factory=None, create=True) # problems_mbox = mailbox.mbox(PROBLEMS_MESSAGES_DIR + '_mbox', # factory=None, create=True) # # Uncomment to use existing messages. # ## inbox = pybossa # Sort by date, but must parse date header to actual time object. date_keys = [] for key, msg in inbox.iteritems(): # http://docs.python.org/2/library/email.util.html date_keys.append((mktime_tz(parsedate_tz(msg['date'])), key)) for date_key in sorted(date_keys, key=lambda x: x[0]): ret = process_msg(date_key[1], inbox, pybossa, problems) if ret: msgs.append(ret) inbox.close() pybossa.close() problems.close() return msgs
def save_local(to=None, ffrom=None, date=None, subject=None, body=None, passphrase=None, Folder='drafts', MK=None): """ MK may be None when the message was Axolotl and disappeared after decrypt """ if not addressbook.gpg.verify_symmetric(passphrase): return {'ok': False, 'extra': 'wrong passphrase'} try: Drafts = mailbox.Maildir(MAIL_ROOT, factory=False).get_folder(Folder) except: create_folder(Folder) Drafts = mailbox.Maildir(MAIL_ROOT, factory=False).get_folder(Folder) Payload = str(addressbook.gpg.symmetric(passphrase=passphrase, msg=body)) Msg = None if ((MK is None) or (folder_from_msg_key(aKey=MK) is None)): Msg = format_email(to=to, ffrom=ffrom, date=date, subject=subject, body=Payload) else: Drafts = folder_from_msg_key(aKey=MK)['mbx'] Msg = format_email(to=to, ffrom=ffrom, subject=subject, body=Payload, msgObject=Drafts.get(MK)) Xtra = None if ((MK is None) or (folder_from_msg_key(aKey=MK) is None)): Xtra = Drafts.add(Msg) else: Drafts[MK] = Msg Xtra = MK return {'ok': True, 'extra': Xtra}
def liberate(user_id, options): """ Get set for liberation, expects User object """ options['user'] = user_id user = get_user_model().objects.get(id=user_id) lib_status = user.liberation tar_type = TAR_TYPES[options.get('compression_type', '0')] rstr = get_random_string(7, string.ascii_letters) username = user.username + rstr username = username.encode("utf-8") basename = "%s_%s_%s_%s" % (time.time(), os.getpid(), rstr, hashlib.sha256(username).hexdigest()[:50]) path = os.path.join(settings.LIBERATION_PATH, basename) tarname = "%s.%s" % (basename, tar_type["ext"]) # Is this safe enough? try: os.mkdir(path, 0o700) except (IOError, OSError) as error: log.info("Couldn't create dir at %s", path) raise liberate.retry(exc=error) try: lib_status.path = tarname lib_status.save() except IntegrityError: os.rmdir(path) raise options["path"] = path options["tarname"] = tarname mail_path = os.path.join(path, 'emails') # make maildir mailbox.Maildir(mail_path, factory=None) inbox_tasks = [liberate_inbox.s(mail_path, inbox.id) for inbox in Inbox.objects.filter(user=user, deleted=False).only('id').iterator()] if len(inbox_tasks) > 0: tasks = chord( inbox_tasks, liberate_collect_emails.s(mail_path, options) ) else: options["noEmails"] = True data = {"results": []} tasks = chain( liberate_fetch_info.s(data, options), liberate_tarball.s(options), liberation_finish.s(options) ) async_result = tasks.apply_async() lib_status.async_result = async_result.id lib_status.save()
def open(self): if self.boxtype == 'maildir': self.box = mailbox.Maildir(self.path, False) self.box.lock() elif self.boxtype == 'mbox': self.box = mailbox.mbox(self.path, create=False) self.box.lock() else: raise Exception('Sorry but amailyzer does not support' + self.boxtype + 'mailbox type.')
def delete_emails(useremail=None): emails = [] mbox = mailbox.Maildir('/home/mailuser/Maildir') for key, msg in mbox.items(): parsed_sender = parseaddr(msg['From'])[1].split('@')[0] if parsed_sender == useremail: try: mbox.discard(key) except KeyError: continue
def _locked(self, mailbox_name): path = os.path.join(self._root, mailbox_name) LOG.debug('locking %s', path) box = mailbox.Maildir(path, create=True) box.lock() try: yield box finally: box.flush() box.close()
def __init__(self, maildir_path=None, queue=None): if maildir_path: self.maildir = mailbox.Maildir(os.path.join(maildir_path, self.inbox), create=True) else: self.maildir = None self.mail_dir = maildir_path self.queue = queue logger.info(f"Created SMTP handler")
def get_icals(folder): res = list() mdir = mailbox.Maildir(folder) for message in mdir: if message.get_content_maintype() == 'multipart': for part in message.walk(): if part.get_content_type() == "text/calendar": res.append(part) return res
def main(argv=None): if argv is None: argv = sys.argv if len(argv) < 4: print argv[0] + " <username> <foldertype> <file> [file...]" sys.exit(1) name = argv[1] foldertype = argv[2] zstore = ZStore(name) if not foldertype in [ "IPF.Appointment", "IPF.Contact", "IPF.StickyNote", "IPF.Task", "IPF.Note" ]: print "foldertype must be one of 'IPF.{Contact,StickyNote,Task,Note,Appointment}'" sys.exit(1) for boxname in argv[3:]: if boxname[-1] == '/': boxname = boxname[:-1] print "[MAIL-IMPORT] Processing " + boxname try: if os.path.isdir(boxname): mbox = mailbox.Maildir(boxname) else: mbox = mailbox.mbox(boxname) except: print "[MAIL-IMPORT] Invalid object" continue if len(mbox) == 0: print "[MAIL-IMPORT] No mails in %s" % (boxname) continue if (os.path.basename(boxname)) != 'INBOX.mbox': zstore.setDestFolder(os.path.basename(boxname.rstrip('.mbox')), foldertype) errors = i = 0 for m in mbox: i += 1 try: if m['from'].startswith('Mail System Internal Data'): continue except: pass try: zstore.addMessage(m) except: errors += 1 continue print "[MAIL-IMPORT] %s imported %d messages with %d import errors, %d mapi errors" % ( boxname, len(mbox), zstore.errors, errors)
def smtp_PSWD(self, arg): if self.canregi == True: # 这里是否存在其他关联bug self.auth[self.username] = arg with open("./auth.json", "w", encoding="UTF-8") as f_dump: json.dump(self.auth, f_dump, ensure_ascii=False) # 同时创建本地邮箱,这里在现代是imap/pop3的工作。 mailbox.Maildir(self.username) self.push('250 OK') else: self.push('599 FAIL')
def liberate_inbox(mail_path, inbox_id): """ Gather email IDs """ inbox = Inbox.objects.get(id=inbox_id, deleted=False) maildir = mailbox.Maildir(mail_path, factory=None) maildir.add_folder(str(inbox)) return { 'folder': str(inbox), 'ids': [email.id for email in Email.objects.filter(inbox=inbox, deleted=False).only('id') .iterator()] }
def maildir_scan(folder): cache_false, cache_true = FILES_PARSE_CACHE.setdefault( folder, (set(), set()), ) md = None files = set() changed_while_scan = False cur_dir = os.path.join(folder, 'cur') if os.path.isdir(cur_dir): files.update(os.listdir(cur_dir)) new_dir = os.path.join(folder, 'new') if os.path.isdir(new_dir): files.update(os.listdir(new_dir)) for key in cache_true.copy(): if key not in files: cache_true.remove(key) for key in cache_false.copy(): if key not in files: cache_false.remove(key) for file_name in files: if len(cache_true) > MAX_MESSAGE_PARSE: break if ':2,' in file_name: key, flags = file_name.split(':2,') else: key, flags = file_name, '' if 'S' in flags: continue if (key not in cache_true) and (key not in cache_false): if FILTER_FUNC is not None: if md is None: md = mailbox.Maildir(folder, factory=None, create=False) try: msg = md[key] except IOError: changed_while_scan = True break except OSError: changed_while_scan = True break if FILTER_FUNC(msg): cache_true.add(file_name) else: cache_false.add(file_name) else: cache_true.add(file_name) if md: md.close() if changed_while_scan: return None else: return len(cache_true)
def process(queue, counter, dry_run, directory, pattern): if verbose: print("Started process %d" % os.getpid()) while not queue.empty(): try: target_dir = queue.get(False) run(mailbox.Maildir(target_dir), counter, dry_run, directory, pattern) except Empty: pass