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 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 __connect_to_mailbox(self, mail_user: str, mail_passwd: str, mailbox_host: str, mailbox_port: int, mailbox_use_ssl: bool) -> MailBox: if mailbox_use_ssl: mailbox = MailBox(host=mailbox_host, port=mailbox_port, ssl_context=self.ssl_context) else: mailbox = MailBox(host=mailbox_host, port=mailbox_port) mailbox.login(username=mail_user, password=mail_passwd) try: if status := mailbox.login_result[0] == "OK": self.logger.info(f"Successfully connect to mailbox: {status}") return mailbox except Exception as e: self.logger.warning(f"There was problem with connecting to mailbox: {e}")
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
def find_attendance_messages(server, mail, password): mailbox = MailBox(server) mailbox.login(mail, password, initial_folder='inbox') f = open('attendance_messages.txt.', 'w') counter = 0 for message in mailbox.fetch(Q(text='Attendance', date=date.today())): f.write(message.text) counter += 1 f.write( '=====================================================================================\n' ) f.write(f'\n{counter} URLS were generated.') f.close() mailbox.logout()
def test_mailbox_conn(email_addr: str, password: str) -> bool: try: logger.info("Connecting to mailbox..") mail_provider = get_imap_svr(email_addr) mailbox = MailBox(mail_provider) mailbox.login(email_addr, password) conn_status = True logger.info("Connection successful") mailbox.logout() except imaplib.IMAP4.error: conn_status = False logger.error("IMAP4 Error: Connection failed!") except gaierror: conn_status = False logger.error("GAIError: Connection failed") return conn_status
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 fetchBox(self, folder=None, Query=None): '''Get list of emails from Mailbox server Folder-> Specific folder to extract emails Query-> Filter Emails from a Query :return List of mail objects''' if folder is None: folder = 'INBOX' if Query is None: Query = Q(all=True) mailbox = MailBox(self.imap_server, self.imap_port) mailbox.login(self.username, self.password, initial_folder=folder) message_list = [] for mail in mailbox.fetch(Query): message_list.append(mail) mailbox.logout() return message_list
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
class EmailServer: 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 __enter__(self): return self def __exit__(self, type, value, traceback): self.logout() def logout(self): self.server.logout() def fetch_emails_headers(self) -> List[Email]: """ :return: all emails headers on server """ results: List[Email] = [] for msg in self.server.fetch(criteria='ALL', headers_only=True): email = Email(int(msg.uid), msg) results.append(email) return results def fetch_emails_bodies(self, email_uids: List[EmailID]) -> List[Email]: """ :return: full emails from the email_uids list """ if len(email_uids) == 0: return [] results: List[Email] = [] uids = 'UID ' + ",".join(map(str, email_uids)) for msg in self.server.fetch(criteria=uids): email = Email(int(msg.uid), msg) results.append(email) return results
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 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 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 fetch_mails(): mailbox = MailBox(settings.inbox_url) mailbox.login(settings.inbox, settings.inbox_pass) msgs = mailbox.fetch(AND(seen=False)) feeds = [] for msg in msgs: feed = { 'title': msg.subject, "description": msg.html, 'author': msg.from_, 'pub_date': msg.date, 'guid': msg.uid, 'to': msg.headers['envelope-to'][0] } feeds.append(feed) logger.info(f"New message retrieved for {msg.headers['envelope-to']}") if len(feeds) == 0: logger.info("No new messages.") mailbox.logout() return feeds
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 fetchBox(self, folder=None, Query=None): if folder is None: folder = 'INBOX' if Query is None: Query = Q(all=True) server = 'imap.' + self.username.split("@")[1] mailbox = MailBox(server) mailbox.login( self.username, self.password, initial_folder=folder) # or mailbox.folder.set instead 3d arg message_list = [] for mail in mailbox.fetch(Query): message_list.append(mail) return message_list mailbox.logout()
class IMAPClient: CHANNELS = {'telegram': TelegramChannel} 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 update(self): for msg in self.mailbox.fetch(A(seen=False), limit=7): logger.info('Found new message with ID: `%s`, from: `%s`', msg.uid, msg.from_) logger.debug(' ... subject: `%s`, date: `%s`', msg.subject, msg.date_str) for spam_filter in self.spam_filters: if spam_filter.match(msg.from_) or spam_filter.match( msg.subject): logger.info(' ... skipping based on the filter rule: "%s"', spam_filter.pattern) break else: for _, channel in self.channels.items(): channel.message(msg)
class EmailConnector: 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_messages(self): return list(self.mailbox.fetch()) def move_message(self, message, error): if error: self.mailbox.move(message.uid, self.error_mailbox) self.logger.info("Moved email with UID " + str(message.uid) + " to " + self.error_mailbox + ".") else: self.mailbox.move(message.uid, self.dest_mailbox) self.logger.info("Moved email with UID " + str(message.uid) + " to " + self.dest_mailbox + ".")
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) mails = [msg for msg in mailbox.fetch(Q(text=self.OTP_EMAIL))] for mail in mails: if "Proton Verification Code" in mail.subject: content = mail.text for code in content.split(): if code.isdigit(): link = code # link = re.search('<(.*)>',content).group(1) if link == "": logger.info("didn't receive mail or mail problem") return self.check_mail() return link
def get_new_emails(self): ''' Checks for new email. If there is new email, it will return the tuple (sender, subject)''' # I suppose one email will have been received, otherwise just pick the last email received try: mailbox = MailBox(self._email_server) mailbox.login(self._email_address, self._email_password, initial_folder='INBOX') received_emails = False subject = None sender = None for msg in mailbox.fetch(Q(seen=False)): received_emails = True subject = msg.subject.lower() sender = msg.from_ #print(subject) mailbox.logout() except Exception: print("Some error occured while checking for new emails...") return (None, None) if received_emails: return (sender, subject) else: return (None, None)
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')
from imap_tools import MailBox from account import * mailbox = MailBox("imap.gmail.com", 993) mailbox.login(EMAIL_ADDRESS, EMAIL_PASSWORD, initial_folder="INBOX") # limit : 최대 메일 갯수 # reverse : True 일 경우 최근 메일부터, False 일 경우 과거 메일부터 for msg in mailbox.fetch(limit=1, reverse=True): print("제목", msg.subject) print("발신자", msg.from_) print("수신자", msg.to) #print("참조자", msg.cc) #print("비밀참조자", msg.bcc) print("날짜", msg.date) print("본문", msg.text) print("HTML 메시지", msg.html) print("=" * 100) # 첨부 파일 없으면 list len[] 0 값 print(msg.attachments) # 첨부 파일 for att in msg.attachments: print("첨부파일 이름", att.filename) print("타입", att.content_type) print("크기", att.size) # 파일 다운로드 with open("download_" + att.filename, "wb") as f: f.write(att.payload) print("첨부 파일 {} 다운로드 완료".format(att.filename))
from imap_tools import MailBox import configparser # get config data config = configparser.ConfigParser() config.read('../credentials.ini') # get email box mailbox = MailBox(config['YANDEX']['host']) mailbox.login(config['YANDEX']['email'], config['YANDEX']['password']) # MESSAGES # -------- for message in mailbox.fetch(): print(message.id) print(message.uid) print(message.subject) print(message.from_) print(message.to) print(message.date) print(message.text) print(message.html) print(message.flags) for filename, payload in message.attachments: print(filename, payload) # MAILBOX # ------- # NOTE: You can use 2 approaches to perform these operations # "by one" - Perform operation for each message separately per N commands # "in bulk" - Perform operation for message set per 1 command
def mail_connexion(): mailbox = MailBox(cs.mail_server) mailbox.login(cs.mail_address, cs.mail_password, \ initial_folder = cs.mail_folder) return mailbox
def get_mbox(auth): mbox = MailBox(auth['host'], auth['port']) if (is_exist(auth, 'box') and is_empty(auth['box']) == False): box = auth['box'] mbox.login(auth['username'], auth['password'], initial_folder=box) return mbox
import webbrowser import os from imap_tools import MailBox, Q import datetime import pandas as pd import re import bestBuy import csv #set up the email connection in imap and imap_tools username = "******" password = "******" imap = imaplib.IMAP4_SSL("imap.gmail.com") mailbox = MailBox("imap.gmail.com") imap.login(username, password) mailbox.login(username, password, initial_folder="INBOX") status, numberMessages = imap.select() #date from which we want the emails year = 2020 day = 8 month = 5 #fetches Best Buy emails bestBuyMessages = mailbox.fetch( Q(from_="*****@*****.**", date_gte=datetime.date(year, month, day))) #creates output dataframe that will be added to orderConfirmations = pd.DataFrame(index=['BBY01'], columns=[
def check_phish(mid): phishing_mails = [] # Retrieves the email address instance logger.info("Click-to-check entered..") owner_id = get_owner_id_from_email_id(mid) if current_user.is_anonymous or not owner_id == current_user.get_id(): logger.warning("Anonymous or unauthorized user attempting"\ " phish check of address ID {}!".format(mid)) return redirect(url_for('index')) mailaddr = get_email_address_by_email_id(mid) # Redirects back to page if selected email is inactive if mailaddr.get_active_status() == False: logger.warning("Redirecting.. User selected inactive email address %s"\ , mailaddr.get_email_address()) flash("Email is inactive!", 'error') return redirect(url_for('dash_email')) logger.info("Mailbox selected is %s", mailaddr.get_email_address()) try: # Logs in to mailbox by retrieving the corresponding IMAP server imap_svr = get_imap_svr(mailaddr.get_email_address()) logger.info("Retrieving IMAP server: %s", imap_svr) mailbox = MailBox(imap_svr) logger.info("Attempting connection..") mailbox.login(mailaddr.get_email_address()\ , mailaddr.get_decrypted_email_password()) logger.info("Connected to mailbox %s", mailaddr.get_email_address()) except ConnectionRefusedError: logger.error("Unable to connect to mailbox for %s", mailaddr.get_email_address()) flash("Unable to connect to mailbox, please update your password!", 'error') return redirect(url_for('dash_email')) # Retrieves date last updated # if new email address is added column is empty # sets last_updated to today - 1 day so that mails in the last 24 hours # are checked last_updated = mailaddr.get_last_updated() \ if mailaddr.get_last_updated() else datetime.today() - timedelta(days=1) # Selects mailbox to Inbox only mailbox.folder.set("INBOX") logger.info("Fetching mails..") # Sets a check criteria so that # only mails newer than last_updated and unread mails are checked check_criteria = AND(date_gte=last_updated.date(), seen=False) """ FOR DEMO USE """ # Test code to intentionally pull retrieve backdated emails for demo purposes # last_updated = datetime(2020, 12,17, 0, 0, 0) # check_criteria = AND(date_gte=[date(2020, 12, 17)], seen=False) # Fetch mails from mailbox based on criteria, does not "read" the mail # and retrieves in bulk for faster performance at higher computing cost all_mails = mailbox.fetch(check_criteria, reverse=True, mark_seen=False, bulk=True) logger.info("Mails fetched..") # Iterates through the mails that are not sent from the sender's address # Creates a EmailData instance for each mail to generate features based on # preprocessing logic, passes it into the model - if predict returns 1 it is a detected phish # appends the detected mail into a list of Mail (phishing_mails) # The purpose of Mail class is for easier display - the values are pulled from the # imap_tool's Mail item instead of our EmailData. # Inserts all phishing mails to the database data = { 'total_count': 0, 'detection_count': 0, 'check_time': datetime.now().strftime('%d-%m-%Y, %H:%M') } mail_check_count = 0 for msg in all_mails: try: sender = msg.from_ except HeaderParseError: # Exception happens when a msg.from_ is malformed resulting in # unparseable values. Automatically assume phishing email and add to record. # Denote Sender as 'INVALID_SENDER' logger.error("HeaderParseError, unparseable msg.from_. \ Setting sender as INVALID_SENDER") sender = 'INVALID_SENDER' if (check_valid_time(last_updated, msg.date)) \ and check_valid_sender(sender, mailaddr.get_email_address()): data['total_count'] += 1 mail_check_count += 1 mail_item = EmailData(msg.subject, sender, msg.attachments\ , (msg.text + msg.html), msg.headers) mail_item.generate_features() result = model.predict(mail_item.repr_in_arr()) logger.info("Checking mail: %s -- Result: %s"\ , mail_item.get_subject(), result) if result == 1: logger.info("Phishing mail detected, subject: %s", msg.subject) mail_exist = check_p_mail_exist(mailaddr.get_email_id()\ , msg.subject, mail_item.get_content()) if not mail_exist: phishing_mails.append(Mail(sender, \ msg.date.astimezone(timezone('Asia/Singapore')), msg.subject)) data['detection_count'] += 1 detected_mail = PhishingEmail( \ sender_address = sender, \ subject = msg.subject, \ content = mail_item.get_content(), \ created_at = datetime.now(), \ receiver_id = mailaddr.get_email_id() ) db.session.add(detected_mail) # Updates last updated to current time mailaddr.set_last_updated(datetime.now()) logger.info("Updating mailbox last updated from %s to %s",\ last_updated.strftime("%d-%m-%Y, %H:%M:%S"), datetime.now()) mailaddr.set_phishing_mail_detected(data['detection_count']) mailaddr.set_total_mails_checked(mail_check_count) db.session.commit() logger.info("Finished checking mails.. logging out") mailbox.logout() send_phish_check_notice(mailaddr.get_email_address(), phishing_mails) mailaddr = get_email_address_by_email_id(mid) mail_address = mailaddr.get_email_address() # return redirect(url_for('dashboard')) return render_template('dashboard/detection_results.html', \ phishing_mails = phishing_mails, data=data, mail_address = mail_address)
settings = common.get_config() # process any pending jobs on startup responses.process(settings) # watch the inbox for form submissions (or edits) # imap host, username & password are stored externally as a json file. print("imap host:", settings["host"]) print("imap email:", settings["user"]) mailbox = None connected = False while True: if not connected: mailbox = MailBox(settings["host"]) mailbox.login(settings["user"], settings["password"]) connected = True now = datetime.datetime.now() print("Checking for new mail notifications:", now.strftime("%Y-%m-%d %H:%M:%S")) try: mailbox.folder.set("INBOX") # messages = mailbox.fetch(AND(all=True), headers_only=True) # messages = mailbox.fetch(AND(seen=False), headers_only=True) messages = mailbox.fetch(AND(seen=False)) except: print("imap connection dropped, or fetch failed...") connected = False if connected: form_notification = False for msg in messages:
# Press the green button in the gutter to run the script. if __name__ == '__main__': # tester() 'Give feedback on start' status = 'Email_Rulz program started at: ' + datetime.datetime.now( ).strftime("%Y-%M-%d %H:%M:%S") print(status) runlog.append(status) # Connect to mailbox mailbox = MailBox(imapserver) try: mailbox.login(userid, pd) runlog.append("Mailbox connected") except Exception as e: runlog.append("Mailbox NOT connected. Error=" + str(e)) # Open a 2nd connection using the imaplib library. # This will allow me to create new email (imap-tools doesn't support) # TODO: Not eloquent to login to the imap server twice mailbox2 = imaplib.IMAP4_SSL(imapserver) try: mailbox2.login(userid, pd) runlog.append("Mailbox2 connected") except Exception as e: runlog.append("!!! ERROR Mailbox2 NOT connected. Error=" + str(e))
# Create a SlackClient for your bot to use for Web API requests SLACK_BOT_TOKEN = os.environ["SLACK_TOKEN"] def sendToSlack(msg: str, token: str): slackClient = WebClient(token) slackClient.chat_postMessage(channel='G01E0FE3KR7', text=msg) try: logging.basicConfig(level=logging.DEBUG) logging.debug('logging in...') mailbox = MailBox(IMAP_SERVER) mailbox.login(MAIL, PASSWORD) logging.debug('Mail counting...') nCount = len([msg.subject for msg in mailbox.fetch(AND(all=True))]) print(nCount) while True: # get list of email subjects from INBOX folder - equivalent verbose version mailbox = MailBox(IMAP_SERVER) mailbox.login(MAIL, PASSWORD) temp = mailbox.fetch(AND(all=True)) mails = [] for msg in temp: mails.append((msg.subject, msg.from_, msg.text[:120] + '...'))