コード例 #1
0
    msgs = [imap.fetch(n, '(UID BODY[TEXT])') for n in data[0].split()[-10:]]
    msgs = [msg[1][0][1].decode('utf-8') for msg in msgs]
    return any(rmsg for rmsg in msgs if msg in rmsg)


print_log("[Firewall] Port  25 (SMTP) open")
print_crit_check(is_port_open(server, 25))

print_log("[Firewall] Port 143 (IMAP) open")
print_crit_check(is_port_open(server, 143))
print()


print_log("[IMAP] Connection possible")
try:
    imap = IMAP4(host=server)
    print_check('OK' in imap.noop())
except IMAP4.error:
    print_crit_check(False)

print_log("[IMAP] STARTTLS successful")
print_crit_check('OK' in imap.starttls(ssl_context=context))

print_log("[IMAP] Successful authentication")
print_crit_check('OK' in imap.login(user, pw))

print_log("[IMAP] Opening INBOX")
print_crit_check('OK' in imap.select())

print_log("[IMAP] Can read e-mails")
secret = b'This is a test mailing\r\n\r\n'
コード例 #2
0
ファイル: imap.py プロジェクト: yktakaha4/email-newbie
from src.email import Email
from imaplib import IMAP4
from email import message_from_string
from email.header import decode_header, make_header
from typing import List
from base64 import urlsafe_b64decode
from os.path import dirname

IMAP_HOSTNAME = "localhost"
IMAP_PORT = 143

imap4 = IMAP4(host=IMAP_HOSTNAME, port=IMAP_PORT)


def receive_email(
    user: str,
    password: str,
):
    imap4.login(user=user, password=password)

    imap4.select()
    _, data = imap4.search(None, "ALL")

    emails: List[Email] = []

    for num in data[0].split():
        _, data = imap4.fetch(message_set=num, message_parts="(RFC822)")
        mail = message_from_string(data[0][1].decode("utf-8"))

        from_address = mail["From"]
        to_addresses = mail["To"]
コード例 #3
0
def fetch_report_imap(imap_host, imap_port, imap_user, imap_pass, imap_folder, done_folder, use_starttls, use_tls):

	skip = False

	if( (use_tls == True) and (use_starttls == True) ):
		print("'use_tls' and 'use_starttls' are mutually exclusive, please update your configuration")
		sys.exit(1)

	try:
		M = IMAP4(imap_host, imap_port)
		#M.debug = 3

		if(use_starttls == True):
			M.starttls()
		elif(use_tls == True):
			#M.ssl
			pass
		else:
			pass

	except Exception as e:
		print("Error: %s" % (e))
		function.log(logfile,"ERR", "Error connecting to IMAP host: %s %s" % (imap_host, e))
		sys.exit(1)

	try:
		M.login(imap_user, imap_pass)

	except Exception as e:
		print("Error authenticating against IMAP host: %s %s" % (imap_host, e))
		function.log(logfile,"ERR", "Error authenticating against IMAP host: %s %s" % (imap_host, e))
		M.close()
		sys.exit(1)

	try:
		ret = M.select(done_folder)[0]
		if(ret == 'NO'):
			print("Creating Folder " + done_folder + " ...")
			M.create(done_folder)
	except:
		pass

	M.select(imap_folder)

	typ, msgnums = M.search(None, 'ALL')
	#print(typ, msgnums)

	for num in msgnums[0].split():

		typ, data = M.fetch(num, '(BODY[HEADER.FIELDS (DATE)])')
		line = (str(data[0][1]))

		date		= 	re.search("Date:\s(\w*,?\s*\w*\s*\w*\s*\w*\s*\w*:\w*:\w*)", line)
		if(date is not None):
			if(debug == True):
				print(date.group(1))

		typ, data = M.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT)])')
		line = (str(data[0][1]))

		submitter     =	re.search("Submitter:\s(\w*\.\w*)", line)
		if(submitter is not None):
			if(debug == True):
				print(submitter.group(1))

		report_id     =	re.search("Report-ID:\s(<?\w*\.?\w*>?)", line)
		if(report_id is not None):
			if(debug == True):
				print(report_id.group(1))

		report_domain     =	re.search("Report domain:\s(\w*\.\w*)", line)
		if(report_domain is not None):
			if(debug == True):
				print(report_domain.group(1))

		typ, data = M.fetch(num, '(RFC822)')
		if(debug == True):
			print(data[0][1])

		mail = email.message_from_string(data[0][1].decode())
		#if(mail.is_multipart()):
		for part in mail.walk():
			ctype = part.get_content_type()
			if(ctype in allowed_content):
				if(part.get_filename() is None):
					continue
				fn = tmpdir + "/" + part.get_filename()
				open(fn, 'wb').write(part.get_payload(decode=True))
			else:
				#Not in allowed content, skip
				skip = True

		if(skip is not False):
			decompress.compr_type(fn)

		M.copy(num, done_folder)
		M.store(num, '+FLAGS', '\\Deleted')

	M.close()
	M.logout()
