Beispiel #1
0
    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)
Beispiel #2
0
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 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
Beispiel #4
0
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
Beispiel #5
0
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()
Beispiel #6
0
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()
Beispiel #7
0
    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
Beispiel #8
0
    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
Beispiel #9
0
    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
Beispiel #10
0
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
Beispiel #11
0
    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()
Beispiel #12
0
    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
Beispiel #13
0
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)
Beispiel #14
0
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 + ".")
Beispiel #15
0
    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)
Beispiel #17
0
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
Beispiel #18
0
from imap_tools import MailBox
from account import *

# 입력한 정보로 로그인하기
mailbox = MailBox("imap.gmail.com", 993)
mailbox.login(EMAIL_ADDRESS, EMAIL_PASSWORD, initial_folder="INBOX")

with MailBox("imap.gmail.com", 993).login(EMAIL_ADDRESS,
                                          EMAIL_PASSWORD,
                                          initial_folder="INBOX") as mailbox:
    # # 전체 메일 다 가져오기
    # for msg in mailbox.fetch():
    #     print("[{}] {}".format(msg.from_, msg.subject))

    # 읽지 않은 메일 가져오기
    for msg in mailbox.fetch('(UNSEEN)'):
        print("[{}] {}".format(msg.from_, msg.subject))

    # 특정인이 보낸 메일 가져오기
    for msg in mailbox.fetch('(FROM [email protected])', limit=3,
                             reverse=True):
        print("[{}] {}".format(msg.from_, msg.subject))

    # 작은 따옴표로 먼저 감싸고, 실제 TEXT 부분은 큰 따옴표로 감싸기
    # 어떤 글자를 포함하는 메일 (제목, 본문)
    for msg in mailbox.fetch('(TEXT "test mail.")'):
        print("[{}] {}".format(msg.from_, msg.subject))

    # 어떤 글자를 포함하는 메일 (제목만)
    for msg in mailbox.fetch('(SUBJECT "test mail")'):
        print("[{}] {}".format(msg.from_, msg.subject))
Beispiel #19
0
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))
Beispiel #20
0
2. mailbox.login()
    Login to mailbox account
    It supports context manager, so you do not need to call logout() in this example
    Select INBOX folder, cause login initial_folder arg = 'INBOX' by default (set folder may be disabled with None)
    
3. mailbox.fetch()
    First searches email nums by criteria in current folder, then fetch and yields MailMessage
    criteria arg = 'ALL' by default
    Current folder is 'INBOX' (set on login)
    Fetch each message separately per N commands, cause bulk arg = False by default
    Mark each fetched email as seen, cause fetch mark_seen arg = True by default
    
4. print
    msg variable is MailMessage instance
    msg.date - email data, converted to datetime.date
    msg.subject - email subject, utf8 str
    msg.text - email plain text content, utf8 str
    msg.html - email html content, utf8 str
