def __init__(self, username, password): print("------------------------------------------------------") print("- SIRI CONTROL -") print("- Created by Sanjeet Chatterjee -") print("- Adapted by Pydathon -") print("- Website: https://medium.com/@thesanjeetc -") print("------------------------------------------------------") try: self.last_checked = -1 self.mail = MailBox('imap.gmail.com') self.mail.login(username, password, initial_folder='Notes') # Gets last Note id to stop last command from executing uidlist = [msg.uid for msg in self.mail.fetch(AND(all=True))] subjects = [msg.subject for msg in self.mail.fetch(AND(all=True))] try: self.last_checked = uidlist[-1].split()[-1] except IndexError: pass self.load() self.handle() except imaplib.IMAP4.error: print("Your username and password is incorrect") print("Or IMAP is not enabled.")
def search_for_email_with_criteria(self, mailbox: MailBox, use_debug: bool, init_mail_download: bool = False): if init_mail_download: search_for_mail = mailbox.fetch() with open('/opt/mailer_daemon/config.json', 'r') as f: lines = f.readlines() with open("/opt/mailer_daemon/config.json", "w") as f: for line in lines: if "mailbox_init_download" not in line: f.write(line) else: search_for_mail = mailbox.fetch(AND(date=(date( year=self.current_date.year, month=self.current_date.month, day=self.current_date.day)))) for message in search_for_mail: self.mail_json_loader[message.uid] = { 'headers': message.headers, 'subject': message.subject, 'from': message.from_, 'to': message.to, 'content': message.text, 'date': str(message.date.strftime('%Y-%-m-%d')) } if use_debug: self.logger.info(f"Debug is on, blob file is saved in {self.debug_file}") if path.isfile(self.debug_file): remove(self.debug_file) with open(self.debug_file, 'x') as f: json_file = loads( dumps(self.mail_json_loader, sort_keys=True) .replace('\\n', '') .replace('\\r', '') .replace('\\t', '')) dump(json_file, f)
def read_mail(self): mailbox = MailBox(SMTP_SERVER) mailbox.login(FROM_EMAIL, FROM_PWD) frommail = "" for msg in mailbox.fetch('(RECENT)', 10): frommail = frommail + str(msg.from_) return frommail
def test_connection(self): for test_mailbox_name in test_mailbox_name_set: config = get_test_mailbox_config(test_mailbox_name) mailbox = MailBox(config['host']) self.assertIs(type(mailbox), MailBox) login_result = mailbox.login(config['email'], config['password']) self.assertEqual(login_result[0], 'OK') logout_result = mailbox.logout() self.assertEqual(logout_result[0], 'BYE')
def __init__(self, logger_name, host, user, password, source_mailbox, dest_mailbox, error_mailbox): self.logger = logging.getLogger(logger_name) self.mailbox = MailBox(host) self.mailbox.login(user, password, initial_folder=source_mailbox) self.logger.info("Connected to " + host + " with user " + user + ".") self.source_mailbox = source_mailbox self.dest_mailbox = dest_mailbox self.error_mailbox = error_mailbox
def get_mailbox(server, port, security): if security == MailAccount.IMAP_SECURITY_NONE: mailbox = MailBoxUnencrypted(server, port) elif security == MailAccount.IMAP_SECURITY_STARTTLS: mailbox = MailBox(server, port, starttls=True) elif security == MailAccount.IMAP_SECURITY_SSL: mailbox = MailBox(server, port) else: raise NotImplementedError("Unknown IMAP security") # pragma: nocover return mailbox
def login(): global email Logger.normal("Logging in...") try: # Connect/ Initialize email = MailBox(f'imap.{mail_domain}.com') # Log into email email.login(username, password, email_folder) except Exception as e: Logger.error(f"Failed to log in! Error : {e}")
def get_mailbox(server, port, security): if security == MailAccount.IMAP_SECURITY_NONE: mailbox = MailBoxUnencrypted(server, port) elif security == MailAccount.IMAP_SECURITY_STARTTLS: mailbox = MailBox(server, port, starttls=True) elif security == MailAccount.IMAP_SECURITY_SSL: mailbox = MailBox(server, port) else: raise ValueError("Unknown IMAP security") return mailbox
def authuser(self): try: self.Mb_main = MailBox(host=self.host) self.Mb_main.login(self.user,self.pwd) d = dict() for f in self.Mb_main.folder.list(): d[f['name'].split('/')[-1]] = f['name'] self.Mb_main.folder.set(d[self.folder]) return d except: return False
def __init__(self, host: str, account: str, password: str, folder: str = '/'): """ :param folder: name of folder prepended with /, ie. '/foobar' """ assert '/' in folder print(f"Connecting to {account} at {host}...") self.server = MailBox(host) self.server.login(account, password, initial_folder='INBOX' + folder)
def update(self): # at least return the mail with 'last_send' uid # if there is new mail, there will be more than # one uid query = AND(uid=UidRange(self._get_last_send(), '*')) mails = None try: with MailBox(self.config['host'], port=self.config['imap_port'], starttls=True).login(self.config['imap_user'], self.config['imap_pw'], 'INBOX') as mailbox: mails = list( map( lambda msg: (msg.uid, email.message_from_bytes(msg.obj.as_bytes(), policy=policy.default)), mailbox.fetch(query, mark_seen=False))) except Exception as e: self.logger.error(f'fetch mail failed : {e}') if mails is None: time.sleep(16) return if len(mails) > 1: # ignore the first email which has been sent last time self._forward_email(mails[1:]) # skip failed mails self._set_last_send(mails[-1][0]) else: interval = self.config['update_interval'] print(f'{time.time()}: start count down for {interval} seconds') time.sleep(interval)
def test_connection(self, secured_connection): """ Test the connection to the IMAP server """ try: if secured_connection: self.conn = MailBox(host=self.host, port=self.port) else: self.conn = MailBoxUnencrypted(host=self.host, port=self.port) except (gaierror, SSLError) as e: error = 'IMAP Host ' + self.host + ' on port ' + self.port + ' is unreachable : ' + str( e) print(error) self.send_notif(error, 'de la connexion IMAP') sys.exit() try: self.conn.login(self.login, self.pwd) except IMAP4_SSL.error as err: error = 'Error while trying to login to ' + self.host + ' using ' + self.login + '/' + self.pwd + ' as login/password : '******'de l\'authentification IMAP') sys.exit()
def do_command(): if not settings.FETCH_EMAILS: return HOST = settings.IMAP_HOST USER = settings.IMAP_USER PASSWORD = settings.IMAP_PWD PORT = settings.IMAP_PORT FROM = settings.IMAP_FROM with MailBox(HOST).login(USER, PASSWORD, 'INBOX') as mailbox: for message in mailbox.fetch(AND( seen=False, subject=_('invoices'), ), mark_seen=True): try: usr = User.objects.get(email=message.from_) if not usr.has_perm('accounting.add_csvinvoice'): continue except: continue for att in message.attachments: # list: [Attachment objects] file = SimpleUploadedFile(att.filename, att.payload, att.content_type) instance = CSVInvoice(csv=file) instance.save()
def get_mail_since_yesterday(): with MailBox(config.mail_imap).login(config.mail_user, config.mail_passwd, initial_folder='INBOX') as mailbox: yesterday = (datetime.now().date() - timedelta(days=1)).strftime("%d-%b-%Y") return [msg for msg in mailbox.fetch(f"SINCE {yesterday}")]
def test_connection(self): # simple for test_mailbox_name in TEST_MAILBOX_NAME_SET: config = get_test_mailbox_config(test_mailbox_name) mailbox = MailBox(config['host']) self.assertIs(type(mailbox), MailBox) login_result = mailbox.login(config['email'], config['password']) self.assertEqual(login_result.login_result[0], 'OK') logout_result = mailbox.logout() self.assertEqual(logout_result[0], 'BYE') # with for test_mailbox_name in TEST_MAILBOX_NAME_SET: config = get_test_mailbox_config(test_mailbox_name) with MailBox(config['host']).login(config['email'], config['password']) as mailbox: self.assertIs(type(mailbox), MailBox) self.assertEqual(mailbox.login_result[0], 'OK')
def process(self): """ Try to connect to the IMAP server, and try process all matched emails """ try: with MailBox(self.server).login(self.email, self.password) as remote_mailbox: count = self.count_elements(remote_mailbox) if (count == 0): print("There are'nt emails to be processed", f"with query '{self.query}'") return for an_email in tqdm(remote_mailbox.fetch(self.query), total=count, desc='Processing emails'): try: an_email_str = an_email.obj.as_string().encode('utf-8') self.push_to_group(an_email_str) except Exception as e: self.handle_error(e, an_email.uid, an_email_str) self.show_errors() except Exception as e: print(f"Error while interacting with the IMAP SERVER: '{e}'")
def save_mail(imap_host, imap_user, select_mailbox, imap_pass, subject_to_check, text, sender_email, receiver_email, flagged_email, check_email, exact_date, after_date, before_date, wait): # time.sleep(5) with MailBox(imap_host).login(imap_user, imap_pass, initial_folder=select_mailbox) as mailboxi: clauses = [] def gt(dt): dt = datetime.strptime(dt, '%Y-%m-%d') return dt if subject_to_check: clauses.append(AND(subject=subject_to_check)) if text: clauses.append(AND(text=text)) if sender_email: clauses.append(AND(from_=sender_email)) if check_email: if check_email in ("false", "unseen", "unread", "unchecked", "no"): clauses.append(AND(seen=False)) else: clauses.append(AND(seen=True)) if flagged_email: if flagged_email in ("true", "ok", "yes", "flag", "flagged"): clauses.append(AND(flagged=True)) else: clauses.append(AND(flagged=False)) if receiver_email: clauses.append(AND(to=receiver_email)) if exact_date: f = gt(exact_date) clauses.append(AND(date=datetime.date(f))) if after_date: a = gt(after_date) clauses.append(AND(date_gte=datetime.date(a))) if before_date: b = gt(before_date) clauses.append(AND(date_lt=datetime.date(b))) end = time.time() + wait while True: all_mails = list(mailboxi.fetch(AND(*clauses))) if len(all_mails) > 0 or time.time() > end: break value = [] for msg in all_mails: value.append({ "Sender": msg.from_, "Receiver": msg.to, "Subject": msg.subject, "Date": msg.date, "Text": msg.text, "htmlBody": msg.html }) return value
def check_mail(self): link = "" mailbox = MailBox('imap.zoho.com') mailbox.login('*****@*****.**', 'memyself555', initial_folder='ZMNotification') mails = [msg for msg in mailbox.fetch(Q(text="Give your account the green light"))] for mail in mails: mail_to = mail.to[0] if mail_to == self.EMAIL: content = mail.text link = re.search('<(.*)>',content).group(1) if link == "" : logger.info("didn't receive mail or mail problem") return self.check_mail() return link
def main(): with MailBox(IMAP_SERVER).login(EMAIL_ACCOUNT, EMAIL_PASSWORD, initial_folder='INBOX') as mailbox: for msg in mailbox.fetch(Q('UNSEEN')): #for msg in mailbox.fetch(): mailBody = msg.text create_tmp_files(remove_umlaut(mailBody))
def check_mail(self): logger.info("checking mail of " + self.EMAIL) link = "" mailbox = MailBox(self.IMAP_SERVER) mailbox.login(self.IMAP_USER, self.IMAP_PASS, initial_folder=self.IMAP_FOLDER) mails = [msg for msg in mailbox.fetch(Q(text=self.EMAIL))] for mail in mails: if "Activate" in mail.subject: content = mail.text link = re.search('<(.*)>',content).group(1) if link == "" : logger.info("didn't receive mail or mail problem") return self.check_mail() return link
def __init__(self, settings: Settings): self.settings = settings self.mailbox = MailBox(self.settings.imap_hostname) self.mailbox.login(self.settings.imap_username, self.settings.imap_password) self.spam_filters = [ re.compile(spam_filter) for spam_filter in settings.spam_filters ] self.channels: typing.Dict[str, BaseChannel] = {} for channel_name in settings.channels: channel_class = self.CHANNELS.get(channel_name) if channel_class: self.channels[channel_name] = channel_class(settings) else: raise BadOptionUsage('channel', f'Channel {channel_name} is not defined')
def _get_new_emails(self): new_emails = [] for mailbox_name in self._config.sections(): config = self._config[mailbox_name] with MailBox(config["url"]).login(config["address"], config["password"]) as mailbox: for msg in mailbox.fetch(Q(seen=False), mark_seen=False): new_emails.append(msg) return new_emails
def F_InitEmailConfig(): email = config.email_user password = config.email_pass server = config.email_server try: mailbox = MailBox(server) mailbox.login(email, password, initial_folder='INBOX') F_WriteLog('Configurações de e-mail realizadas com sucesso!') return mailbox except: logging.error('Erro:', exc_info=True) return None
def readMessages(self): with MailBox('imap.gmail.com').login(os.getenv("USER"), os.getenv("PASSWORD")) as mailbox: for msg in mailbox.fetch(AND(seen=False)): print(msg) code = msg.subject.split(":", 1) if code[0].lower() == "matprat": print("matprat") return code[0].lower(), code[1] else: return "no messages found"
def search_email_inbox(search_string, username, password): search_from_date = dt.date.today() - dt.timedelta(days=90) # get list of email subjects from INBOX folder with MailBox('imap.gmail.com').login(username, password) as mailbox: for message in mailbox.fetch(Q( AND(subject=search_string, date_gte=search_from_date)), miss_defect=False, miss_no_uid=False): yield message.subject
def CheckMail(): config = LoadConfig() ConfirmArchive(config) mailbox = MailBox(config['imap_ssl_host'], config['imap_ssl_port']) try: mailbox.login(config['email_address'], config['email_password'], initial_folder='Inbox') except: i=0 while i < config['max_retries']: print("Failed to log in to mailbox to retrieve mail. Retrying...") print("Retry %d of max %d" % (i, config['max_retries'])) sleep(1) try: mailbox.login(config['email_address'], config['email_password'], initial_folder='Inbox') break except: i=i+1 for message in mailbox.fetch(Q(seen=False)): if config['allowed_senders']: sender = message.from_ if sender not in config['allowed_senders']: continue subject = message.subject body = message.text video_url = body.strip() try: with youtube_dl.YoutubeDL(config['youtube-dl_options']) as ydl: ydl.download([video_url]) except Exception as e: print("Reporting error to user via email.") ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') error = ansi_escape.sub('', str(e)) s= smtplib.SMTP(host=config['smtp_host'], port=config['smtp_port']) s.starttls() s.login(config['email_address'], config['email_password']) msg = MIMEMultipart() msg['From']=config['email_address'] msg['To']=sender msg['Subject']="Re: %s" % subject msg.attach(MIMEText(error, 'plain')) s.send_message(msg) del msg s.quit() mailbox.logout()
def store_email_data(self): try: find_email = False login = self.conf['main']['login'] password = self.conf['main']['password'] store_dir = self.conf['other']['tmp_folder'] prefix_email_subject = self.conf['main']['prefix_email_subject'] self.logger.info(f'Login to {login}') # логинимся try: with MailBox('imap.gmail.com').login(login, password) as mailbox: for msg in mailbox.fetch( AND(subject=prefix_email_subject, seen=False)): find_email = True self.logger.info( f'Find email with subjetc contains {prefix_email_subject}' ) self.logger.info(f'Message subject: {msg.subject}') # создаем папку для сохранения файлов if not os.path.exists( os.path.join(os.getcwd(), store_dir)): os.makedirs(os.path.join(os.getcwd(), store_dir)) # сохраняем текст сообщения path = os.path.join(store_dir, 'message_text.txt') self.logger.info(f'Save message body: {path}') with open(path, 'w') as f: f.write(msg.text) # сохраняем вложения self.logger.info( f'Find {len(msg.attachments)} attachments:') for att in msg.attachments: path = os.path.join(store_dir, att.filename) self.logger.info(f'Save attachment: {path}') with open(path, 'wb') as f: f.write(att.payload) return True if not find_email: self.logger.info( f'Not found NEW email with subjetc contains {prefix_email_subject}' ) return False except MailboxLoginError: self.logger.warning('Login fail') return False except: self.logger.exception('store_email_data') return False
def check_mail(self): time.sleep(10) logger.info("checking mail of " + self.OTP_EMAIL) link = "" mailbox = MailBox(self.IMAP_SERVER) mailbox.login(self.IMAP_USER, self.IMAP_PASS, initial_folder=self.IMAP_FOLDER) try : for mail in mailbox.fetch(Q(all=True)) : if mail.to[0] == self.OTP_EMAIL : content = mail.text for code in content.split() : if code.isdigit(): link = code except: pass if link == "" : logger.info("didn't receive mail or mail problem") return self.check_mail() return link
def get_email_from_last(num_days: 1, folders, email_info): Mail = namedtuple('Mail', ['from_', 'date', 'object', 'body']) mails_roundcube = [] with MailBox(email_info.host).login(email_info.login, email_info.password) as mailbox: for folder in folders: mailbox.folder.set(folder) query_result = [Mail(msg.from_, msg.date,msg.subject, msg.text) for msg in mailbox.fetch(Q(date_gte=dt.date.today() - dt.timedelta(days=num_days)))] mails_roundcube = mails_roundcube + query_result return mails_roundcube
def parse_email(imap, u, pw): # get list of email subjects from INBOX folder - equivalent verbose version mailbox = MailBox(imap) mailbox.login( u, pw, initial_folder='INBOX') # or mailbox.folder.set instead 3d arg # for debugging, you can set mark_seen=False # texts = [msg.html for msg in mailbox.fetch(AND(all=True),mark_seen=False)] # sometimes html, sometimes text h = [msg.html for msg in mailbox.fetch(AND(all=True), mark_seen=False)] h += [msg.text for msg in mailbox.fetch(AND(all=True), mark_seen=False)] #print(h) # debugging mailbox.logout() return h