コード例 #4
0
    def _read_mail(
            self, imap_host, imap_port, imap_user, imap_pass, imap_folder,
            eNum,
            imap_sent):  # reads the most recent email and parses the text
        ### Reading emails from the server. The bulk of the work is here
        ### We prosses an email, clean up the text, check if it is a reply
        ### Load the original email if it is a reply, and check for and load images
        try:
            if "gmail" in imap_host:  # gmail server requires an ssl connection
                imap = IMAP4_SSL(imap_host, imap_port)
            else:  # tls is preferred
                imap = IMAP4(imap_host, imap_port)
                imap.starttls()
            ## login to server
            imap.login(imap_user, imap_pass)
        except:
            print("Failed to login")
            return
        #print(imap.list()) # for identifying mailboxes on the server
        imap.select("Inbox")  # connect to all mail.
        result, data = imap.uid('search', None,
                                "ALL")  # search and return uids instead
        ids = data[0]  # data is a list.
        id_list = ids.split()  # ids is a space separated string
        latest_email_uid = data[0].split()[eNum]
        result, data = imap.uid(
            'fetch', latest_email_uid, '(RFC822)'
        )  # fetch the email headers and body (RFC822) for the given ID
        raw_email = data[0][
            1]  # here's the body, which is raw headers and html and body of the whole email
        b = email.message_from_bytes(raw_email)
        date = b['Date']
        email_subject = b['subject']
        email_from = b['from']
        email_inreply = b['in-reply-to']
        email_body = b.get_payload()
        if b.is_multipart():  # search for text in the body
            for part in b.walk():
                ctype = part.get_content_type()
                cdispo = str(part.get('Content-Disposition'))
                if ctype == ('text/plain'
                             or 'text/html') and 'attachment' not in cdispo:
                    email_body = part.get_payload()
                    #print(email_body)
                    break
        frm = BeautifulSoup(email_from, 'html.parser')
        sub = BeautifulSoup(email_subject, 'html.parser')
        try:  # Try parsing the body text
            body = BeautifulSoup(email_body, 'html.parser')
        except:  # if email is encrypted it will throw an exception
            body = encription_warning
            #print(b['subject'])
            #print(b.keys())
        '''
		text = text.strip('\\t')
		text = text.replace('\\n', ' \n ')
		'''
        if email_inreply != "None":  # if this email is a reply
            try:
                if "gmail" in imap_host:
                    sent = IMAP4_SSL(imap_host, imap_port)
                else:
                    sent = IMAP4(imap_host, imap_port)
                    sent.starttls()
                ## login to server
                sent.login(imap_user, imap_pass)
                if "gmail" in imap_host:
                    sent.select('"[Gmail]/Sent Mail"')  # connect to sent mail.
                    #print("Opening gmail 'Sent'")
                else:
                    sent.select('Sent')  # connect to sent mail.
                    #print("Opening 'Sent'")
                # Search for the original email ID
                messages = sent.search(None, 'HEADER', 'MESSAGE-ID',
                                       email_inreply)
                # Process the result to get the message id’s
                messages = messages[1][0].split()
                # Use the first id to view the headers for a message
                result, source_data = sent.fetch(messages[0], '(RFC822)')
                raw_source = source_data[0][
                    1]  # here's the body, which is raw headers and html and body of the whole email
                s = email.message_from_bytes(
                    raw_source)  # convert to message object
                source_subject = s['subject']
                source_date = s['Date']
                source_bcc = s['bcc']
                #print("BCC from source: ", source_bcc)
                source_body = s.get_payload(decode=True)
                #print(frm, " Sent a reply to: ", source_subject)
                self.get_parent().msgIsReply = True
                src_sub = BeautifulSoup(source_subject, 'html.parser')
                try:  # extra check for encryption (in case user has encypted email)
                    src_body = BeautifulSoup(source_body, 'html.parser')
                except:  # if email is encrypted it will throw an exception
                    src_body = encription_warning
                self.get_parent().originSub = src_sub.get_text()
                self.get_parent().originBody = src_body.get_text()
                self.get_parent().originDate = src_date
                self.get_parent().originbcc = src_bcc
            except:
                print("No origin found")
        self.get_parent().msgFrom = frm.get_text()
        #print(frm.contents)
        add1 = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)",
                          str(frm))
        self.get_parent().msgEmail = add1[0]
        self.get_parent().msgSubject = sub.get_text()
        if body != encription_warning:
            body = body.get_text()
        self.get_parent().msgBody = body
        self.get_parent().msgDate = date
        self.get_parent()._print_mail()
        for part in b.walk():  # Check for image attachments
            ctype = part.get_content_type()
            if ctype in ['image/jpeg', 'image/png']:
                by = bytearray(part.get_payload(decode=True))
                #print(by[0])
                pool = PoolByteArray(by)  # create a Godot PoolByteArray
                self.get_parent().add_image(pool)  #Pass it to the mail fetcher
コード例 #5
0
ファイル: MailBox.py プロジェクト: zkycaesar/MailBox
    conn.commit()


if __name__ == '__main__':
    # conn = sqlite3.connect('test.db')
    # conn.execute("drop table MAILS")
    # conn.commit()
    # FetchFromMailbox("sina", 50)

    # app = QApplication(sys.argv)
    # window = UI()
    # window.show()
    # sys.exit(app.exec_())

    # conn = sqlite3.connect('test.db')
    # cursor = conn.execute("SELECT SUBJECT,MFROM,MTO,MDATE,CONTENTPLAIN,CONTENTHTML,ATTACHNAMES  from MAILS")
    # for row in cursor:
    #     for i in range(7):
    #         try:
    #             print(str(DecryptData(row[i]), encoding='utf-8'))
    #         except:
    #             print(str(DecryptData(row[i]), encoding='GBK'))
    #     print("\n")

    mailboxName = "sina"
    s = IMAP4('imap.%s.com' % mailboxName)
    file = open("sinamima.txt")
    pwd = file.read()
    s.login('zkycaesar', pwd)
    rsp, msg_num = s.select('TRASH', False)
コード例 #6
0
def query_folder(host, wait, user, passwd, learn, done, task, command, rhost):
    """
    queries all mails in folder named learn, passes this to rspamd
    and moves mail info done folder

    host: ip address or name of imapt host
    wait: time in seconds to wait between two messages
    user, passwd: imap credentials
    learn: imap folder to read from
    done: imap folder to move to after learn success
    task: one of ['ham'|'spam']
    command: executable to run for learning spam
    """

    con = IMAP4(host)
    con.starttls()

    try:
        con.enable("UTF8=ACCEPT")
    except IMAP4.error as e:
        logging.warning('IMAP4 error: {}'.format(e))

    con.login(user, passwd)

    # get number of messages to be learned
    try:
        typ, data = con.select(learn, readonly=False)
        num_msgs = int(data[0])
        logging.info("%d Messages in '%s'", num_msgs, learn)
    except IMAP4.error as e:
        logging.warning('IMAP4 error: {}'.format(e))
        return

    # get message ids as list
    try:
        typ, message_ids = con.search(None, 'ALL')
    except IMAP4.error as e:
        logging.warning('IMAP4 error: {}'.format(e))
        return

    # iterate over all messages in mailbox
    for num in message_ids[0].split():
        message = b""  # empty raw message

        typ, mesg_head = con.fetch(num, '(BODY.PEEK[HEADER])')
        for response_part in mesg_head:
            if isinstance(response_part, tuple):
                message += response_part[1]  # add header lines

        typ, mesg_body = con.fetch(num, '(BODY.PEEK[TEXT])')
        for response_part in mesg_body:
            if isinstance(response_part, tuple):
                message += response_part[1]  # add body lines

        try:
            # decode raw bytes to utf-8 string
            mesg_text = "".join(message.decode('utf-8'))
        except UnicodeDecodeError as e:
            logging.info('unicode decoding error: {}'.format(e))
            # try fallback decode latin-1
            mesg_text = "".join(message.decode('latin-1'))

        # pipe assembled message through rspam cli
        with subprocess.Popen([
                command,
                '--connect',
                rhost,
                'learn_%s' % task,
        ],
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE) as rspamc:
            rspamc.stdin.write(bytearray(mesg_text, "utf-8"))
            rspamc.stdin.close()
            result = rspamc.stdout.read().decode("utf-8")
            rspamc.stdout.close()

        result_lines = result.split("\n")

        # test if learning succesfull or "already learned". If either one,
        # move to "done" imap folder
        if re.match(rspamd_success_pattern, result_lines[1]):
            logging.debug(result)
            result = con.copy(num, done)
            logging.info("copied mail %d to %s" % (int(num), done))

            if result[0] == 'OK':
                mov, data = con.store(num, '+FLAGS', '(\\Deleted)')
                logging.debug("removed learned mail vom %s" % learn)
                con.expunge()
                logging.debug("expunged learned mail vom %s" % learn)

        else:
            logging.warning("mail not moved: %s", result)

        time.sleep(wait)

    con.logout()
    return
