Example #1
0
def login(name):
    account = EMAIL_ACCOUNTS[name]
    server = IMAPClient(account['Host'])
    server.login(account['User'], account['Password'])
    inbox = server.select_folder('INBOX', readonly=True)
    app_log.debug("Inbox: %s" % inbox)
    return server
Example #2
0
def loop():
    global oldmails

    server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
    server.login(USERNAME, PASSWORD)
 
    if DEBUG:
        print('Logging in as ' + USERNAME)
        select_info = server.select_folder(MAILBOX)
        print('%d messages in INBOX' % select_info['EXISTS'])
 
    folder_status = server.folder_status(MAILBOX, 'UNSEEN')
    newmails = int(folder_status['UNSEEN'])


    if DEBUG:
        print "You have", newmails, "new emails!"
 
    if newmails > oldmails:
        GPIO.output(GREEN_LED, True)
        GPIO.output(RED_LED, False)
    else:
        GPIO.output(GREEN_LED, False)
        GPIO.output(RED_LED, True)
 
    time.sleep(MAIL_CHECK_FREQ)
    oldmails = newmails
Example #3
0
def debug(account):
    c = ACCOUNTS[account]
    if 'pass_cmd' in c:
        c['pass'] = check_output([c['pass_cmd']], shell=True).strip()
    client = IMAPClient(c['host'], use_uid=True, ssl=c['ssl'])
    client.login(c['user'], c['pass'])
    print(client.capabilities())