"""
with MailBox('imap.mail.com').login('*****@*****.**', 'pwd') as mailbox:
    for msg in mailbox.fetch():
        print(msg.date, msg.subject, len(msg.text or msg.html))

# Equivalent verbose version:
mailbox = MailBox('imap.mail.com')
mailbox.login('*****@*****.**', 'pwd',
              'INBOX')  # or use mailbox.folder.set instead initial_folder arg
for msg in mailbox.fetch(AND(all=True)):
    print(msg.date, msg.subject, len(msg.text or msg.html))
mailbox.logout()
Beispiel #21
0
from imap_tools import MailBox, AND

usuario = '*****@*****.**'
senha = '   123'

meu_email = MailBox('imap.gmail.com').login(usuario, senha)

#Pegar email que foram enviados por um remetente especifico

listaEmails = meu_email.fetch(AND(from_='*****@*****.**'))

print(f'Existe quantos e-mails com esse remetente ? {len(list(listaEmails))}')

#Pegar e-mail sobre o assunto ...
listaEmails2 = meu_email.fetch(AND(subject='Alerta'))

for email in listaEmails2:
    print(email.subject)
    print(email.text)

#Pegando anexo
listaEmails3 = meu_email.fetch(AND(from_='*****@*****.**'))
for email in listaEmails3:
    if len(email.attachments) > 0:
        for anexo in email.attachments:
            if 'Relatorio.pdf' in anexo.filename:
                informacoes_anexo = anexo.payload
                with open('CriandoArquivo', 'wb') as arquivo:
                    arquivo.write(informacoes_anexo)

meu_email.logout()
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=[
                                      'Shipping #', 'Date',
                                      'Product # Quantity', 'Destination',
                                      'Cost'
                                  ])
shippedOrders = pd.DataFrame(index=['BBY01'],
                             columns=[
                                 'Shipping #', 'Date', 'Product # Quantity',
                                 'Destination', 'Cost'
                             ])
#orderConfirmations = pd.read_csv(r'orderConfirmations.csv', index_col = 0)
Beispiel #23
0
async def updatemails(ctx):
    """Check for new staff applications and ban appeals"""
    if "Staff" not in str(ctx.author.roles):
        await ctx.respond(":warning: Insufficient permission.", ephemeral=True)
        return

    channel = bot.get_channel(564783779474833431)

    # Connect to imap
    mailbox = MailBox(config.IMAP_HOST).login(config.IMAP_NAME,
                                              config.IMAP_PASS, "INBOX")

    # Cycle through inbox
    for mail in mailbox.fetch():

        # Try to split the email into the form's parts
        try:
            content = mail.text.split("§§")
        except:
            pass

        # Switch submission types

        # Staff application
        if len(content) == 11:
            embed = discord.Embed(
                title=content[0],
                description=f"{content[1]} years old, {content[2]}")
            embed.add_field(name="Minecraft username",
                            value=f"`{content[3]}`",
                            inline=True)
            embed.add_field(name="Discord#Tag",
                            value=f"`{content[4]}`",
                            inline=True)
            embed.add_field(name="E-mail",
                            value=f"`{content[5]}`",
                            inline=True)
            embed.add_field(name="Do you have experience as staff?",
                            value=content[6],
                            inline=False)
            embed.add_field(name="Why do you want to be staff?",
                            value=content[7],
                            inline=False)
            embed.add_field(
                name="Why should you be chosen instead of someone else?",
                value=content[8],
                inline=False)
            embed.add_field(
                name="Do you have any issues with the current staff team?",
                value=content[9],
                inline=False)
            embed.add_field(name="How many hours per week can you contribute?",
                            value=content[10],
                            inline=False)
            content_length = len(content[6].split()) + len(
                content[7].split()) + len(content[8].split())
            embed.set_footer(text=f"{content_length} words")
            if content_length > 50:
                msg = await channel.send(
                    content="**STAFF APPLICATION** @staff", embed=embed)
            else:
                msg = await channel.send(content="**STAFF APPLICATION**",
                                         embed=embed)
            await msg.add_reaction("<:vote_yes:601899059417972737>")
            await msg.add_reaction("<:vote_no:601898704231989259>")
            await msg.create_thread(name=f"{content[0]}\'s Staff Application")

        # Ban appeal
        elif len(content) == 4:
            embed = discord.Embed(title="Ban Appeal")
            embed.add_field(name="Minecraft username",
                            value=f"`{content[0]}`",
                            inline=True)
            embed.add_field(name="Contact",
                            value=f"`{content[1]}`",
                            inline=True)
            embed.add_field(name="More about your ban",
                            value=content[2],
                            inline=False)
            embed.add_field(name="Why should you be unbanned?",
                            value=content[3],
                            inline=False)
            content_length = len(content[2].split()) + len(content[3].split())
            embed.set_footer(text="{} words".format(content_length))
            if content_length > 50:
                msg = await channel.send(content="@staff", embed=embed)
            else:
                msg = await channel.send(embed=embed)
            await msg.add_reaction("<:vote_yes:601899059417972737>")
            await msg.add_reaction("<:vote_no:601898704231989259>")
            await msg.create_thread(name=f"{content[0]}\'s Ban Appeal")

        # Unknown
        else:
            embed = discord.Embed(
                title="Unknown email",
                description=
                f"I've received an email that doesn't match the layout of any form:\n\n{mail.text}"
            )
            await channel.send(embed=embed)

        # Delete the email and log out
        mailbox.delete(mail.uid)
    mailbox.logout()
Beispiel #24
0
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:
            print(msg.from_)
            print(msg.subject)
            if "Virtual Choir" in msg.subject:
                form_notification = True
        if form_notification:
            print("new google form work ...")
            responses.fetch(settings["responses"])
            responses.process(settings)
Beispiel #25
0
import common
import responses

settings = common.get_config()

# 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 = MailBox(settings["host"])
mailbox.login(settings["user"], settings["password"])
while True:
    now = datetime.datetime.now()
    notification = False
    print("Checking for new mail notifications:",
          now.strftime("%Y-%m-%d %H:%M:%S"))
    mailbox.folder.set("INBOX")
    messages = mailbox.fetch(AND(all=True), headers_only=True)
    # messages = mailbox.fetch(AND(seen=False), headers_only=True)
    for msg in messages:
        print(msg.subject)
        if "Virtual Choir" in msg.subject:
            notification = True
    if notification:
        print("new work ...")
        responses.fetch(settings["responses"])
        responses.process()
    # subjects = [msg.subject for msg in mailbox.fetch(AND(all=True))]
    print("  sleeping", settings["interval"], "seconds ...")
    time.sleep(settings["interval"])
Beispiel #26
0
class Control():
    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 load(self):
        """Try to load all modules found in the modules folder"""
        print("\n")
        print("Loading modules...")
        self.modules = []
        path = os.path.join(os.path.dirname(__file__), "modules")
        directory = pkgutil.iter_modules(path=[path])
        for finder, name, ispkg in directory:
            try:
                loader = finder.find_module(name)
                module = loader.load_module(name)
                if hasattr(module, "commandWords") \
                        and hasattr(module, "moduleName") \
                        and hasattr(module, "execute"):
                    self.modules.append(module)
                    print("The module '{0}' has been loaded, "
                          "successfully.".format(name))
                else:
                    print("[ERROR] The module '{0}' is not in the "
                          "correct format.".format(name))
            except:
                print("[ERROR] The module '" + name + "' has some errors.")
        print("\n")

    def fetch_command(self):
        """Retrieve the last Note created if new id found"""
        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:
            latest_email_id = uidlist[-1].split()[-1]
        except IndexError:
            return

        if latest_email_id == self.last_checked:
            return

        self.last_checked = latest_email_id
        data = subjects[-1]
        return data.lower().strip()

    def handle(self):
        """Handle new commands
        Poll continuously every second and check for new commands.
        """
        print("Fetching commands...")
        print("\n")

        while True:
            folder_status = self.mail.folder.status('Notes')
            nb_messages = folder_status[
                "MESSAGES"]  # permet d'actualiser la mailbox
            try:
                command = self.fetch_command()
                if not command:
                    raise ControlException("No command found.")
                print("The word(s) '" +
                      unidecode.unidecode(str(command).lower()) +
                      "' have been said")
                for module in self.modules:
                    foundWords = []
                    for word in module.commandWords:
                        #print("command=",unidecode.unidecode(str(command).lower()))
                        #print("word=", unidecode.unidecode(str(word).lower()))
                        #print(unidecode.unidecode(str(word).lower()) in unidecode.unidecode(str(command).lower()))
                        if unidecode.unidecode(
                                str(word).lower()) in unidecode.unidecode(
                                    str(command).lower()):
                            foundWords.append(
                                unidecode.unidecode(str(word).lower()))
                    if len(foundWords) == len(module.commandWords):
                        try:
                            module.execute(
                                unidecode.unidecode(str(command).lower()))
                            print("The module {0} has been executed "
                                  "successfully.".format(module.moduleName))
                        except:
                            print("[ERROR] There has been an error "
                                  "when running the {0} module".format(
                                      module.moduleName))
                    else:
                        print("\n")
                    self.mail.move(self.mail.fetch(), 'notes_old')
            except (TypeError, ControlException):
                pass
            except Exception as exc:
                print("Received an exception while running: {exc}".format(
                    **locals()))
                print("Restarting...")
            time.sleep(1)
Beispiel #27
0
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] + '...'))

        #subjects = [msg.subject for msg in mailbox.fetch(AND(all=True))]

        if len(mails) == nCount:
            logging.debug('No new messages')
Beispiel #28
0
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)
Beispiel #29
0
class IservMails(Task):
    def __init__(self, bot: Bot):
        self.name = self._get_name(__file__)
        self.bot = bot

        self.shown_mails = self.bot.db.table('shown_mails')
        self.mailbox = None
        self.mail_date_within = timedelta(weeks=1)

        self.executor = ThreadPoolExecutor(1)
        self.loop = asyncio.get_event_loop()

    async def run_once(self) -> None:
        await self.loop.run_in_executor(self.executor, self._init_mailbox)
        await self.bot.mail_channel_available.wait()

    async def run(self) -> None:
        mails = await self.loop.run_in_executor(self.executor, self._get_mails)

        shown_mail_uids = [elem['uid'] for elem in self.shown_mails.all()]
        not_shown_mails = [
            mail for mail in mails if mail.uid not in shown_mail_uids
        ]
        for mail in not_shown_mails:
            self.shown_mails.insert({'uid': mail.uid})

        for mail in not_shown_mails:
            embed = await self._generate_embed(mail)
            await self.bot.mail_channel.send(content='@everyone', embed=embed)

    def _cut_mail_text(self, text: str) -> str:
        if len(text.encode('utf-8')) > 5990:
            b = text.encode('utf-8')
            return b[:5990].decode('utf-8') + '...'
        elif len(text) > 1020:
            return text[:1020] + '...'
        else:
            return text

    async def _generate_embed(self, mail: MailMessage) -> Embed:
        embed = Embed(
            title=f'`Neue E-Mail: {mail.subject}`',
            type='rich',
            colour=Colour.dark_magenta(),
        )

        text = self._cut_mail_text(mail.text)

        embed.set_author(name=mail.from_)
        embed.add_field(name='Nachricht', value=text)
        embed.add_field(name='Datum', value=mail.date.strftime('%c'))
        embed.set_footer(text=self.bot.signature)

        return embed

    def _init_mailbox(self):
        self.mailbox = MailBox(config.iserv_hostname)
        self.mailbox.login(config.iserv_username, config.iserv_password)

    def _get_mails(self) -> List[MailMessage]:
        return list(
            self.mailbox.fetch(
                AND(to=config.target_mail,
                    date_gte=date.today() - self.mail_date_within),
                bulk=True,
            ))
Beispiel #30
0
class CustomMailBox():
    def __init__(self,user,pas,host='imap.gmail.com',n_per_page=40,folder='INBOX'):
        self.user=user
        self.pwd = pas
        self.host = host
        self.folder = folder
        self.n_per_page = n_per_page
    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 getbypagenum(self,page_number,searchterm):
        d = self.authuser()
        if d:

            print(page_number,self.n_per_page,'ufifidh')
            mb = IMAPClient(self.host)
            mb.login(self.user,self.pwd)
            mb.select_folder(d[self.folder])
            if searchterm:
                ids= mb.search(['OR',['OR',[u'TEXT',f'{searchterm}'],['FROM',f'{searchterm}']],['OR',[u'SUBJECT',f'{searchterm}'],[u'BODY',f'{searchterm}']]])
            else:
                ids = mb.search()
            print(len(ids),'hmmm')
            last = math.ceil(len(ids)/self.n_per_page)
            print(last,'last page')
            page_number = last-page_number+1
            start = max(0,((page_number-1)*self.n_per_page))
            end = min(len(ids),(page_number*self.n_per_page))
            print(start,end)
            print(ids[start:end])
            return (next(self.Mb_main.fetch(AND(uid=f'{m}'),headers_only=True,reverse=True) )for m in reversed(ids[start:end])),last
    def getbyuid(self,uid):
        if self.authuser():
            return self.Mb_main.fetch(AND(uid=uid))
    def getbysearch(self,text):
        if self.authuser():
            return self.Mb_main.fetch(OR(subject=text))
    def get_searched_chunks(self):
        gen = self.get_searched_chunks()
        pass
    def get_info(self,folder='INBOX'):
        if self.authuser():
            return self.Mb_main.folder.status(folder)
    def get_folders(self):
        if self.authuser():
            return self.Mb_main.folder.list()
    def get_cur(self):
        if self.authuser():
            return self.Mb_main.folder.get()
    def create_folder(self,folder):
        if self.authuser():
            if self.Mb_main.folder.exists(folder):
               return 'no'
            self.Mb_main.folder.create(folder)
            return 'ok'
    def delete_folder(self,folder):
        if self.authuser():
            self.Mb_main.folder.delete(folder)
    def rename_folder(self,folder):
        d = self.authuser()
        if d:
            self.Mb_main.folder.rename(d[self.folder],folder)
    def delete_msg(self,uid):
        if self.authuser():
            self.Mb_main.delete(uid)
    def move_msgto(self,folder,uid):
        if self.authuser():
            self.Mb_main.move(uid,folder)
    def copy_msgto(self,folder,uid):
        if self.authuser():
            self.Mb_main.copy(uid,folder)