コード例 #7
0
import codecs
import email
import os
import locale
CHARSET = locale.getpreferredencoding(do_setlocale=False)
del locale


detach_dir = '.'  # directory where to save attachments (default: current)
username = config.gmail_username
password = config.gmail_password

# connecting to the gmail imap server
# server = IMAP4_SSL('imap.gmail.com')
# connecting to the UKR imap server
server = IMAP4('imap.ukr.net')
r, d = server.login(username, password)
filename = 'not file'
flag1 = True


def get_attached_for_email(log, params):
    global r, d, flag1, server

    while flag1:
        print r, d
        if 'OK' in r and 'LOGIN completed' in d:
            server.select()
            flag1 = False
            break
        else:
コード例 #8
0
ファイル: Extract.py プロジェクト: Raydrrrr1234/GiftCards
    def extract_real(self):
        config = self._settings
        extractor = None
        self.update_progress("Initializing...")
        cards = {}
        urls = []

        e = [
            extractors_list[i] for i, c in enumerate(self.checkboxes)
            if c.get()
        ]
        if len(e) == 0:
            self.update_progress('No sources selected!')
            self.extraction_cleanup()

        emails = [i for e_list in [x.email() for x in e] for i in e_list]
        days = int(config.get('Settings', 'days'))
        browser = None
        self.browser = None
        for section in (e for e in self._settings.sections()
                        if e.startswith('Email')):
            #        for section in ['Email1', 'Email2', 'Email3', 'Email4']:
            if int(config.get(section, 'imap_active')) == 1:
                imap_ssl = int(config.get(section, 'imap_ssl')) == 1
                imap_host = config.get(section, 'imap_host')
                imap_port = int(config.get(section, 'imap_port'))
                imap_username = config.get(section, 'imap_username')
                imap_password = config.get(section, 'imap_password')
                phonenum = config.get(section, 'phonenum')

                self.update_progress(
                    "Connecting to {}...".format(imap_username))

                # Connect to the server
                if imap_ssl:
                    mailbox = IMAP4_SSL(host=imap_host, port=imap_port)
                else:
                    mailbox = IMAP4(host=imap_host, port=imap_port)

                # Log in and select the configured folder
                mailbox.login(imap_username, imap_password)
                mailbox.select("INBOX")
                since = (date.today() -
                         timedelta(days - 1)).strftime("%d-%b-%Y")
                from_list = '(OR ' * (len(emails) - 1) + '(FROM "'+emails[0]+'") ' +\
                            ''.join(['(FROM "'+em+'")) ' for em in emails[1:]])[0:-1]
                # subject = ' HEADER Subject "'+extractor.subject()+'" ' if extractor.subject() is not "" else " "
                space = ' ' if len(emails) > 1 else ''
                search = '({}{}SINCE "{}")'.format(from_list, space, since)
                # status, messages = mailbox.search(None, '(OR (OR (FROM "*****@*****.**") (FROM "*****@*****.**")) (FROM "*****@*****.**"))')
                # search = '(FROM "*****@*****.**") SINCE "23-Nov-2018"'
                status, messages = mailbox.search(None, search)

                if status == "OK":
                    # Convert the result list to an array of message IDs
                    messages = [m.decode('ascii') for m in messages[0].split()]
                    if len(messages) == 0:
                        continue
                    self.update_progress(
                        "Fetching messages from {}...".format(imap_username))
                    data = []
                    try:
                        status, data = mailbox.fetch(','.join(messages),
                                                     '(RFC822)')
                        # remove every other element of list, extract messages
                        data = [
                            email.message_from_bytes(i[1])
                            for index, i in enumerate(data)
                            if (index + 1) % 2 != 0
                        ]
                    except IMAP4.error:
                        # Can't fetch all messages at once, do them one at a time
                        for msg_id in messages:
                            self.update_progress(
                                "{}: Fetching message id {}...".format(
                                    imap_username, msg_id))
                            # Fetch it from the server
                            status, m = mailbox.fetch(msg_id, '(RFC822)')
                            if status == "OK":
                                data.append(email.message_from_bytes(m[0][1]))

                    if status == "OK":
                        for idx, msg in enumerate(data):
                            # Get To: and From: addresses
                            to_address = email.utils.parseaddr(
                                msg.get("To", imap_username))[1]
                            from_address = email.utils.parseaddr(
                                msg.get("From"))[1]
                            # Get extractor
                            extractor = [
                                ext for ext in extractors_list
                                if from_address in ext.email()
                            ][0]
                            # Get the HTML body payload
                            msg_html = extractor.fetch_payload(msg)
                            if msg_html is None:
                                continue
                            # Save the email timestamp
                            datetime_received = datetime.fromtimestamp(
                                email.utils.mktime_tz(
                                    email.utils.parsedate_tz(msg.get('date'))))
                            # Parse the message
                            msg_parsed = BeautifulSoup(msg_html, 'html.parser')
                            # Find the "View My Code" link
                            url = extractor.fetch_url(msg_parsed, browser,
                                                      imap_username)

                            if url is not None:
                                if isinstance(url, list):
                                    for u in url:
                                        urls.append([
                                            messages[idx], extractor,
                                            datetime_received, u,
                                            imap_username, to_address, phonenum
                                        ])
                                else:
                                    urls.append([
                                        messages[idx], extractor,
                                        datetime_received, url, imap_username,
                                        to_address, phonenum
                                    ])
        if len(urls) < 1:
            self.update_progress('No cards to extract!')
            self.extraction_cleanup()

        if browser is None:
            self.update_progress("Launching ChromeDriver...")
            chrome_options = webdriver.ChromeOptions()
            if config.get('Settings', 'hide_chrome_window') == 'True':
                chrome_options.add_argument("--window-position=-10000,0")
            try:
                profile = config.get('Settings', 'profile')
                chrome_options.add_argument(
                    '--user-data-dir={}'.format(profile))
            except NoOptionError:
                pass

            browser = webdriver.Chrome(config.get('Settings',
                                                  'chromedriver_path'),
                                       chrome_options=chrome_options)
            self.browser = browser  # TODO make it all self.browser

        for msg_id, extractor, datetime_received, url, imap_username, to_address, phonenum in urls:
            self.update_progress(
                "{}: Getting gift card from message id: {}".format(
                    imap_username, msg_id))
            while True:
                # keep retrying to load the page if it's timing out.
                # TODO add cancel option
                while True:
                    try:
                        browser.get('about:blank')
                        browser.get(url)
                    except TimeoutException:
                        self.update_progress(
                            'Page load timed out. Retrying...')
                        continue
                    # if page load times out, retry...
                    if 'ERR_TIMED_OUT' in browser.page_source or 'com.bhn.general.service.error' in browser.page_source:
                        self.update_progress(
                            'Page load timed out. Retrying...')
                        time.sleep(1)
                        continue
                    break

                # challenege for various cards
                extractor.complete_challenge(browser, to_address, phonenum)
                card = extractor.fetch_codes(browser)

                if card is None:
                    break

                if card['card_store'] not in cards:
                    cards[card['card_store']] = []

                if card['card_code'] != '':
                    break

            if card is not None:
                card['datetime_received'] = str(datetime_received)
                card['url'] = url
                cards[card['card_store']].append(card)
                if config.get('Settings', 'screenshots'):
                    self.save_screenshot(browser, card['card_code'])
                # if self.ids.prints.active:
                #     browser.execute_script('window.print()')
                extractor.delay()

                # update output window for each new card
                self.output_cards(cards)

        browser.close()
        self.extraction_cleanup()
