Example #1
0
    def download_all_mailbox(self):
        with Imbox(
                self.server,
                username=self.userName,
                password=self.password,
                ssl=self.ssl,
                ssl_context=self.ssl_context,
                starttls=self.starttls,
        ) as imbox:

            # Gets all messages after the day x
            all_inbox_messages = imbox.messages()

            i = 0
            for uid, message in reversed(all_inbox_messages):
                # Check if the mail exists in the local database
                percentage = i / all_inbox_messages.__len__()
                i = i + 1
                query = DBApi("mails").get("uuid",
                                           "WHERE uuid =" + uid.decode())
                if not query:
                    try:
                        self.mail_parsing_from_server(uid, message, "1",
                                                      "Inbox")
                    except Exception as e:
                        pass
Example #2
0
 def get_attach_info_from_db(self, uid):
     data_stream = DBApi("attach").get_files_information(
         uid=uid, user_id=self.user_id)
     files = []
     for file in data_stream:
         files.append(file[0])
     return files
Example #3
0
def add_contact(name, surname, mail, note, nick):
    DBApi('contact').insert({
        'name': name,
        'surname': surname,
        'mail': mail,
        'note': note,
        'nick': nick
    })
Example #4
0
def get_email_platform():
    # Getting vendors with ID
    result = DBApi('mail_server_settings').get(field='id, service_name',
                                               expression='')
    fe_data = []
    for data in result:
        fe_data.append({'id': data[0], 'name': data[1]})
    return fe_data
Example #5
0
 def get_number_unread(self):
     imap = ImapApi().get_number_unread_from_server()
     db = DBApi().get_unopened()
     if imap > db:
         # we need to fetch new mails
         if multiprocessing.active_children() is None:
             p = multiprocessing.Process(
                 target=self.download_new_mails_from_server(), args=())
             p.start()
     return imap
Example #6
0
    def download_new_mails_from_server(self):
        """This func download new mails from the server and
        save them in the local database"""

        # @todo: get folder list -> download per folder (pool of download)

        # Getting the last email uid
        last_mail_id = DBApi().get_last_email_id(user_id=self.__userId)

        # Getting new mails and saving them in the local db
        ImapApi().save_greater_than_uuid_from_server(uuid=last_mail_id)
Example #7
0
def get_email_platform_settings(id):
    result = DBApi('mail_server_settings').get(
        field='*', expression=f'WHERE ID = {str(id)}')
    fe_data = []
    for data in result:
        fe_data.append({
            'id': data[0],
            'name': data[1],
            'smtp': data[2],
            'imap': data[3],
            'ssl': data[4],
            'sslcontext': data[5],
            'starttls': data[6],
        })
    return fe_data