Example #4
0
    def connect_account(self, email, pw, imap_endpoint, account_id=None):
        """Provide a connection to a IMAP account.

        Raises
        ------
        socket.error
            If we cannot connect to the IMAP host.
        IMAPClient.error
            If the credentials are invalid.
        """
        host, port = imap_endpoint
        try:
            conn = IMAPClient(host, port=port, use_uid=True, ssl=True)
        except IMAPClient.AbortError as e:
            log.error('account_connect_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error="[ALERT] Can't connect to host - may be transient")
            raise TransientConnectionError(str(e))
        except(IMAPClient.Error, gaierror, socket_error) as e:
            log.error('account_connect_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] (Failure): {0}'.format(str(e)))
            raise ConnectionError(str(e))

        conn.debug = False
        try:
            conn.oauth2_login(email, pw)
        except IMAPClient.AbortError as e:
            log.error('account_verify_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error="[ALERT] Can't connect to host - may be transient")
            raise TransientConnectionError(str(e))
        except IMAPClient.Error as e:
            log.error('IMAP Login error during connection. '
                      'Account: {}, error: {}'.format(email, e),
                      account_id=account_id)
            if str(e) == '[ALERT] Invalid credentials (Failure)' or \
               str(e) == '[AUTHENTICATIONFAILED] OAuth authentication failed.':
                raise ValidationError(str(e))
            else:
                raise ConnectionError(str(e))
        except SSLError as e:
            log.error('account_verify_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] (Failure) SSL Connection error')
            raise ConnectionError(str(e))

        return conn
    def _connect_and_fetch(self):
        self._server = IMAPClient(self.host, timeout=self.socket_timeout)
        self._server.login(self.username, self.password)

        self._server.select_folder('INBOX')

        self._fetch_unseen()
class EmailBot:
    def __init__(self):
        # TODO: refactor for OAUTH2  https://developers.google.com/gmail/imap/xoauth2-protocol , https://stackoverflow.com/questions/5193707/use-imaplib-and-oauth-for-connection-with-gmail
        # IMAP lib docs https://imapclient.readthedocs.io/en/2.1.0/concepts.html#tls-ssl
        ssl_context = ssl.create_default_context()
        ssl_context.check_hostname = False
        ssl_context.verify_mode = ssl.CERT_NONE
        self.ssl_context = ssl_context

    def connect_for_user(self, user: str, passw: str):
        self.server = IMAPClient('imap.gmail.com',
                                 ssl_context=self.ssl_context)
        self.server.login(user, passw)
        inbox_selected = self.server.select_folder('INBOX')

    def get_emails_for_user(self,
                            from_address: str = '*****@*****.**',
                            last_n_days: int = 5):
        cutoff = datetime.today() - timedelta(days=last_n_days)
        messages = self.server.search([
            'FROM "%s' % from_address,
            'SINCE %s' % cutoff.strftime('%d-%b-%Y')
        ])
        response = self.server.fetch(messages, ['RFC822'])

        for id, data in response.iteritems():
            msg_string = data['RFC822']
            msg = email.message_from_string(msg_string)
            print('IFrom: %s Date: %s' % (msg['From'], msg['date']))
Example #7
0
 def get_imap_connectivity(self):
     """ Connect to imap server and close the connection """
     context = ssl.SSLContext(ssl.PROTOCOL_TLS)
     context.options |= ssl.OP_NO_SSLv2
     context.options |= ssl.OP_NO_SSLv3
     context.options |= ssl.OP_NO_TLSv1
     context.options |= ssl.OP_NO_TLSv1_1
     context.verify_mode = ssl.CERT_NONE
     try:
         if self.opt_use_ssl:
             self.server = IMAPClient(self.opt_imap_server,
                                      use_uid=True,
                                      ssl=True,
                                      ssl_context=context)
         else:
             self.server = IMAPClient(self.opt_imap_server,
                                      use_uid=True,
                                      ssl=False)
     except Exception as e:
         raise Exception("Error connecting to %s with exception %s" %
                         (self.opt_imap_server, str(e)))
     else:
         self.helper.log_debug(
             'get_imap_connectivity: successfully connected to %s' %
             self.opt_imap_server)
def mail_check():
    # login to mailserver
    server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
    server.login(USERNAME, PASSWORD)

    #fetch from address

    # select our MAILBOX and looked for unread messages
    unseen = server.folder_status(MAILBOX, ['UNSEEN'])

    # number of unread messages
    # print to console to determine NEWMAIL_OFFSET
    newmail_count = (unseen[b'UNSEEN'])
    print('%d unseen messages' % newmail_count)
    lcd.clear()
    lcd.message = '%d IN INBOX' % newmail_count
    time.sleep(5.0)
    lcd.clear()

    if newmail_count > NEWMAIL_OFFSET:
        lcd_line1 = 'NEW MAIL'
        time.sleep(5.0)
        lcd.clear()

    else:
        lcd.message = 'NO NEW MAIL'
        time.sleep(5.0)
        lcd.clear()

    time.sleep(MAIL_CHECK_FREQ)
Example #9
0
class _IMAPConn():
    """ Class for MailFetch which defines connection + auth to IMAP server.
    Separated from MailFetch in order to implement as a context manager, so we don't leave the server connection open for the life of MailFetch()
    """
    def __init__(self, account_name: str,
                 server_options: Dict[str, Union[str, bool, int]],
                 auth_type: str, credentials: Tuple[str, str]) -> None:
        self.server_options = server_options
        self.account_name = account_name

        #connect
        self.server = IMAPClient(use_uid=True, timeout=None, **server_options)

        # login
        try:
            getattr(self.server, auth_type)(*credentials)
        except (NameError, TypeError) as e:
            logging.exception(
                'No (or wrong type of) credentials were passed to mail._IMAPConn for imap server {} from config.conf, are you sure this is correct? '
                .format(self.server_options['host']),
                exc_info=True)
            raise IMAPExceptions.LoginError() from e

    def __enter__(self) -> IMAPClient:
        return self.server

    def __exit__(self, exc_type, exc_value, exc_traceback) -> None:
        try:
            self.server.logout()
        except IMAPExceptions.IMAPClientError:
            self.server.shutdown()  #try this if above fails for some reason
Example #10
0
    def connect(self):
        kwargs = {}

        if self._disable_cert_check:
            ssl_context = ssl.create_default_context()

            # don't check if certificate hostname doesn't match target hostname
            ssl_context.check_hostname = False

            # don't check if the certificate is trusted by a certificate authority
            ssl_context.verify_mode = ssl.CERT_NONE

            kwargs['ssl_context'] = ssl_context

        client = IMAPClient(
            self.uri.location,
            self.uri.port if self.uri.port else None,
            use_uid=True,
            ssl=self.uri.use_ssl,
            **kwargs)

        if self.uri.use_tls:
            client.starttls()

        # TODO: Check for condstore and enable
        # if client.has_capability('ENABLE') and client.has_capability('CONDSTORE'):
        #       client.enable('CONDSTORE')
        #       condstore_enabled = True

        client.login(self.uri.username, self.uri.password)
        return client
Example #11
0
def connect_imap(logger, mail: str, password: str) -> IMAPClient:
    """
    prepare the connection to receive mails

    :param logger: logging object
    :param mail: string for mail, e.g.
    :param password: string for password (not printed to log)
    :return: connection object
    """

    host = mail.split("@")[1]
    try:
        imap_domain = imap_provider[host]
    except KeyError:
        logger.error(f"IMAP-Provider {host} is unknown, exiting")
        # TODO try imap.XX before giving up
        exit(2)

    logger.info(f"Connecting to {imap_domain}")
    imap_obj = IMAPClient(imap_domain, ssl=True)

    logger.info(f"Logging in")

    response = imap_obj.login(mail, password)
    logger.debug(response)

    logger.info(f"Successful. Ready to receive")

    return imap_obj
Example #12
0
def accessEmail(username,password):
	HOST = "pod51009.outlook.com"
	ssl = False
	server = IMAPClient(HOST, use_uid = True, ssl = False)
	server.login(username,password)

	select_info = server.select_folder("INBOX")
Example #13
0
def connect_account(account, host):
    """Provide a connection to a generic IMAP account.

    Raises
    ------
    ConnectionError
        If we cannot connect to the IMAP host.
    ValidationError
        If the credentials are invalid.
    """
    try:
        conn = IMAPClient(host, use_uid=True, ssl=True)
    except IMAPClient.Error as e:
        log.error('account_connect_failed',
                  host=host,
                  error="[ALERT] Can't connect to host (Failure)")
        raise ConnectionError(str(e))

    conn.debug = False
    try:
        conn.login(account.email_address, account.password)
    except IMAPClient.Error as e:
        log.error('account_verify_failed',
                  email=account.email_address,
                  host=host,
                  password=account.password,
                  error="[ALERT] Invalid credentials (Failure)")
        raise ValidationError()

    return conn
Example #14
0
    def _send_request(
        self,
        mail: DecodedMail,
        imapc: imapclient.IMAPClient,
        method: str,
        endpoint: str,
        json_body_dict: dict,
    ):
        logger.debug(
            self,
            "Contact API on {endpoint} with method {method} with body {body}".format(
                endpoint=endpoint, method=method, body=str(json_body_dict)
            ),
        )
        if method == "POST":
            request_method = requests.post
        else:
            # TODO - G.M - 2018-08-24 - Better handling exception
            raise UnsupportedRequestMethod("Request method not supported")

        r = request_method(
            url=endpoint,
            json=json_body_dict,
            headers=self._get_auth_headers(mail.get_from_address()),
        )
        if r.status_code not in [200, 204]:
            details = r.json().get("message")
            msg = "bad status code {} (200 and 204 are valid) response when sending mail to tracim: {}"
            msg = msg.format(str(r.status_code), details)
            raise BadStatusCode(msg)
        # Flag all correctly checked mail
        if r.status_code in [200, 204]:
            imapc.add_flags((mail.uid,), IMAP_CHECKED_FLAG)
            imapc.add_flags((mail.uid,), IMAP_SEEN_FLAG)
Example #15
0
    def connect(self) -> bool:
        """
        Connect and login to the remote IMAP server.

        Returns:
            (bool): True if successful, otherwise False
        """
        mailserver = os.environ.get('mailserver')
        imapport = os.environ.get('imapport')
        mailssl = os.environ.get('mailssl')
        try:
            self.server = IMAPClient(
                mailserver,
                port=imapport,
                ssl=mailssl,
                use_uid=True
            )
            username = os.environ.get('mail_username')
            password = os.environ.get('mail_password')
            self.logger.debug(f"Username: {username}, Password: {password}")
            self.server.login(username, password)
        except ConnectionRefusedError as e:
            self.logger.fatal(f"Connection to {mailserver}:{imapport} was refused: {str(e)}")
            return False
        except Exception as e:
            self.logger.fatal(f"Connection to {mailserver}:{imapport} was refused: {str(e)}")
            return False

        return True
    def __connect(self):
        if self.__fix_weak_dh:
            context = ssl.SSLContext(
                ssl.PROTOCOL_TLSv1_2)  # Workaround for weak dh key
            context.set_ciphers('DEFAULT@SECLEVEL=1')
        else:
            context = ssl.SSLContext()

        try:
            if self.isDebug():
                self.print("Connecting to server {}".format(self.__server))
            self.__imap_client = IMAPClient(self.__server,
                                            use_uid=True,
                                            ssl=self.__ssl,
                                            ssl_context=context,
                                            timeout=1.0)
        except gaierror:
            self.error("Failed to connect to Mail Server")
        except ssl.SSLError:
            self.fatal("Failed to connect to Mail Server (TLS Error)")
        else:
            try:
                if self.isDebug():
                    self.print("Login as user {}".format(self.__user))
                self.__imap_client.login(self.__user, self.__password)
            except LoginError:
                self.error("Mail Server login failed")
            else:
                if self.isDebug():
                    self.print("Login successful")
                self.__healthy = True
                self.__imap_client.select_folder('INBOX', readonly=False)
Example #17
0
    def reset(self):

        try:
            self._client.logout()
        except IMAPClientError as e:
            pass
        except OSError as e:
            pass

        try:
            self._client = IMAPClient(
                host=self._hote_imap,
                port=993 if self._use_secure_socket else 143,
                ssl=self._use_secure_socket,
                ssl_context=self._ssl_context)
            self._client.login(
                self._nom_utilisateur,
                Session.UNIVERSELLE.retranscrire(self._mot_de_passe))
        except IMAPClientError as e:
            logger.error(
                "Une erreur IMAP critique est survenue lors de la reconnexion. {msg_err}",
                msg_err=str(e))
            self._echec = True
            return

        self._echec = False
Example #18
0
    def connect_account(self, email, pw, imap_endpoint, account_id=None):
        """Provide a connection to a IMAP account.

        Raises
        ------
        socket.error
            If we cannot connect to the IMAP host.
        IMAPClient.error
            If the credentials are invalid.
        """
        host, port = imap_endpoint
        try:
            conn = IMAPClient(host, port=port, use_uid=True, ssl=True)
        except IMAPClient.AbortError as e:
            log.error('account_connect_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error="[ALERT] Can't connect to host - may be transient")
            raise TransientConnectionError(str(e))
        except (IMAPClient.Error, gaierror, socket_error) as e:
            log.error('account_connect_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] (Failure): {0}'.format(str(e)))
            raise ConnectionError(str(e))

        conn.debug = False
        try:
            conn.oauth2_login(email, pw)
        except IMAPClient.AbortError as e:
            log.error('account_verify_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error="[ALERT] Can't connect to host - may be transient")
            raise TransientConnectionError(str(e))
        except IMAPClient.Error as e:
            log.error('IMAP Login error during connection. '
                      'Account: {}, error: {}'.format(email, e),
                      account_id=account_id)
            if (str(e) == '[ALERT] Invalid credentials (Failure)'
                    or str(e).startswith('[AUTHENTICATIONFAILED]')):
                raise ValidationError(str(e))
            else:
                raise ConnectionError(str(e))
        except SSLError as e:
            log.error('account_verify_failed',
                      account_id=account_id,
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] (Failure) SSL Connection error')
            raise ConnectionError(str(e))

        return conn
Example #19
0
    def __init__(self, host, username, password, debug=False):
        """
        Server class __init__ which expects an IMAP host to connect to

        @param host: gmail's default server is fine: imap.gmail.com
        @param username: your gmail account (i.e. [email protected])
        @param password: we highly recommend you to use 2-factor auth here
        """

        if not host:
            raise Exception('Missing IMAP host parameter in your config')

        try:
            self._server = IMAPClient(host, use_uid=True, ssl=True)
        except:
            raise Exception('Could not successfully connect to the IMAP host')

        setattr(self._server, 'debug', debug)

        # mails index to avoid unnecessary redownloading
        index = '.index_%s' % (username)
        index = os.path.join(_app_folder(), index)
        self._index = shelve.open(index, writeback=True)

        # list of attachments hashes to avoid dupes
        hashes = '.hashes_%s' % (username)
        hashes = os.path.join(_app_folder(), hashes)
        self._hashes = shelve.open(hashes, writeback=True)

        self._username = username
        self._login(username, password)
Example #20
0
def loop():
    server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
    server.login(USERNAME, PASSWORD)

    if DEBUG:
        print('Logging in as ' + USERNAME)
        select_info = server.select_folder(MAILBOX)
        print('%d messages in INBOX' % select_info['EXISTS'])

    folder_status = server.folder_status(MAILBOX, 'UNSEEN')
    newmails = int(folder_status['UNSEEN'])

    if newmails >= 1:
        print "Exist new mail!!!!"
      	subprocess.call('bash /home/pi/alarm/ShellScript/checkEmail.sh', shell=True)

    if DEBUG:
        print "You have", newmails, "new emails!"

    if newmails > NEWMAIL_OFFSET:
        print "test1"

    else:
        print "test2"
    time.sleep(MAIL_CHECK_FREQ)
Example #21
0
def connect_imap_and_sync(from_host,
                          to_host,
                          last_state,
                          state_config_filename,
                          idle_timeout=240):  # TODO wait more?
    with IMAPClient(from_host['host'], port=int(from_host['port'])) as from_server,\
            IMAPClient(to_host['host'], port=int(to_host['port'])) as to_server:
        from_server.login(from_host['username'], from_host['password'])
        to_server.login(to_host['username'], to_host['password'])
        # from_server.capabilities()
        from_server.select_folder('INBOX', readonly=True)
        # to_server.capabilities()
        to_server.select_folder('INBOX')
        while True:
            copy_emails(from_server, to_server, last_state,
                        to_host['target_label'], state_config_filename)
            responses = []
            while len(responses) == 0:
                """
                Note that IMAPClient does not handle low-level socket errors that can happen 
                when maintaining long-lived TCP connections.
                Users are advised to renew the IDLE command every 10 minutes to avoid the connection
                from being abruptly closed.
                """
                from_server.idle()  # Start IDLE mode
                responses = from_server.idle_check(
                    timeout=idle_timeout
                )  # Wait max idle_timeout seconds for response
                from_server.idle_done()  # Must finish idle to send Keepalive
                from_server.noop()  # Keepalive for source
                to_server.noop()  # Keepalive for target
Example #22
0
 def connect(self, host, username, password):
     '''Connects to the IMAP server and returns a new connection.
     The inbox is selected by default'''
     server = IMAPClient(host, use_uid=self.use_uid, ssl=self.useSSL)
     server.login(username, password)
     server.select_folder('INBOX')
     return server
Example #23
0
    def __init__(self,
                 username,
                 password='******',
                 host='localhost',
                 port=143,
                 *args,
                 **kwargs):
        super().__init__(username, *args, **kwargs)
        self.imap = IMAPClient(host, port, use_uid=True, ssl=False)
        res = self.imap.login(username, password)
        self.cursor.execute(
            "SELECT lowModSeq,highModSeq,highModSeqMailbox,highModSeqThread,highModSeqEmail FROM account LIMIT 1"
        )
        row = self.cursor.fetchone()
        self.lastfoldersync = 0
        if row:
            self.lowModSeq,
            self.highModSeq,
            self.highModSeqMailbox,
            self.highModSeqThread,
            self.highModSeqEmail = row
        else:
            self.lowModSeq = 0
            self.highModSeq = 1
            self.highModSeqMailbox = 1
            self.highModSeqThread = 1
            self.highModSeqEmail = 1

        # (imapname, readonly)
        self.selected_folder = (None, False)
        self.mailboxes = {}
        self.sync_mailboxes()
        self.messages = {}
Example #24
0
def create_imap_connection(host, port, ssl_required, use_timeout=True):
    """
    Return a connection to the IMAP server.
    The connection is encrypted if the specified port is the default IMAP
    SSL port (993) or the server supports STARTTLS.
    IFF neither condition is met and SSL is not required, an insecure connection
    is returned. Otherwise, an exception is raised.

    """
    use_ssl = port == 993
    timeout = 120 if use_timeout else None

    # TODO: certificate pinning for well known sites
    context = create_default_context()
    conn = IMAPClient(host, port=port, use_uid=True,
                      ssl=use_ssl, ssl_context=context, timeout=timeout)

    if not use_ssl:
        # If STARTTLS is available, always use it. If it's not/ it fails, use
        # `ssl_required` to determine whether to fail or continue with
        # plaintext authentication.
        if conn.has_capability('STARTTLS'):
            try:
                conn.starttls(context)
            except Exception:
                if not ssl_required:
                    log.warning('STARTTLS supported but failed for SSL NOT '
                                'required authentication', exc_info=True)
                else:
                    raise
        elif ssl_required:
            raise SSLNotSupportedError('Required IMAP STARTTLS not supported.')

    return conn
Example #25
0
def create_imap_connection(host, port, ssl_required):
    """
    Return a connection to the IMAP server.
    The connection is encrypted if the specified port is the default IMAP
    SSL port (993) or the server supports STARTTLS.
    IFF neither condition is met and SSL is not required, an insecure connection
    is returned. Otherwise, an exception is raised.

    """
    use_ssl = port == 993

    # TODO: certificate pinning for well known sites
    context = create_default_context()
    conn = IMAPClient(host, port=port, use_uid=True,
                      ssl=use_ssl, ssl_context=context, timeout=120)

    if not use_ssl:
        # If STARTTLS is available, always use it. If it's not/ it fails, use
        # `ssl_required` to determine whether to fail or continue with
        # plaintext authentication.
        if conn.has_capability('STARTTLS'):
            try:
                conn.starttls(context)
            except Exception:
                if not ssl_required:
                    log.warning('STARTTLS supported but failed for SSL NOT '
                                'required authentication', exc_info=True)
                else:
                    raise
        elif ssl_required:
            raise SSLNotSupportedError('Required IMAP STARTTLS not supported.')

    return conn
Example #26
0
    def _fetch(self, imapc: imapclient.IMAPClient) -> typing.List[MessageContainer]:
        """
        Get news message from mailbox
        :return: list of new mails
        """
        messages = []

        logger.debug(self, "Fetch unflagged messages")
        uids = imapc.search(["UNFLAGGED"])
        logger.debug(self, "Found {} unflagged mails".format(len(uids)))
        for msgid, data in imapc.fetch(uids, ["BODY.PEEK[]"]).items():
            # INFO - G.M - 2017-12-08 - Fetch BODY.PEEK[]
            # Retrieve all mail(body and header) but don't set mail
            # as seen because of PEEK
            # see rfc3501
            logger.debug(self, 'Fetch mail "{}"'.format(msgid))

            try:
                msg = message_from_bytes(data[b"BODY[]"])
            except KeyError as e:
                # INFO - G.M - 12-01-2018 - Fetch may return events response
                # In some specific case, fetch command may return events
                # response unrelated to fetch request.
                # This should happen only when someone-else use the mailbox
                # at the same time of the fetcher.
                # see https://github.com/mjs/imapclient/issues/334
                except_msg = "fetch response : {}".format(str(data))
                raise BadIMAPFetchResponse(except_msg) from e

            msg_container = MessageContainer(msg, msgid)
            messages.append(msg_container)

        return messages
Example #27
0
    def authenticate(self, username=None, password=None, host=None):

        if host:
            try:
                host_addr = host.address
                host_port = host.port
                host_ssl = host.ssl
            except:
                pass
        else:
            host_addr = settings.IMAP_DEFAULT_HOST
            host_port = settings.IMAP_DEFAULT_PORT
            host_ssl = settings.IMAP_DEFAULT_SSL

        try:
            server = IMAPClient(
                host_addr,
                port=host_port,
                ssl=host_ssl,
            )
            server.login(username, password)
            server.logout()
        except:
            return None

        try:
            user = User.objects.get(email=username)
        except User.DoesNotExist:
            user = User.objects.create_user(
                time.time(),
                username,
                'password-not-in-use',
            )

        return user
Example #28
0
 def login(self, server, username, password, use_ssl=False, **kwargs):
     try:
         self.server = IMAPClient(server, use_uid=True, ssl=use_ssl)
         self.server.login(username, password)
     except Exception as e:
         return (False, e.message)
     return (True, 'Login Successful')
Example #29
0
    def connect_account(self, provider, email, credential):
        """Provide a connection to a generic IMAP account.

        Raises
        ------
        ConnectionError
            If we cannot connect to the IMAP host.
        TransientConnectionError
            Sometimes the server bails out on us. Retrying may
            fix things.
        ValidationError
            If the credentials are invalid.
        """

        info = provider_info(provider, email)
        host, port = info['imap']
        try:
            conn = IMAPClient(host, port=port, use_uid=True, ssl=True)
        except IMAPClient.AbortError as e:
            log.error('account_connect_failed',
                      email=email,
                      host=host,
                      port=port,
                      error="[ALERT] Can't connect to host - may be transient")
            raise TransientConnectionError(str(e))
        except(IMAPClient.Error, gaierror, socket_error) as e:
            log.error('account_connect_failed',
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] (Failure): {0}'.format(str(e)))
            raise ConnectionError(str(e))

        conn.debug = False
        try:
            conn.login(email, credential)
        except IMAPClient.AbortError as e:
            log.error('account_verify_failed',
                      email=email,
                      host=host,
                      port=port,
                      error="[ALERT] Can't connect to host - may be transient")
            raise TransientConnectionError(str(e))
        except IMAPClient.Error as e:
            log.error('account_verify_failed',
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] Invalid credentials (Failure)')
            raise ValidationError(str(e))
        except SSLError as e:
            log.error('account_verify_failed',
                      email=email,
                      host=host,
                      port=port,
                      error='[ALERT] SSL Connection error (Failure)')
            raise ConnectionError(str(e))

        return conn
Example #30
0
File: main.py Project: IOEPAS/zippy
def create_folder_if_not_exists(client: IMAPClient, folder: str,
                                logger: logging.Logger):
    """Create folder if it already exists."""
    try:
        client.create_folder(folder)
    except IMAPClientError:
        # most likely, it already exists
        logger.info("Looks like the folder %s already exists.", folder)
Example #31
0
def delete_unsub_email(host, sender, password, to):
    server = IMAPClient(host)
    server.login(sender, password)
    sent_folder = server.find_special_folder(imapclient.SENT)
    server.select_folder(sent_folder)
    messages = server.search(['TO', to])
    server.delete_messages(messages)
    server.logout
def login():
    """
    Log in to the mailbox we will read from.
    """
    imap = IMAPClient(settings.MAILREADER_HOST, use_uid=True)
    imap.login(settings.MAILREADER_HOST_USER, settings.MAILREADER_HOST_PASSWORD)
    imap.select_folder('INBOX')
    return imap
Example #33
0
    def login(self):
        self.incoming_server = IMAPClient(self.host, use_uid=True, ssl=True)
        self.incoming_server.login(self.username, self.pwd)
        self.incoming_server.select_folder("INBOX")

        self.outgoing_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        self.outgoing_server.ehlo()
        self.outgoing_server.login(self.username, self.pwd)
Example #34
0
def test_flag_happy_path(logged_in_client: IMAPClient, random_mail, logger,
                         caplog):
    mark_processed(logged_in_client, random_mail, logger)

    assert random_mail in logged_in_client.get_flags(random_mail)
    assert FLAG_TO_CHECK in logged_in_client.get_flags(random_mail).get(
        random_mail)
    assert f"Flag added to {random_mail}\n" in caplog.text
Example #35
0
def test_create_folder_if_not_exists_already_exists(
        logged_in_client: IMAPClient, logger, caplog):
    assert logged_in_client.folder_exists(EmailFolders.INBOX)

    create_folder_if_not_exists(logged_in_client, EmailFolders.INBOX, logger)

    assert logged_in_client.folder_exists(EmailFolders.INBOX)
    assert "Looks like the folder INBOX already exists." in caplog.text
def initialize():
    global g_server_objects
    #global g_inbox_folder
    for i in range(THREAD_COUNT):
        server = IMAPClient(HOST)
        server.login(USERNAME, PASSWORD)
        inbox_folder = server.select_folder('INBOX')
        g_server_objects.append(server)
Example #37
0
def flagged_random_mail(logged_in_client: IMAPClient, random_mail):
    logged_in_client.select_folder(EmailFolders.INBOX)
    logged_in_client.add_flags(random_mail, FLAG_TO_CHECK)
    assert FLAG_TO_CHECK in logged_in_client.get_flags(random_mail).get(
        random_mail)

    # no need to cleanup, as the message will get deleted anyway
    return random_mail
Example #38
0
def list_boxes(account):
    c = ACCOUNTS[account]
    if 'pass_cmd' in c:
        c['pass'] = check_output([c['pass_cmd']], shell=True).strip()
    client = IMAPClient(c['host'], use_uid=True, ssl=c['ssl'])
    client.login(c['user'], c['pass'])
    from pprint import pprint
    pprint(client.list_folders())
Example #39
0
def main():
    global imap_server
    imap_server = IMAPClient(GMAIL_IMAP_SERVER, use_uid=True, ssl=True)
    imap_server.login(GMAIL_ADDR, PASSWORD)

    messages = get_matching_messages()
    label_as_in_process(messages)
    process_messages(messages)
def main():
	global imap_server
	imap_server = IMAPClient(GMAIL_IMAP_SERVER, use_uid=True, ssl=True)
	imap_server.login(GMAIL_ADDR, PASSWORD)
	
	messages = get_matching_messages()
	label_as_in_process(messages)
	process_messages(messages)
def connect_imap(imapHost, imapUsername):
  imapObj = IMAPClient(imapHost, use_uid=True, ssl=True)
  try:
    imapObj.login(imapUsername, getpass.getpass())
    print("Connecting to mailbox via IMAP...")
    return imapObj
  except imapObj.Error:
    print("Login failed!!!")
    sys.exit(1)
 def clear_email_for_address(self, email_address, content_filter=None):
     from imapclient import IMAPClient
     server = IMAPClient(IMAP_HOST, ssl=True)
     messages_to_delete = []
     for m_id, parsed_headers, body_text in self.all_emails(server):
         if email_address in parsed_headers['To']:
             if content_filter is None or content_filter in body_text:
                 messages_to_delete.append(m_id)
     server.delete_messages(messages_to_delete)
Example #43
0
def new_imap_connection(log, host, username, password, port=143, legacy_ssl=False):
    log.debug("Establishing IMAP connection", host=host, port=port, ssl=legacy_ssl, username=username)
    c = IMAPClient(host, port=port, use_uid=True, ssl=legacy_ssl)
    if not legacy_ssl:
        log.debug("Upgrading to TLS")
        c.starttls()
    log.debug("Logging in")
    c.login(username, password)
    log.debug("Successfully connected to IMAP server")
    return c
Example #44
0
def get_email(user):
    server = IMAPClient(HOST, use_uid=True, ssl=True)
    username = db.get("user:%s:login" % user)
    password = db.get("user:%s:password" % user)
    server.login(username, password)
    server.select_folder('INBOX', readonly=True)
    messages = server.search(['NOT DELETED','SINCE 15-May-2014'])
    response = server.fetch(messages, ['RFC822', 'FLAGS'])
    for msgid, data in response.iteritems():
        # check for duplicates
        duplicate = db.zrangebyscore("mail:%s:inbox" % user, msgid, msgid)
        if duplicate:
            continue
        emailUTF8 = data['RFC822'].encode('utf-8')
        msg = parser.parsestr(emailUTF8)
        body = extract_body(msg).encode('utf-8')
        msg['message'] = body
        msg['subject'] = ('NoSubj' if (msg['Subject'] == None or msg['Subject'].encode('utf-8') == "".encode('utf-8'))  else msg['Subject'])
        msg['to'] = ('NoTo' if (msg['To'] == None) else msg['To'])
        plain = {'plain_body': extract_body_text(msg).encode('utf-8'), 'subject': msg['subject']}
        # TODO set unread
        email = {'id': msgid, 'from': msg['From'], 'to': msg['To'], 'subject': msg['Subject'],
                'date': msg['Date'], 'cc': msg['CC'], 'read': False,
                'message': body, 'categorized': False, 'summary': shorten(plain), 'archived': False}

        trained = db.get("user:%s:trained" % user)
        if trained == "true":
            email['category'] = int(classify(msg, user))
        else:
            email['category'] = 1
        emailJSON = json.dumps(email, sort_keys=True, indent=4, separators=(',', ': '))
        db.zadd("mail:%s:inbox" % user, emailJSON, msgid)
        db.sadd("mail:%s:%s" % (user, email['category']), msgid)
    server.logout()
Example #45
0
def create_imap_connection(host, port):
    use_ssl = port == 993

    # TODO: certificate pinning for well known sites
    context = create_default_context()

    conn = IMAPClient(host, port=port, use_uid=True, ssl=use_ssl, ssl_context=context, timeout=120)
    if not use_ssl:
        # Raises an exception if TLS can't be established
        conn.starttls(context)
    return conn
class Base(object):
    def __init__(self, conf):
        self.username = conf['USERNAME']
        self.host = conf['HOST']
        self.server = IMAPClient(conf['HOST'], use_uid=True, ssl=conf['SSL'])
        self.server.login(conf['USERNAME'], conf['PASSWORD'])

    def __str__(self):
        return "<user: %s | host: %s>" % (self.username, self.host)

    def folder_separator(self):
        return self.server.namespace()[0][0][1]
Example #47
0
def create_imap_connection(host, port):
    use_ssl = port == 993

    # TODO: certificate pinning for well known sites
    context = create_default_context()
    context.check_hostname = False
    context.verify_mode = ssl.CERT_NONE

    conn = IMAPClient(host, port=port, use_uid=True,
                      ssl=use_ssl, ssl_context=context)
    if not use_ssl:
        # Raises an exception if TLS can't be established
        conn.starttls(context)
    return conn
Example #48
0
 def gmail_search(self, query):
     """Search the gmail imap server using gmail queries"""
     self.client.logout()
     self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
     self._login()
     # Can use full gmail queries like 'has:attachment in:unread'
     messages = self.client.gmail_search(query)
     self.messages_for_this_session.append(messages)
     # We recreate a whole connection after querying gmail because
     # otherwise it caches our search results forever
     self.client.logout()
     self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
     self._login()
     return self.emails_from_messages(messages)
Example #49
0
def imapclient_test(login, password):
    
    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(login, password)
    
    capabilities = server.capabilities()
    
    #print("list folders = %s\n" %(server.list_folders()))
       
    select_info = server.select_folder('[Gmail]/All Mail')
    
    #print '%d messages in INBOX' % select_info['EXISTS']
      
    messages = server.search(['NOT DELETED'])
    print "%d messages in [Gmail]/Inbox" % len(messages)
    
    for msg in messages[:20]:
        print("msg uid = %d\n" %(msg))
    
     
    sys.exit()  
    select_info = server.select_folder('[Gmail]/Sent Mail')
    
    #print '%d messages in INBOX' % select_info['EXISTS']
      
    messages = server.search(['NOT DELETED'])
    print "%d messages in [Gmail]/Sent Mail" % len(messages)   
Example #50
0
class IMAPService(object):

    def __init__(self, host, port=None):

        self.service = IMAPClient(host, port=port, ssl=True)

    def auth(self, usr, pwd):

        self.service.login(usr, pwd)

    def list(self):

        self.service.select_folder('INBOX')

        messages = self.service.search(['UNSEEN'])

        response = self.service.fetch(messages, ['RFC822'])

        for msg_id, data in response.iteritems():

            yield (msg_id, email.message_from_string(
                data['RFC822'].encode("utf-8", "ignore")))

    def quit(self):

        pass

    def mark_unread(self, msg_ids):

        self.service.remove_flags(msg_ids, [SEEN])
Example #51
0
def loop():
    server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
    server.login(USERNAME, PASSWORD)

    if DEBUG:
        print('Logging in as ' + USERNAME)
        select_info = server.select_folder(MAILBOX)
        print('%d messages in INBOX' % select_info['EXISTS'])

    folder_status = server.folder_status(MAILBOX, 'UNSEEN')
    newmails = int(folder_status['UNSEEN'])

    if DEBUG:
        print "tienes ", newmails, " correos nuevos en tu bandeja"
Example #52
0
def verify_account(account):
    try:
        conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True)
    except IMAPClient.Error as e:
        raise socket.error(str(e))

    conn.debug = False
    try:
        conn.login(account.email_address, account.password)
    except IMAPClient.Error as e:
        log.error('IMAP connection error: {}'.format(e))
        raise

    return conn
Example #53
0
File: yahoo.py Project: caitp/inbox
def verify_yahoo_account(account):
    try:
        conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True)
    except IMAPClient.Error as e:
        raise socket.error(str(e))

    conn.debug = False
    try:
        conn.login(account.email_address, account.password)
    except IMAPClient.Error as e:
        print >>sys.stderr, '[ALERT] Invalid credentials (Failure)'
        sys.exit(1)

    return conn
Example #54
0
def verify_account(account):
    try:
        conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True)
    except IMAPClient.Error as e:
        raise socket.error(str(e))

    conn.debug = False
    try:
        conn.login(account.email_address, account.password)
    except IMAPClient.Error as e:
        log.error('yahoo_account_verify_failed',
                  error='[ALERT] Invalid credentials (Failure)')
        raise

    return conn
def main():
    if len(sys.argv) != 4:
        print('usage: %s hostname username foldername' % sys.argv[0])
        sys.exit(2)

    hostname, username, foldername = sys.argv[1:]
    c = IMAPClient(hostname, ssl=True)
    try:
        c.login(username, getpass.getpass())
    except c.Error as e:
        print('Could not log in:', e)
    else:
        print_summary(c, foldername)
    finally:
        c.logout()
Example #56
0
def main():
        
    def checkConfigs():
        '''check for config file and define variables'''
        config = os.path.join(CUCKOO_ROOT,"cuckooinbox","cuckooinbox.conf")
        if not os.path.exists(config):
            raise CuckooStartupError("Config file does not exist at path: %s" % config)
    
    checkConfigs()
    config = Config(cfg=os.path.join(CUCKOO_ROOT,"cuckooinbox","cuckooinbox.conf"))
    config = config.get('cuckooinbox')
    username = config['username']
    passwd = config['passwd']
    imap = config['imap']
    imap_ssl = config['imap_ssl']
    email_whitelist = config['email_whitelist']
    interval = config['interval']
    
    '''welcome screen'''
    print '\n\n'
    print '\t\t@\tsend your malware to %s !\n' % (username)
    welcome_message = '           _,\n         ((\')\n        /\'--)\n        | _.\'\n       / |`=\n      \'^\''
    print welcome_message

            
    def analyze(message):
        request = CuckooRequest(message)
        request.fetch(message)
        request.sendReport()
    
    '''define imap connection'''
    server = IMAPClient(imap, use_uid=True, ssl=imap_ssl)
    
    '''connect, login'''
    server.login(username, passwd)

    while True:
        '''set retrieve folder'''
        select_info = server.select_folder('INBOX')
        '''search for new message from email whitelist'''
        for account in email_whitelist.split(','):
            messages = server.search('UNSEEN FROM "%s"' % account)
            '''analyze emails from one account at a time'''
            if messages:
                for message in messages:
                    thread = threading.Thread( target = analyze, args = (message,))
                    thread.start()
        time.sleep(interval)
Example #57
0
def connect_account(account):
    """Provide a connection to a generic IMAP account.

    Raises
    ------
    ConnectionError
        If we cannot connect to the IMAP host.
    ValidationError
        If the credentials are invalid.
    """

    info = provider_info(account.provider)
    host = info["imap"]
    try:
        conn = IMAPClient(host, use_uid=True, ssl=True)
    except IMAPClient.Error as e:
        log.error('account_connect_failed',
                  host=host,
                  error="[ALERT] Can't connect to host (Failure)")
        raise ConnectionError(str(e))
    except gaierror as e:
        log.error('account_connect_failed',
                  host=host,
                  error="[ALERT] Name resolution faiure (Failure)")
        raise ConnectionError(str(e))
    except socket_error as e:
        log.error('account_connect_failed',
                  host=host,
                  error="[ALERT] Socket connection failure (Failure)")
        raise ConnectionError(str(e))

    conn.debug = False
    try:
        conn.login(account.email_address, account.password)
    except IMAPClient.Error as e:
        log.error('account_verify_failed',
                  email=account.email_address,
                  host=host,
                  error="[ALERT] Invalid credentials (Failure)")
        raise ValidationError()
    except SSLError as e:
        log.error('account_verify_failed',
                  email=account.email_address,
                  host=host,
                  error="[ALERT] SSL Connection error (Failure)")
        raise ConnectionError(str(e))

    return conn
Example #58
0
    def __init__(self, host, username, password, debug=False):
        """
        Server class __init__ which expects an IMAP host to connect to

        @param host: gmail's default server is fine: imap.gmail.com
        @param username: your gmail account (i.e. [email protected])
        @param password: we highly recommend you to use 2-factor auth here
        """

        if not host:
            raise Exception('Missing IMAP host parameter in your config')

        try:
            self._server = IMAPClient(host, use_uid=True, ssl=True)
        except:
            raise Exception('Could not successfully connect to the IMAP host')

        setattr(self._server, 'debug', debug)

        # mails index to avoid unnecessary redownloading
        index = '.index_%s' % (username)
        index = os.path.join(_app_folder(), index)
        self._index = shelve.open(index, writeback=True)

        # list of attachments hashes to avoid dupes
        hashes = '.hashes_%s' % (username)
        hashes = os.path.join(_app_folder(), hashes)
        self._hashes = shelve.open(hashes, writeback=True)

        self._username = username
        self._login(username, password)
Example #59
-1
def test_imap(user):
    credentials = decorator.credentials
    if credentials.access_token_expired:
        logging.debug('Refreshing...')
        credentials.refresh(httplib2.Http())
    conn = IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
    conn.debug = 4

    conn.oauth2_login(user, credentials.access_token)

    # status, labels = conn.list()
    folders = conn.list_folders()
    try:
        all_box = next(box for (flags, _, box) in folders if '\All' in flags)
    except StopIteration:
        raise Error('all message box not found')

    logging.debug('All message box is {}'.format(all_box))

    conn.select_folder(all_box)
    # Once authenticated everything from the impalib.IMAP4_SSL class will
    # work as per usual without any modification to your code.
    # typ, msgnums = conn.search('X-GM-RAW vget')
    tid = int('14095f27c538b207', 16)
    # msgs = conn.search('X-GM-THRID {}'.format(tid))
    msgs = conn.search('X-GM-RAW uniquetokenXXX')

    logging.info(msgs)
    logging.info(conn.fetch(msgs, 'X-GM-MSGID'))
    msg = conn.fetch(msgs, 'RFC822')
    logging.info(msg)
    return msg