コード例 #9
0
def main():
    global args
    options = argparse.ArgumentParser(epilog='Example: \
  %(prog)s  -s imap.example.com -u [email protected] -f inbox -o ./mymail -S \"SINCE \\\"8-Sep-2014\\\"\" -P ./paswdfile'
                                      )
    options.add_argument("-v",
                         "--verbose",
                         help="increase output verbosity",
                         action="store_true")
    options.add_argument("--attachmentsonly",
                         help="download attachments only",
                         action="store_true")
    options.add_argument(
        "--disablereadonly",
        help="enable state changes on server; Default readonly",
        action="store_true")
    options.add_argument("--quiet",
                         help="supress all comments (stdout)",
                         action="store_true")
    options.add_argument("-s",
                         "--host",
                         help="imap server; eg. imap.mail.yahoo.com",
                         required=True)
    options.add_argument("-p",
                         "--port",
                         help="imap server port; Default is 143",
                         default=143)
    options.add_argument("-u", "--user", help="user's email id", required=True)
    options.add_argument(
        "-f",
        "--folder",
        help="mail folder from which the mail to retrieve; Default is INBOX",
        default="INBOX")
    options.add_argument("-o",
                         "--outdir",
                         help="directory to output",
                         required=True)
    options.add_argument(
        "-S",
        "--search",
        help=
        "search criteria, defined in IMAP RFC 3501; eg. \"SINCE \\\"8-Sep-2014\\\"\"",
        default="ALL")
    options.add_argument(
        "-P",
        "--pwdfile",
        help=
        "A file that stores IMAP user password. If not set, the user is prompted to provide a passwd"
    )
    args = options.parse_args()

    # redirect stdout to /dev/null
    if args.quiet:
        f = open(os.devnull, 'w')
        sys.stdout = f

    if args.pwdfile:
        infile = open(args.pwdfile, 'r')
        firstline = infile.readline().strip()
        args.pwd = firstline
    else:
        args.pwd = getpass.getpass()

    mail = IMAP4(args.host, args.port)
    mail.starttls()
    mail.login(args.user, args.pwd)
    ret, data = mail.select(args.folder, True)
    if ret == 'OK':
        vprint("Processing mailbox: " + args.folder)
        if process_mailbox(mail):
            mail.expunge()
            mail.close()
            mail.logout()
            sys.exit(1)

        mail.close()
    else:
        print("ERROR: Unable to open mailbox ", ret, file=sys.stderr)
        mail.logout()
        sys.exit(1)

    mail.logout()
コード例 #10
0
#!/usr/env/python
# -*- coding: utf-8 -*-
#
#  筛选邮箱中指定的邮件并获取邮件正文
#
#  Author: Dongdong Tian @ USTC
#  Date:   2016-07-28
#

import email
from imaplib import IMAP4

host = 'mail.ustc.edu.cn'
port = '143'
username = '******'
password = '******'

with IMAP4(host, port) as M:
    # 登录
    M.login(username, password)
    # 选择收件箱
    M.select('INBOX')
    # 筛选特定发件人的邮件
    type, data = M.search(None, '(FROM "*****@*****.**")')
    # 对邮件做循环
    for num in data[0].split():
        # 获取邮件中的TEXT部分
        type, data = M.fetch(num, "(BODY[TEXT])")
        msg = email.message_from_bytes(data[0][1])
        print(msg)
コード例 #11
0
ファイル: imap.py プロジェクト: c4tz/dotfiles
#         "host": "imap.example.com",
#         "port": 993,
#         "login": "******",
#         "password": "******"
#     }
# ]

acc_file = os.path.join(os.path.expanduser("~"), ".imap.json")
with open(acc_file, "r") as f:
    accounts = json.load(f)

unreads = 0