Example #8
0
def first_download():
    """ DOWNLOAD 200 MAILS FROM THE POSTAL
    AND SAVE THEM IN THE DB

    Run only on the first login - When the
    user scrolls down more emails are downloader
    from the server
    """
    mails = []
    with Imbox(get_user_connection_data()) as imbox:
        # Gets all messages from the inbox
        all_inbox_messages = imbox.messages()

        for uid, message in reversed(all_inbox_messages):
            sanitized_body = str(message.body["html"])
            sanitized_body = sanitized_body.replace(r"['\r\n", "
")
            sanitized_body = sanitized_body.replace(r"[b'", "
")
            sanitized_body = sanitized_body.replace(r"\r\n", "
")
            sanitized_body = sanitized_body.replace(r"\r", "
")
            sanitized_body = sanitized_body.replace(r"\n", "
")
            sanitized_body = sanitized_body.replace(r"\t", "")
            sanitized_body = sanitized_body.replace(r"['", "")
            sanitized_body = sanitized_body.replace(r"']", "")

            # Apply local time to base server time
            from_name = message.sent_from[0]["name"]
            from_mail = message.sent_from[0]["email"]
            to_name = message.sent_to[0]["name"]
            to_mail = message.sent_to[0]["email"]

            subject = str(message.subject) if str(
                message.subject) else "(No subject)"

            data = [
                uid.decode(),
                str(from_name),
                str(from_mail),
                str(to_name),
                str(to_mail),
                str(subject),
                str(sanitized_body),
                str(message.body["plain"]),
                "Inbox",
                str(message.date),
            ]

            DBApi("emails").insert(data)
            """
Example #9
0
    def get_mails(self, step):
        ImapApi().get_today_mails()

        # return mails day by day
        step = step + 60
        mails = DBApi("mails").get_mail(step=step, user_id=self.user_id)

        # now we need to create the dict
        dict_mails = []
        for mail in mails:
            # Getting json info
            # read file
            try:
                with open(".db/mails/" + str(mail[1]) + ".json",
                          "r") as my_file:
                    data = my_file.read()

                # parse file
                obj = json.loads(data)

                # getting attach info
                shipped_with = self.get_attach_info_from_db(mail[1])
                # parsing datetime
                received = mail[6]
                parsed_date = dateutil.parser.parse(received)
                parsed_date = parsed_date.strftime("%d/%m/%Y %H:%M:%S")

                # Creating the dict
                temp_mail = {
                    "uid": mail[1],
                    "From_name": str(obj['From_name']),
                    "from_mail": str(obj['from_mail']),
                    "To_name": str(obj['To_name']),
                    "To_mail": str(obj['To_mail']),
                    "Subject": str(mail[2]),
                    "bodyHTML": str(obj['bodyHTML']),
                    "bodyPLAIN": str(obj['bodyPLAIN']),
                    "attach": obj['attach'],
                    "directory": str(mail[4]),
                    "datetimes": str(parsed_date),
                    "readed": str(mail[5]),
                }

                dict_mails.append(temp_mail)
            except Exception as e:
                logger.error('Error retrieving emails', e)

        return dict_mails
Example #10
0
def get_mails(year, month, day):
    # Messages received after specific date
    # inbox_messages_received_after = imbox.messages(date__gt=datetime.date(2018, 7, 30))

    mails = []
    with Imbox(get_user_connection_data()) as imbox:
        # Gets all messages after the day x

        # to get the mails of today:
        all_inbox_messages = imbox.messages(
            date__on=datetime.date(year, month, day))

        for uid, message in reversed(all_inbox_messages):
            sanitized_body = str(message.body["html"])
            sanitized_body = sanitized_body.replace(r"['\r\n", "
")
            sanitized_body = sanitized_body.replace(r"[b'", "
")
            sanitized_body = sanitized_body.replace(r"\r\n", "
")
            sanitized_body = sanitized_body.replace(r"\r", "
")
            sanitized_body = sanitized_body.replace(r"\n", "
")
            sanitized_body = sanitized_body.replace(r"\t", "")
            sanitized_body = sanitized_body.replace(r"['", "")
            sanitized_body = sanitized_body.replace(r"']", "")

            # Apply local time to base server time
            from_name = message.sent_from[0]["name"]
            from_mail = message.sent_from[0]["email"]
            to_name = message.sent_to[0]["name"]
            to_mail = message.sent_to[0]["email"]

            subject = str(message.subject) if str(
                message.subject) else "(No subject)"

            DBApi("emails").insert(
                {
                    "uid": uid.decode(),
                    "from_name": str(from_name),
                    "from_mail": str(from_mail),
                    "to_name": str(to_name),
                    "to_mail": str(to_mail),
                    "subject": str(subject),
                    "bodyHTML": str(sanitized_body),
                    "bodyPLAIN": str(message.body["plain"]),
                    "directory": "",
                    "datetimes": datetime.date(year, month, day),
                }, )
Example #11
0
    def get_folder(self, folder_name="Inbox"):
        mails = DBApi("mails").get_mail(user_id=self.user_id,
                                        folder=folder_name)

        # now we need to create the dict
        dict_mails = []
        for mail in mails:
            # Getting json info
            # read file
            with open(".db/mails/" + str(mail[1]) + ".json", "r") as my_file:
                data = my_file.read()

            # parse file
            obj = json.loads(data)

            # getting attach info
            self.get_attach_info_from_db(mail[0])

            # Creating the dict
            temp_mail = {
                "uid": mail[1],
                "From_name": str(obj['From_name']),
                "from_mail": str(obj['from_mail']),
                "To_name": str(obj['To_name']),
                "To_mail": str(obj['To_mail']),
                "Subject": str(mail[2]),
                "bodyHTML": str(obj['bodyHTML']),
                "bodyPLAIN": str(obj['bodyPLAIN']),
                "attach": str(obj['attach']),
                "directory": str(mail[4]),
                "datetimes": str(mail[6]),
                "readed": str(mail[5]),
            }

            dict_mails.append(temp_mail)

        return dict_mails
Example #12
0
def get_user_id():
    data = DBApi("user").get("id", "WHERE is_logged_in = 1")
    p = data[0]
    return p[0]
Example #13
0
def get_user_server_config(what):
    config_id = get_user_info("mail_server_setting")
    data = DBApi("mail_server_settings").get(what,
                                             "WHERE id = " + str(config_id))
    p = data[0]
    return p[0]
Example #14
0
 def mark_as_seen(uid):
     ImapApi().mark_as_seen(uid=uid)
     DBApi().mark_as_seen(uid=uid)
Example #15
0
def delete_mail(uid):
    # @todo: delete the mail both local and remote
    DBApi('mails').delete(what=uid, where='')
Example #16
0
def del_note(uid):
    DBApi('notes').delete(what=uid, where='')
Example #17
0
def add_note(text, uid, attach):
    DBApi('notes').insert({'uid': uid, 'attach': attach, 'text': text})
Example #18
0
 def get_username():
     data = DBApi('user').get('name', 'WHERE is_logged_in = 1')
     p = data[0]
     return p[0]
Example #19
0
 def get_user_id():
     data = DBApi('user').get('id', 'WHERE is_logged_in = 1')
     p = data[0]
     return p[0]
Example #20
0
 def user_registration(datagram):
     DBApi('user').insert(data=datagram)
Example #21
0
 def test__data_get(self):
     self.assertEqual(DBApi("user").get("id", "WHERE ID = 1"), [])
Example #22
0
def mail_parsing(uid, message, unread_uid, directory):
    if len(message.body['html']) < 1:
        sanitized_body = str(message.body['plain'][0])
    else:
        sanitized_body = str(message.body['html'][0])

    from_name = message.sent_from[0]['name'] if message.sent_from else ''
    from_mail = message.sent_from[0]['email'] if message.sent_from else ''
    to_name = message.sent_to[0]['name'] if message.sent_to else ''
    to_mail = message.sent_to[0]['email'] if message.sent_to else ''

    date_message = message.date

    # If html body is empty, load the plain
    if sanitized_body == '[]':
        sanitized_body = message.body['plain']

    if uid.decode() in unread_uid:
        unread = False
    else:
        unread = True

    # Getting the subject
    subject = str(message.subject) if str(message.subject) else '(No subject)'

    # saving attach in the local disk and on the db
    attach_names = []
    for attach in message.attachments:
        attach_name = attach.get('filename')
        attach_names.append(attach_name)
        content = attach.get('content').read()

        if not attach_name or not content:
            return

        with open('.db/mails/attach/' + uid.decode() + '_' + attach_name,
                  'wb') as file:
            file.write(content)
        payload = {
            'uuid': uid.decode(),
            'subject': subject,
            'real_filename': attach_name,
            'saved_as': uid.decode() + '_' + attach_name,
            'user_id': '1',
            'deleted': 'false',
            'datetime': date_message,
        }
        # saving the file information into the db
        DBApi('files').insert(data=payload)

    appmails = {
        'uid': uid.decode(),
        'From_name': str(from_name),
        'from_mail': str(from_mail),
        'To_name': str(to_name),
        'To_mail': str(to_mail),
        'Subject': str(subject),
        'bodyHTML': str(sanitized_body),
        'bodyPLAIN': str(message.body['plain']),
        'attach': attach_names,
        'directory': directory,
        'datetimes': str(''),
        'readed': unread,
    }

    # inserting the mail in the database
    mail_payload = {
        'uuid': uid.decode(),
        'subject': str(subject),
        'user_id': '1',
        'readed': date_message,
    }

    DBApi('mails').insert(data=mail_payload)

    with open('./.db/mails/' + str(uid.decode()) + '.json', 'w') as file:
        json.dump(appmails, file)

    return appmails
Example #23
0
def get_user_info(what):
    data = DBApi("user").get(what, "WHERE is_logged_in = 1")
    p = data[0]
    return p[0]
Example #24
0
    def mail_parsing_from_server(self, uid, message, unread_uid, directory):
        """
        This function clean, parse and save a mail in the local filesystem.
        """
        # CHECK IF FILE EXIST
        if path.exists("./.db/mails/" + str(uid.decode()) + ".json"):
            return

        if len(message.body["html"]) < 1:
            sanitized_body = str(message.body["plain"][0])
        else:
            sanitized_body = str(message.body["html"][0])

        from_name = message.sent_from[0]["name"] if message.sent_from else ""
        from_mail = message.sent_from[0]["email"] if message.sent_from else ""
        to_name = message.sent_to[0]["name"] if message.sent_to else ""
        to_mail = message.sent_to[0]["email"] if message.sent_to else ""

        date_message = message.date
        date_time_obj = parsedate_to_datetime(date_message)

        iso_date = date_time_obj.isoformat()

        # If html body is empty, load the plain
        if sanitized_body == "[]":
            sanitized_body = message.body["plain"][0]

        # Getting clean raw body
        if len(message.body["plain"]) < 1:
            plain_message = ""
        else:
            plain_message = message.body["plain"][0]

        if uid.decode() in unread_uid:
            unread = False
        else:
            unread = True

        # Getting the subject
        subject = str(message.subject) if str(
            message.subject) else "(No subject)"

        # saving attach in the local disk and on the db
        attach_names = []
        for attach in message.attachments:
            attach_name = attach.get("filename")
            attach_names.append(attach_name)
            content = attach.get("content").read()

            if not attach_name or not content:
                return

            with open(".db/mails/attach/" + uid.decode() + "_" + attach_name,
                      "wb") as file:
                file.write(content)
            payload = {
                "uuid": uid.decode(),
                "subject": subject,
                "real_filename": attach_name,
                "saved_as": uid.decode() + "_" + attach_name,
                "user_id": "1",
                "deleted": "false",
                "added": str(iso_date),
            }
            # saving the file information into the db
            DBApi("files").insert(data=payload)

        appmails = {
            "uid": uid.decode(),
            "From_name": str(from_name),
            "from_mail": str(from_mail),
            "To_name": str(to_name),
            "To_mail": str(to_mail),
            "Subject": str(subject),
            "bodyHTML": str(sanitized_body),
            "bodyPLAIN": str(plain_message),
            "attach": attach_names,
            "directory": directory,
            "datetimes": str(iso_date),
            "readed": unread,
        }

        # inserting the mail in the database
        mail_payload = {
            "uuid": uid.decode(),
            "subject": str(subject),
            "user_id": str(self.userId),
            "folder": directory,
            "opened": unread,
            "received": str(iso_date),
        }

        DBApi("mails").insert(data=mail_payload)

        with open(".db/mails/" + str(uid.decode()) + ".json", "w") as file:
            json.dump(appmails, file)

        return appmails