for account in accounts:
    try:
        if account["port"] == 143:
            obj = IMAP4(account["host"], account["port"])
            obj.starttls()
        else:
            obj = IMAP4_SSL(account["host"], account["port"])
        obj.login(account["login"], account["password"])
        obj.select()
        unreads += len(obj.search(None, "unseen")[1][0].split())
    except (OSError, IMAP4.error):
        sys.exit("Could not fetch unread mails from " + account["host"])

if unreads:
    print("%{F#b94646}%{F-} " + str(unreads))
else:
    print("%{F#666}%{F-}")
コード例 #12
0
def create_imap_connection():
    """Creates an secured imap connection to localhost."""
    imap = IMAP4("localhost", 143)
    imap.starttls()

    return imap
# Python Imaplib
# imaplib — IMAP4 protocol client.
# This module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of
# the IMAP4rev1 client protocol as defined in RFC 2060.
# It is backward compatible with IMAP4 (RFC 1730) servers, but note that the STATUS command is not supported in IMAP4.
#

#
# class imaplib.IMAP4(host='', port=IMAP4_PORT): 
# This class implements the actual IMAP4 protocol.
# The connection is created and protocol version (IMAP4 or IMAP4rev1) is determined when the instance is initialized.
# If host is not specified, '' (the local host) is used.
# If port is omitted, the standard IMAP4 port (143) is used.
# 

#
# The IMAP4 class supports the with statement.
# When used like this, the IMAP4 LOGOUT command is issued automatically when the with statement exits. E.g.:
# 

from imaplib import IMAP4

        with IMAP4("domain.org") as M:

              M.noop()

# OUTPUT: '('OK', [b'Nothing Accomplished. d25if65hy903weo.87'])'
コード例 #14
0
ファイル: test_import.py プロジェクト: LDO-CERT/methlab
    arguments:
    - content: mail payload to write on disk

    returns:
    - eml_path: path on disk
    """
    eml_path = "/wip/{}.eml".format(uuid.uuid4())
    with open(eml_path, "wb") as f:
        f.write(content)
    return eml_path


if __name__ == "__main__":
    try:
        info = InternalInfo.objects.first()
        inbox = IMAP4(info.imap_server)
        inbox.starttls()
        inbox.login(info.imap_username, info.imap_password)
        inbox.select(info.imap_folder)
    except ObjectDoesNotExist:
        raise Error
    except (IMAP4.error, ConnectionRefusedError, socket.gaierror):
        raise Error

    try:
        if info.http_proxy and info.https_proxy:
            cortex_api = Api(
                info.cortex_url,
                info.cortex_api,
                proxies={
                    "http": info.http_proxy,
コード例 #15
0
 def __init__(self, host):
     self.imap = IMAP4(host)
コード例 #16
0
ファイル: backupmails.py プロジェクト: mmerian/backupmails
    # Check for password
    password = args.password
    if password is None:
        password = getpass('password : '******'Connecting to mail server')
    if ssl:
        client = IMAP4_SSL(args.host, args.port)
    else:
        client = IMAP4(args.host, args.port)
    logger.info('Logging in')
    client.login(args.username, password)
    logger.info('Logged in')

    # Create dest dir if not exists
    if args.dest_dir is not None:
        if not os.path.isdir(args.dest_dir):
            os.mkdir(args.dest_dir)

except Exception as e:
    logger.exception(e)
    sys.exit(2)

# Iterate through all IMAP folders and backup messages
r, response = client.list()
コード例 #17
0
 def receive(self, criterion='ALL', pref='TEXT'):
     emails = []
     if self.imap_ssl:
         conn = IMAP4_SSL(self.imap_host, self.imap_port)
     else:
         conn = IMAP4(self.imap_host, self.imap_port)
     if self.imap_tls:
         conn.ehlo()
         conn.starttls()
         conn.ehlo()
     conn.login(self.user, self.password)
     conn.select('inbox')
     typ, msgs = conn.search(None, criterion)
     for id in msgs[0].split():
         typ, data = conn.fetch(id, '(RFC822)')
         msg = email.message_from_string(data[0][1].decode())
         # parse message body
         if msg.is_multipart():
             text = None
             html = None
             for msg1 in msg.get_payload():
                 if msg1.get_content_type() == 'text/plain':
                     text = self.convert_payload(msg1)
                 if msg1.get_content_type() == 'text/html':
                     html = self.convert_payload(msg1)
                 if msg1.get_content_type() == 'multipart/alternative':
                     for msg2 in msg1.get_payload():
                         if msg2.get_content_type() == 'text/plain':
                             text = self.convert_payload(msg2)
                         if msg2.get_content_type() == 'text/html':
                             html = self.convert_payload(msg2)
             if pref == 'TEXT':
                 if text is not None:
                     body = text.strip()
                 elif html is not None:
                     body = html.strip()
                 else:
                     body = '(null)'
             if pref == 'HTML':
                 if html is not None:
                     body = html.strip()
                 elif text is not None:
                     body = text.strip()
                 else:
                     body = '(null)'
         else:
             body = self.convert_payload(msg)
         # pack data
         item = {'body': body}
         for key in ['From', 'To', 'CC', 'Reply-To', 'Subject']:
             item[key] = self.convert_header(msg, key)
         for key in ['Date', 'Message-Id']:
             item[key] = msg.get(key, '')
         # escape Message-Id
         item['Message-Id'] = quote(item['Message-Id'].strip('<>'), safe='@')
         # append to array
         emails.append(item)
     conn.close()
     conn.logout()
     # sort array in reverse order of Date
     return sorted(emails, key=lambda item: email.utils.mktime_tz(email.utils.parsedate_tz(item['Date'])), reverse=True)
コード例 #18
0
ファイル: main.py プロジェクト: RcrdBrt/FitoTrack-logger
import email
import os
from glob import glob
from email.message import Message

from sqlalchemy import create_engine, text
import gpxpy

config = configparser.ConfigParser()
config.read("config.ini")

db = create_engine(
    f"postgresql://{config['db']['username']}:{config['db']['password']}@{config['db']['host']}/{config['db']['database']}"
).connect()

mail = IMAP4(host=config["mail"]["host"])
fitotrack_msg_filter = "ALL"


def init_database():
    with open("init.sql") as f:
        db.execute("\n".join(f.readlines()))


def _get_sender(msg: Message) -> str:
    sender: str = msg.get("from")
    if " " in sender:
        sender = sender.split(" ")
        for field in sender:
            if "@" in field and "<" in field and ">" in field:
                return field[1:-1]
コード例 #19
0
class EmailNotesError(Exception):
    pass

define('imap_server', default=None, type=str)
define('username', default=None, type=str)
define('password', default=None, type=str)
define('folder', default=None, type=str)
define('repo', default=None, type=str)
define('use_ssl', default=True, type=bool)
define('default_notebook', default='', type=str)
parse_config_file(config_path.email_notes)

if options.use_ssl:
    imap = IMAP4_SSL(options.imap_server)
else:
    imap = IMAP4(options.imap_server)
imap.login(options.username, options.password)

result, data = imap.select(options.folder)
if result != 'OK':
    raise EmailNotesError(result)
result, messages = imap.search(None, '(UNSEEN)')
if result != 'OK':
    raise EmailNotesError(result)

if messages[0] == '':
    exit()

# TODO there seems to be a bug where other notes in other notebooks can be deleted
# it might be when a folder is created?
git = sh.git.bake(_cwd=options.repo)
コード例 #20
0
def email_imap_trigger_monitor_evaluate():
    pattern_uid = re.compile(r'\d+ \(UID (?P<uid>\d+)\)')

    items = EmailImapTrigger.objects.filter(activated=True)

    for item in items:
        if not time_filter_evalution(item):
            continue

        try:
            if item.tls:
                server = IMAP4_SSL(item.server, item.port)
            else:
                server = IMAP4(item.server, item.port)

            server.login(item.email, item.password)

            server.select('INBOX')

            server.select('INBOX/' + item.folder_in)
            emails = server.select('INBOX/' + item.folder_in,
                                   readonly=True)[-1][-1]
            emails = str(emails, 'utf-8', 'ignore')
            if "doesn't exist" in emails:
                raise EmailImapFolderException

            server.select('INBOX/' + item.folder_out)
            emails = server.select('INBOX/' + item.folder_out,
                                   readonly=True)[-1][-1]
            emails = str(emails, 'utf-8', 'ignore')
            if "doesn't exist" in emails:
                raise EmailImapFolderException

            if item.status != "Active":
                item.status = "Active"
                item.save_without_historical_record()

            try:
                server.select('INBOX/' + item.folder_in)
                _, emails = server.search(None, 'All')
                emails = emails[0].split()
                email_id = emails[-1]

                email_data = server.fetch(email_id, '(RFC822)')

                email_subject = ""
                for email_data_part in email_data[1]:
                    if isinstance(email_data_part, tuple):
                        email_subject = email.message_from_string(
                            email_data_part[1].decode())['subject']
                        break

                email_uid = server.fetch(email_id, '(UID)')
                email_uid = str(email_uid[-1][0], 'utf-8', 'ignore')
                email_uid = pattern_uid.match(email_uid).group('uid')

                email_copy_response = server.uid('COPY', email_uid,
                                                 'INBOX/' + item.folder_out)

                if email_copy_response[0] == 'OK':
                    server.uid('STORE', email_uid, '+FLAGS', r'(\Deleted)')
                    server.expunge()

                if not Botflow.objects.get(
                        pk=item.botflow.pk).queue_if_already_running:
                    if BotflowExecution.objects.filter(
                            Q(status="Pending") | Q(status="Running"),
                            botflow=Botflow.objects.get(
                                pk=item.botflow.pk).path).exists():
                        add_botflow_execution = False
                    else:
                        add_botflow_execution = True
                else:
                    add_botflow_execution = True

                if add_botflow_execution:
                    add_botflow_execution_object(
                        bot_pk=determine_execution_bot(item).pk,
                        app_pk=item.app.pk,
                        botflow_pk=item.botflow.pk,
                        trigger=f"Email IMAP Trigger: {email_subject}",
                        custom_status=item.botflow_execution_custom_status)

            except:
                pass

        except:
            if item.status != "ERROR":
                item.status = "ERROR"
                item.save_without_historical_record()

        finally:
            try:
                server.logout()
            except:
                pass

            server = None
            del server

    items, pattern_uid = None, None
    del items, pattern_uid
コード例 #21
0
def get_attached_for_email(log, params):
    global r, d, flag1, server

    while flag1:
        print r, d
        if 'OK' in r and 'LOGIN completed' in d:
            server.select()
            flag1 = False
            break
        else:
            flag1 = True
            server = IMAP4('imap.ukr.net')
            r, d = server.login(username, password)
            continue
    subject = params['subject']
    sender = params['from']
    ext = params['ext']
    log.info('From: %s, Sub: %s' % (sender, subject))

    log.debug('Start parsing email')
    flag = False
    global filename
    # проверка писем по заданному отправителю и теме items = id, flg = OK, BAD
    # flg, items = server.search(None, '(UNSEEN FROM "%s" SUBJECT "%s")' % (sender, subject))
    flg, items = server.search(None, '(UNSEEN SUBJECT "%s")' % subject)
    items = items[0].split()  # getting the mails id
    # print items
    if len(items) > 0:
        flag = True
    else:
        log.warning('letters not found From: %s' % sender)
    repeat = False
    while flag:

        email_id = int(items[-1])
        if repeat:
            for i in items[::-1]:
                print i
                email_id = int(i)
                resp, data = server.fetch(str(email_id), "(RFC822)")
                email_body = data[0][1]  # getting the mail content
                mail = email.message_from_string(email_body)
                if sender.lower() in mail['From'].lower():
                    break

        # достает тело письма, и "читает" его
        resp, data = server.fetch(str(email_id), "(RFC822)")
        email_body = data[0][1]  # getting the mail content
        mail = email.message_from_string(email_body)
        print "[" + mail["From"] + "]: " + mail["Subject"]
        if sender.lower() not in mail['From'].lower():
            repeat = True
            continue
        else:
            repeat = False

        # Check if any attachments at all
        if mail.get_content_maintype() != 'multipart':
            log.warning('content not multipart')
            break

        for part in mail.walk():
            if not part.get_filename() or 'xml' in part.get_filename():
                continue
            # print part.get_filename()
            if part.get_content_maintype() == 'image':
                continue
            if part.get_content_maintype() == 'multipart':
                continue

            # is this part an attachment ?
            # if not part.get('Content-Disposition'):
            #     continue

            filename = part.get_filename()
            counter = 1

            # if there is no filename, we create one with a counter to avoid duplicates
            if not filename:
                filename = 'part-%03d%s' % (counter, 'bin')
                counter += 1

            att_path = os.path.join('file', 'file.' + ext)

            if not os.path.isfile(att_path):
                log.debug('Save attachment')
                fp = open(att_path, 'wb')
                fp.write(part.get_payload(decode=True))
                fp.close()
                flag = False
                break
    return filename
コード例 #22
0
origHdrs = [
    'From: [email protected]', 'To: [email protected]',
    'Subject: I wonder if it really works'
]
origBody = ['if it does', "I'll be quite", 'SURPRISED']
origMsg = '\r\n\r\n'.join(['\r\n'.join(origHdrs), '\r\n'.join(origBody)])

sendSvr = SMTP(SMTPSVR)
print 'Sending mail...'
sendSvr.login('*****@*****.**', 'nbenbi')
errs = sendSvr.sendmail('*****@*****.**', '*****@*****.**', origMsg)
sendSvr.quit()
print 'Mail sent.'
assert len(errs) == 0, errs
sleep(10)

recvSvr = IMAP4(IMAPSVR)
# recvSvr.user('*****@*****.**')
# recvSvr.pass_('nbenbi')
print 'Receiving mail...'
recvSvr.login('*****@*****.**', 'nbenbi')
recvSvr.select(readonly=True)
# you haven't see it, you won't remember it
retcode, msg = recvSvr.search(None, '(UNSEEN)')
# rsp, msg, siz = recvSvr.retr(recvSvr.stat()[0])

sep = msg.index('')
recvBody = msg[sep + 1:]
print recvBody
assert origBody == recvBody
コード例 #23
0
ファイル: mail.py プロジェクト: phzg03/PythonProject
def _format_addr(s):
    # 邮件地址格式化
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


SMTPSVR = 'smtp.126.com'  #smtp 服务器
IMAP4SVR = 'imap.126.com'
who = '*****@*****.**'
info = {
    'From': who,
    'To': who,
    'Subject': 'test message',
    'Content': 'this is a test email sended by python3.6'
}

msg = MIMEText(info['Content'], 'html', 'utf-8')
msg['From'] = _format_addr(info['From'])
msg['To'] = _format_addr(info['To'])
msg['Subject'] = Header(info['Subject'], 'utf-8').encode()

sendSvr = SMTP(SMTPSVR, 25)
sendSvr.login('user', 'password')  # 更换成自己的email 用户名和密码
errs = sendSvr.sendmail(who, '*****@*****.**', msg.as_string())
sendSvr.quit()
assert len(errs) == 0, errs

recvSvr = IMAP4('imap.126.com', 143)
recvSvr.login('user', 'password')
コード例 #24
0
#!/usr/bin/python3.7
from imaplib import IMAP4
imapconn = IMAP4("10.171.1.129")

fail = '\033[91m[-]\033[0m'
ok = '\033[92m[+]\033[0m'
msg_ok = " BINGO - Username: "******" FAIL - Username: "******"******"
username = "******"
wordlist = "wordlist.txt"

with open(wordlist, "r") as _word:
    for _line in _word:
        _password = _line.strip("\n")

        try:
            connection = imapconn.login(username, _password)
            print(ok + msg_ok + username + msg_pass + _password)

        except:
            print(fail + msg_fail + username + msg_pass + _password)
コード例 #25
0
ファイル: MailBox.py プロジェクト: zkycaesar/MailBox
def FetchFromMailbox(mailboxName, numMails=0):
    s = IMAP4('imap.%s.com' % mailboxName)
    file = open("sinamima.txt")
    pwd = file.read()
    # pwd = input("password: "******"mails", ))
    conn.execute("DELETE FROM ?", (mailboxName + "mails", ))
    conn.commit()
    if numMails == 0:
        numMails = int(msg_num[0])
    for i in range(numMails):
        print(i)
        try_num = 0
        mail_id = int(msg_num[0]) - i
        while True:
            try:
                rsp, data = s.fetch(str(mail_id), '(RFC822)')
            except:
                if try_num < 2:
                    continue
                    try_num += 1
                else:
                    break
            break
        emailData = email.message_from_bytes(data[0][1])

        # print(emailData.get('to'))
        mto = EncryptData(parseMailHeader(emailData.get('to')))
        # print(emailData.get('from'))
        mfrom = EncryptData(parseMailHeader(emailData.get('from')))
        # print(emailData.get('subject'))
        msubject = EncryptData(parseMailHeader(emailData.get('subject')))
        mdate = EncryptData(emailData.get('date'))
        mcontent_html = ""
        mcontent_plain = ""
        mattach_names = ""

        if emailData.is_multipart():
            partNum = 0
            for part in emailData.walk():
                partNum += 1
                ctype = part.get_content_type()
                # print(ctype)
                name = part.get_param('name')
                if name:
                    dir_name = "attachment/%s/mail#%d/" % (mailboxName,
                                                           mail_id)
                    if not os.path.isdir(dir_name):
                        os.makedirs(dir_name)
                    name = parseMailHeader(name)
                    data = part.get_payload(decode=True)  # 解码出附件数据,然后存储到文件中
                    f = open(dir_name + name,
                             'wb')  # 注意一定要用wb来打开文件,因为附件一般都是二进制文件
                    mattach_names += "\r\n%s" % name
                    f.write(data)
                    f.close()
                elif ctype == 'text/html':
                    mcontent_html = part.get_payload(decode=True)
                elif ctype == 'text/plain':
                    mcontent_plain = part.get_payload(decode=True)
        else:
            mcontent_html = emailData.get_payload(decode=True)

        mcontent_html = EncryptData(mcontent_html)
        mcontent_plain = EncryptData(mcontent_plain)
        mattach_names = EncryptData(mattach_names)

        conn.execute(
            "INSERT INTO ? (MAILID,SUBJECT,MFROM,MTO,MDATE,CONTENTPLAIN,CONTENTHTML,ATTACHNAMES) \
                      VALUES (?,?,?,?,?,?,?,?)",
            (mailboxName + "mails", mail_id, msubject, mfrom, mto, mdate,
             mcontent_plain, mcontent_html, mattach_names))

    conn.commit()
コード例 #26
0
#!/usr/bin/python
from imaplib import IMAP4
import email
import re

#U¿ywane do przetworzenia odpowiedzi IMAP.
FROM_HEADER = 'From: '
IMAP_UID = re.compile('UID ([0-9]+)')

#Po³±czenie z serwerem.
server = IMAP4('imap.przyklad.pl')
server.login('[u¿ytkownik]', '[has³o]')
server.select('Inbox')

#Pobranie unikatowych identyfikatorów wiadomo¶ci.
uids = server.uid('SEARCH', 'ALL')[1][0].split(' ')
uidString = ','.join(uids)

#Pobierz nag³ówek From ka¿dej wiadomo¶ci.
headers = server.uid('FETCH', '%s' % uidString,
                     '(BODY[HEADER.FIELDS (FROM	)])')
for header in headers[1]:
    if len(header) > 1:
        uid, header = header
        #Przetwórz odpowied¼ IMAP na rzeczywisty UID i warto¶æ nag³ówka From.
        match = IMAP_UID.search(uid)
        uid = match.groups(1)[0]

        fromHeader = header[len(FROM_HEADER):].strip()

        #Utwórz skrzynkê dla osoby, która wys³a³a wiadomo¶æ.
コード例 #27
0
from getpass import getpass

SERVER = input("IMAP4 Server: ")
if not SERVER:
    SERVER = "imap.physik.uni-muenchen.de"
    print("using server", SERVER)
MBOX = input("IMAP4 Mailfolder: ")
if not MBOX:
    MBOX = "INBOX"
    print("using mail folder", MBOX)
USER = input("IMAP4 User: "******"IMAP4 Password: ")

from imaplib import IMAP4_SSL as IMAP4

imap4 = IMAP4(SERVER)
imap4.login(USER, PASSWD)

pprint(imap4.list())

print()

imap4.select(MBOX, readonly=True)
#
# read 1st 20 headers
pprint(imap4.fetch('1:20', '(UID BODY[HEADER.FIELDS (SUBJECT DATE FROM)])'))

#
# read first 20 headers and text
# pprint(imap4.fetch('1:20', '(UID BODY[HEADER.FIELDS (SUBJECT)] BODY[TEXT])'))
コード例 #28
0
 def __init__(self, ssl=True, **kwargs):
   if ssl:
     self.connection = IMAP4_SSL(**kwargs)
   else:
     self.connection = IMAP4(**kwargs)
コード例 #29
0
import re
import csv
import getpass
from PIL import Image
from datetime import datetime
from imaplib import IMAP4, IMAP4_SSL
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep
import config

# Connect to the server
if config.IMAP_SSL:
    mailbox = IMAP4_SSL(host=config.IMAP_HOST, port=config.IMAP_PORT)
else:
    mailbox = IMAP4(host=config.IMAP_HOST, port=config.IMAP_PORT)

# Log in and select the configured folder
mailbox.login(config.IMAP_USERNAME, config.IMAP_PASSWORD)
mailbox.select(config.FOLDER)

# Search for matching emails
status, messages = mailbox.search(None, '(FROM {})'.format(config.FROM_EMAIL))
if status == "OK":
    # Convert the result list to an array of message IDs
    messages = messages[0].split()

    if len(messages) < 1:
        # No matching messages, stop
        print("No matching messages found, nothing to do.")
        exit()
コード例 #30
0
ファイル: ImapFetcher.py プロジェクト: Fulvio75/fpdb
def run(config, db):
    #print "start of IS.run"
    #try:
    #print "useSSL",config.useSsl,"host",config.host
    if config.useSsl:
        server = IMAP4_SSL(config.host)
    else:
        server = IMAP4(config.host)
    response = server.login(config.username,
                            config.password)  #TODO catch authentication error
    #print(_("response to logging in: "), response)
    #print "server.list():",server.list() #prints list of folders

    response = server.select(config.folder)
    #print "response to selecting INBOX:",response
    if response[0] != "OK":
        print response[1]
        raise IMAP4_SSL.error(response[1])  #TODO: show error message

    neededMessages = []
    response, searchData = server.search(
        None, "SUBJECT", "PokerStars Tournament History Request")
    for messageNumber in searchData[0].split(" "):
        response, headerData = server.fetch(messageNumber,
                                            "(BODY[HEADER.FIELDS (SUBJECT)])")
        if response != "OK":
            raise error  #TODO: show error message
        neededMessages.append(("PS", messageNumber))

    print _("Found %s eMails to fetch") % (len(neededMessages))

    if (len(neededMessages) == 0):
        raise error  #TODO: show error message

    NIL = Literal("NIL").setParseAction(replaceWith(None))
    integer = Word(nums).setParseAction(lambda t: int(t[0]))
    quotedString.setParseAction(removeQuotes)
    content = (NIL | integer | Word(alphanums))

    email_bodies = []
    for i, messageData in enumerate(neededMessages, start=1):
        #print("Retrieving message %s" % i)

        # Obtain BODYSTRUCTURE call
        email_uid = messageData[1]
        response, bodystructure = server.fetch(email_uid, '(BODYSTRUCTURE)')
        if response != "OK":
            raise error  #TODO: show error message

        response, bodyData = server.fetch(email_uid, "(UID BODY[TEXT])")
        if response != "OK":
            raise error  #TODO: show error message
        if messageData[0] == "PS":
            splitted_bodystructure = string.split(str(bodystructure[0]), " ",
                                                  1)
            parsed_bodystructure = nestedExpr(
                content=content,
                ignoreExpr=quotedString).parseString(splitted_bodystructure[1])
            charset = parsed_bodystructure[0][1][2][1]
            content_transfer_encoding = parsed_bodystructure[0][1][5]
            if content_transfer_encoding == 'QUOTED-PRINTABLE':
                decoded_body = quopri.decodestring(
                    bodyData[0][1]).decode(charset)
            else:
                decoded_body = bodyData[0][1].decode(charset)
            email_bodies.append(decoded_body)
            #finally:
            #   try:
    server.close()
    # finally:
    #    pass
    server.logout()
    print _("Finished downloading emails.")

    errors = 0
    if len(email_bodies) > 0:
        errors = importSummaries(db, config, email_bodies, options=None)
    else:
        print _("No Tournament summaries found.")

    print(_("Errors:"), errors)