Beispiel #1
0
    def recv_mail(self):
        if not self.downloader:
            slog.warning("downloader not login")
            return False

        self.downloader.select()  # Select inbox or default namespace
        (retcode, messages) = self.downloader.search(None, '(UNSEEN)')
        if retcode != 'OK':
            slog.warning("read mail from server failed")
            return False

        if not os.path.exists("./downmail"):
            os.mkdir("./downmail")

        for num in messages[0].split():
            slog.info('Processing: {0}'.format(num))
            typ, data = self.downloader.fetch(num.decode('utf-8'), '(RFC822)')
            # 标记已读
            sr = self.downloader.store(num, '+FLAGS', '\Seen')
            # 标记删除
            #sr = downloader.store(num, '+FLAGS', '\\Deleted')
            email_message = email.message_from_string(
                data[0][1].decode('utf-8'))

            # mail to string
            fp = StringIO()
            g = generator.Generator(fp, mangle_from_=True, maxheaderlen=60)
            g.flatten(email_message)
            email_text = fp.getvalue()

            # mail_string to json_string
            pmail = mailparser.parse_from_string(email_text)
            email_json = pmail.mail_json
            # mail to json obj
            email_data = json.loads(email_json)

            # 处理邮件
            self.handle_mail(email_data)

            subject = email_data.get('subject')
            body = email_data.get('body')
            slog.info("get mail: subject[{0}] body.size[{1}]".format(
                subject, len(body)))

            filename = './downmail/{0}.eml'.format(subject)
            with open(filename, 'w') as fout:
                gr = generator.Generator(fout)
                gr.flatten(email_message)
                #fout.write(email_text)
                fout.close()

            filename_j = './downmail/{0}.json'.format(subject)
            with open(filename_j, 'w') as fjout:
                fjout.write(
                    json.dumps(email_data, indent=4, ensure_ascii=False))
                fjout.close()

            slog.info("save {0} ok,\n".format(filename))

        return True
Beispiel #2
0
    def handle(self, *args, **options):
        raw_mail = options.get('message').read()

        from mailparser import parse_from_string
        mail = parse_from_string(raw_mail)

        To = mail.to
        try:
            ml = List.objects.get(email=To)
        except List.DoesNotExist:
            raise CommandError('No mailing list by this name "%s"' % To)

        From = str(mail.from_)
        Subject = str(mail.subject)
        Body = str(mail.body)
        Attachments = mail.attachments

        ok = send_email(To, From, Subject, Body, Attachemnts)
        if ok:
            self.stdout.write(
                self.style.SUCCESS('Successfully send message to "%s"' %
                                   list_email))
        else:
            self.stdout.write(
                self.style.DANGER('Error in sending message to "%s"' %
                                  list_email))
Beispiel #3
0
    def _run_action(self, action_parameters):
        """
        Run action with the provided action parameters, return status output and
        parse the output email data.
        """
        models = self.fixtures_loader.load_models(
            fixtures_pack='packs/core',
            fixtures_dict={'actions': ['sendmail.yaml']})
        action_db = models['actions']['sendmail.yaml']
        entry_point = self.fixtures_loader.get_fixture_file_path_abs(
            'packs/core', 'actions', 'send_mail/send_mail')

        runner = self._get_runner(action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters)
        runner.post_run(status, result)

        # Remove footer added by the action which is not part of raw email data and parse
        # the message
        if 'stdout' in result:
            email_data = result['stdout']
            email_data = email_data.split('\n')[:-2]
            email_data = '\n'.join(email_data)

            if six.PY2 and isinstance(email_data, six.text_type):
                email_data = email_data.encode('utf-8')

            message = mailparser.parse_from_string(email_data)
        else:
            email_data = None
            message = None

        return (status, result, email_data, message)
Beispiel #4
0
def attach(email_id, file_name):
    try:
        conn = sqlite3.connect(DB)
        cur = conn.cursor()
        sql = f"""
                            select email_raw from {TABLE}  where id={email_id}
                        """
        cur.execute(sql)
        val = cur.fetchone()
        cur.close()
        conn.close()
        raw_email = val[0]
        mail = mailparser.parse_from_string(raw_email)
        data = ""
        charset = "utf-8"
        for att in mail.attachments:
            fnm =att['filename']
            if fnm==file_name:
                data = att['payload']
                charset = "utf-8" if att['charset'] is None else att['charset']
                data = base64.b64decode(data)
        response = make_response(data)
        mime_type = mimetypes.guess_type(file_name)[0]
        response.headers['Content-Type'] = mime_type
        response.headers['Content-Disposition'] = f'attachment; "filename*=UTF-8 {quote(file_name.encode("utf-8"))}'
        return response
    except Exception as err:
        print(err)
        abort(404)
Beispiel #5
0
    def parse_from_email(self, content):
        """
            Parse a raw email

            :param str content: The raw email
            :rtype: `cerberus.parsers.ParsedEmail`
            :return: The parsed email
        """
        email = mailparser.parse_from_string(content)
        provider = self._get_provider(email)
        recipients = "{} {}".format(" ".join([addr for _, addr in email.to_]),
                                    email.message.get("Cc"))

        parsed = ParsedEmail(
            headers=email.headers,
            subject=email.subject.strip().rstrip("\x00"),
            body=email.body.strip().rstrip("\x00"),
            date=int(time.mktime(utc2local(email.date).timetuple())),
            provider=provider,
            recipients=self.email_re.findall(recipients),
            attachments=decode_attachments(email.attachments),
            blacklisted=provider in self.blacklisted_providers,
        )

        # Add 'message/*' content to body
        parsed.body = "{}\n\n--- Forwarded email(s) ---\n{}".format(
            parsed.body,
            "\n\n--------".join(get_mime_message_type_contents(email.message)),
        )

        self._fetch_items(parsed)
        self._force_abuse_category(parsed)
        parsed.clean_items()

        return parsed
Beispiel #6
0
    def _run_action(self, action_parameters):
        """
        Run action with the provided action parameters, return status output and
        parse the output email data.
        """
        models = self.fixtures_loader.load_models(
            fixtures_pack='packs/core', fixtures_dict={'actions': ['sendmail.yaml']})
        action_db = models['actions']['sendmail.yaml']
        entry_point = self.fixtures_loader.get_fixture_file_path_abs(
            'packs/core', 'actions', 'send_mail/send_mail')

        runner = self._get_runner(action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters)
        runner.post_run(status, result)

        # Remove footer added by the action which is not part of raw email data and parse
        # the message
        if 'stdout' in result:
            email_data = result['stdout']
            email_data = email_data.split('\n')[:-2]
            email_data = '\n'.join(email_data)

            if six.PY2 and isinstance(email_data, six.text_type):
                email_data = email_data.encode('utf-8')

            message = mailparser.parse_from_string(email_data)
        else:
            email_data = None
            message = None

        return (status, result, email_data, message)
Beispiel #7
0
    def _run_action(self, action_parameters):
        """
        Run action with the provided action parameters, return status output and
        parse the output email data.
        """
        models = self.fixtures_loader.load_models(
            fixtures_pack="packs/core",
            fixtures_dict={"actions": ["sendmail.yaml"]})
        action_db = models["actions"]["sendmail.yaml"]
        entry_point = self.fixtures_loader.get_fixture_file_path_abs(
            "packs/core", "actions", "send_mail/send_mail")

        runner = self._get_runner(action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters)
        runner.post_run(status, result)

        # Remove footer added by the action which is not part of raw email data and parse
        # the message
        if "stdout" in result:
            email_data = result["stdout"]
            email_data = email_data.split("\n")[:-2]
            email_data = "\n".join(email_data)

            if six.PY2 and isinstance(email_data, six.text_type):
                email_data = email_data.encode("utf-8")

            message = mailparser.parse_from_string(email_data)
        else:
            email_data = None
            message = None

        return (status, result, email_data, message)
Beispiel #8
0
def detail(id, type):
    """

    :param id:
    :param type:
    :return:
    """
    conn = sqlite3.connect(DB)
    cur = conn.cursor()
    sql = f"""
                    select id, dt, email_raw from {TABLE}  where id={id}
                """
    cur.execute(sql)
    val = cur.fetchone()
    cur.close()
    conn.close()
    raw_email = val[2]
    mail = mailparser.parse_from_string(raw_email)
    if type=='html':
        mail_content = mail.text_html
    else:
        mail_content = mail.text_plain
    from_ = mail.from_[0][1]
    to_ = mail.to[0][1]
    dt = val[1]
    email_subject = mail.subject
    return render_template("detail.html", **{"title":email_subject, "from":from_, "type":type, "to":to_, "dt":dt, "mail_content":mail_content,
                                             "attachements":mail.attachments, "mid":id
                                             })
Beispiel #9
0
def email(email):
    """
    获得时间最近的一个email
    :param email:
    :param type:
    :return:
    """
    conn = sqlite3.connect(DB)
    cur = conn.cursor()
    sql = f"""
                        select id,dt,email_raw from {TABLE}  where email_to=? order by dt desc limit 1;
                    """
    cur.execute(sql, [email])
    val = cur.fetchone()
    cur.close()
    conn.close()
    raw_email = val[2]
    mail = mailparser.parse_from_string(raw_email)
    result = {}
    result['id'] = val[0]
    result['from'] = mail.from_[0][1]
    result['to_'] = mail.to[0][1]
    result['title'] = mail.subject
    result['dt'] = val[1]
    result['html'] = mail.text_html
    result['text'] = mail.text_plain
    return jsonify(result)
     def tarnsforming_raw_text(self):
        # Import the email modules we'll need
        import glob
        import email
        import mailparser
        from email import policy
        from email.parser import BytesParser

        path = 'C:/Users/Lokesh/jupyter works/email_classification/datawe/raw/Email_Classification/*'
        email_types = glob.glob(path)
        self.appendFilesData = []
        for folder in email_types:
            files = glob.glob(folder + "/*.txt")
            email_type = folder.split('\\')[1]
            for name in files:
                try:
                    with open(name) as fp:
                        raw_data = fp.read()
                        #                     file_raw_data.append(raw_data)
                        msg = mailparser.parse_from_string(raw_data)
                        self.appendFilesData.append({
                            "to": msg.to,
                            "from": msg.from_,
                            "subject": msg.subject,
                            "date": msg.date,
                            #                     "sent":msg["Sent"],
                            #                     "importance":msg["Importance"],
                            "content": raw_data,
                            "class_to_exec": email_type,
                        })

                except IOError as exc:
                    print('Exception')

        return self.appendFilesData
Beispiel #11
0
def parseThis(fullfilename):
    myStream = openEr(fullfilename)
    email_message = mailparser.parse_from_string(myStream)
    global _mdate
    _mdate = email_message.date
    _body = email_message.body
    _id = email_message.message_id
    return _body
Beispiel #12
0
    async def parse(self, from_,to_, raw_mail):
        mail = mailparser.parse_from_string(raw_mail)
        email_subject = mail.subject
        has_attach = 1 if len(mail.attachments)>0 else 0
        tm = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        print(f"{tm}\t{from_}\t{to_}\t{email_subject}")
        for ito_ in to_:
            await self.save_mail(from_, ito_, email_subject,  tm, raw_mail, has_attach)
Beispiel #13
0
def main():
    args = get_args().parse_args()

    if args.file:
        if args.outlook:
            parser = mailparser.parse_from_file_msg(args.file)
        else:
            parser = mailparser.parse_from_file(args.file)
    elif args.string:
        parser = mailparser.parse_from_string(args.string)
    elif args.stdin:
        if args.outlook:
            raise MailParserOutlookError(
                "You can't use stdin with msg Outlook")
        parser = mailparser.parse_from_file_obj(sys.stdin)

    if args.json:
        safe_print(parser.mail_json)

    if args.body:
        safe_print(parser.body)

    if args.headers:
        safe_print(parser.headers_json)

    if args.to:
        safe_print(parser.to_json)

    if args.delivered_to:
        safe_print(parser.delivered_to_json)

    if args.from_:
        safe_print(parser.from_json)

    if args.subject:
        safe_print(parser.subject)

    if args.receiveds:
        safe_print(parser.received_json)

    if args.defects:
        for i in parser.defects_categories:
            safe_print(i)

    if args.senderip:
        r = parser.get_server_ipaddress(args.senderip)
        if r:
            safe_print(r)
        else:
            safe_print("Not Found")

    if args.attachments or args.attachments_hash:
        print_attachments(parser.attachments, args.attachments_hash)

    if args.mail_hash:
        print_mail_fingerprints(parser.body.encode("utf-8"))
Beispiel #14
0
    def from_string(cls, raw_email, key):
        mail = mailparser.parse_from_string(raw_email)
        data = {
            'id': key,
            'title': mail.subject,
            'from_email': mail._from,
            'body_html': mail.body,
            'body_plain': mail.body,
            'media': [],
            'date': mail.date,
        }

        for i, att in enumerate(mail.attachments):

            # Ensure unique objectKeys for attachments
            filename = '{}-{}-{}'.format(key, i, att['filename'])

            response = s3client.put_object(
                    ACL='public-read',
                    Body=base64.b64decode(att['payload']),
                    Bucket=bucket_name,
                    ContentType=att['mail_content_type'],
                    Key=filename,
            )

            location = s3client.get_bucket_location(Bucket=bucket_name)['LocationConstraint']
            url = "%s/%s" % (bucket_location, filename)
            cid = att['content-id'].strip('<>')

            data['media'].append({
                'type': att['mail_content_type'],
                'filename': att['filename'],
                'url': url,
                'cid': cid,
            })

        # if we only got html
        if not 'body_plain' in data and 'body_html' in data:
            data['body_plain'] = data['body_html'] # TODO: strip html

        # if we only got plain text
        if not 'body_html' in data and 'body_plain' in data:
            data['body_html'] = data['body_plain']

        # update src of embedded images
        if 'body_html' in data:
            for att in data['media']:
                if att['cid']:
                    data['body_html'] = data['body_html'].replace('cid:{}'.format(att['cid']), att['url'])

        issuer = Issuer.from_email(data['from_email'][0][1])

        data['issuer_id'] = issuer.get_id() if isinstance(issuer, Issuer) else None
        data['issuer_name'] = issuer.get_name() if isinstance(issuer, Issuer) else None

        return cls(data)
Beispiel #15
0
 def process_message(self, peer, mailfrom, rcpttos, data):
     parsed = mailparser.parse_from_string(data)
     sender = parsed.from_[0][0]
     _from = parsed.from_[0][1]
     _to = parsed.to[0][1]
     print('New email to %s.' % _to)
     _date = int(time.time())
     body = str(base64.b64encode(parsed.body.encode()), 'utf-8')
     params = (_date, sender, _from, _to, body)
     c.execute('INSERT INTO mails VALUES (?, ?, ?, ?, ?)', params)
     conn.commit()
Beispiel #16
0
def main():
    args = get_args().parse_args()

    if args.file:
        if args.outlook:
            parser = mailparser.parse_from_file_msg(args.file)
        else:
            parser = mailparser.parse_from_file(args.file)
    elif args.string:
        parser = mailparser.parse_from_string(args.string)

    if args.json:
        j = json.loads(parser.parsed_mail_json)
        safe_print(json.dumps(j, ensure_ascii=False, indent=4))

    if args.body:
        # safe_print(parser.body)
        safe_print(parser.body)

    if args.headers:
        safe_print(parser.headers)

    if args.to:
        safe_print(parser.to_)

    if args.from_:
        safe_print(parser.from_)

    if args.subject:
        safe_print(parser.subject)

    if args.receiveds:
        safe_print(parser.receiveds)

    if args.defects:
        for i in parser.defects_category:
            safe_print(i)

    if args.anomalies:
        for i in parser.anomalies:
            safe_print(i)

    if args.senderip:
        r = parser.get_server_ipaddress(args.senderip)
        if r:
            safe_print(r)
        else:
            safe_print("Not Found")

    if args.attachments or args.attachments_hash:
        print_attachments(parser.attachments_list, args.attachments_hash)

    if args.mail_hash:
        print_mail_fingerprints(parser.body.encode("utf-8"))
Beispiel #17
0
def read_emails(emails_dir):
    emails = pd.DataFrame()
    for filename in os.listdir(emails_dir):
        f = open(os.path.join(emails_dir, filename),
                 "r",
                 encoding='ISO-8859-1')
        raw_email = f.read()
        mail = mailparser.parse_from_string(raw_email)
        emails = emails.append({
            'body': mail.body,
            'headers': 'mail.headers'
        },
                               ignore_index=True)
    return emails
Beispiel #18
0
 def get(self):
     # production UNSEEN development ALL
     result_emails = []
     messages = self.client.search(['ALL'])
     responses = self.client.fetch_email_messages(messages)
     for message_id, data in responses.items():
         mail = mailparser.parse_from_string(data.as_string())
         path_list = self.save_attachments(mail.attachments)
         result = {
             'subject': mail.subject,
             'message': mail.text_plain[-1],
             'attachments': path_list,
             'date': mail.date
         }
Beispiel #19
0
def inbound_parse():
    """Process POST from Inbound Parse and print received data."""
    parse = Parse(config, request)
    print("Email!")
    raw_email = parse.get_raw_email()

    try:
        email = \
        {
            "raw_email": raw_email,
            "created_timestamp": datetime.datetime.now()
        }
        inserted_email = db_emails_collection.insert_one(email).inserted_id
        print("Saved last email in database!", inserted_email)
    except Exception as e:
        print(e)
        print("Could not save last email to database")

    mail = mailparser.parse_from_string(raw_email)
    print("Attachments", len(mail.attachments))
    for attached_file in mail.attachments:
        try:
            raw_payload = attached_file["payload"]
            if attached_file["content_transfer_encoding"] == "base64":
                payload_bytes = base64.b64decode(raw_payload)
                html_payload = payload_bytes.decode("utf-8")
            else:
                html_payload = raw_payload
            highlights = parse_highlights(html_payload)
            print(f"Received and parsed highlights for {highlights['title']}")
            try:
                db_highlights_collection.remove({
                    "title":
                    highlights["title"],
                    "authors":
                    highlights["authors"]
                })
                inserted_highlights = db_highlights_collection.insert_one(
                    highlights).inserted_id
                print("Saved highlights to database", inserted_highlights)
            except Exception as e:
                print(e)
                print("Could not save highlights to database")

            SyncToNotion(highlights)
        except Exception as e:
            print(e)
            print("Could not process attached file")

    return "OK"
Beispiel #20
0
def deletet():
    try:
        args = request.args
        mail_id = args['mail_id']
        mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
        mail.login(args['email'], args['password'])
        mail.list()
        mail.select('inbox')

        # # type, data = mail.search(None, '(UNSEEN)')
        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]
        print('mail_ids', mail_ids)

        id_list = mail_ids.split()
        # first_email_id = int(id_list[0])
        # latest_email_id = int(id_list[-1])
        print("========++++++++++========== id_list ", id_list)

        for num, i in enumerate(id_list):
            print('iiiii========================', i, num)
            typ, data = mail.fetch(i, '(RFC822)')
            raw_email = data[0][1]
            raw_email = str(raw_email, 'utf-8')
            mail_data = mailparser.parse_from_string(raw_email)
            data = {
                'id': mail_data.message_id,
                'subject': mail_data.subject,
                'from': mail_data.from_,
                'body': mail_data.body
            }
            print(data['id'].replace('+', ' '), mail_id)
            # mail.store(i, '+X-GM-LABELS', '\\Trash')
            if data['id'].replace('+', ' ') == mail_id:
                print('Match', data['id'])
                mail.store(i, '+X-GM-LABELS', '\\Trash')

        mail.expunge()
        mail.close()
        mail.logout()

        return jsonify({'success': 'true', 'message': 'Delete successfully'})

    except Exception as e:
        print(e)
        return jsonify({
            'success': 'false',
            'message': 'some thing is wrong with login credentials'
        })
Beispiel #21
0
    def recv_mail_weibo(self, subject_pattern, from_email_pattern):
        if not self.downloader:
            slog.warning("downloader not login")
            if not self.login():
                return

        self.downloader.select()  # Select inbox or default namespace
        (retcode, messages) = self.downloader.search(None, '(UNSEEN)')
        if retcode != 'OK':
            slog.warning("read mail from server failed")
            return False

        for num in messages[0].split():
            slog.info('Processing: {0}'.format(num))
            typ, data = self.downloader.fetch(num.decode('utf-8'), '(RFC822)')
            email_message = email.message_from_string(
                data[0][1].decode('utf-8'))

            # mail to string
            fp = StringIO()
            g = generator.Generator(fp, mangle_from_=True, maxheaderlen=60)
            g.flatten(email_message)
            email_text = fp.getvalue()

            # mail_string to json_string
            pmail = mailparser.parse_from_string(email_text)
            email_json = pmail.mail_json
            # mail to json obj
            email_data = json.loads(email_json)
            subject = email_data.get('subject')
            if subject.find(subject_pattern) == -1:
                continue
            from_email = email_data.get('from')[0]  # list
            if from_email_pattern not in from_email:
                continue

            # 标记已读
            sr = self.downloader.store(num, '+FLAGS', '\Seen')
            # 标记删除
            #sr = downloader.store(num, '+FLAGS', '\\Deleted')

            # find target email
            body = email_data.get('body')
            slog.info("read email: subject:{0} body size:{1}".format(
                subject, len(body)))
            vcode = body.split('\n')[0]
            return vcode

        return ''
Beispiel #22
0
def endpoint(event, context):
    if 'ses' not in event['Records'][0]:
        print(
            'this was not an SES event. event["Records"][0]["ses"] not found')
        return
    ses_notification = event['Records'][0]['ses']
    mail_message_id = ses_notification['mail']['messageId']
    print(f"Message Id received: {mail_message_id}")

    try:
        data = s3.get_object(Bucket=SES_S3_BUCKET_NAME, Key=mail_message_id)
        raw_email = data['Body'].read()
        parsed_email = mailparser.parse_from_string(raw_email.decode('utf-8'))
        process_incoming_mail(parsed_email)
    except Exception as e:
        print(e)
        raise e
Beispiel #23
0
def check_if_recipient_is_vip(list_data):
    raw_string = ''

    for line in list_data:
        if 'append "[Gmail]/Sent Mail"' not in line:
            raw_string += line
    mail = mailparser.parse_from_string(raw_string)

    mail_to = mail.to[0][1]
    mail_from = mail.from_[0][1]

    mutation.mutate_and_send(mail)

    if mail_to in vip_list:
        return True, mail
    else:
        return False, mail
Beispiel #24
0
def trash():
    try:
        args = request.args
        mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
        mail.login(args['email'], args['password'])
        mail.list()
        # print(mail.list())
        # mail.select('Travel')
        # mail.select('INBOX')
        mail.select('"[Gmail]/Trash"')
        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]
        # print('======>',mail_ids)

        id_list = mail_ids.split()
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])
        print('======>', first_email_id, latest_email_id)

        # for i in reversed(range(first_email_id, latest_email_id)):
        email = []
        # id_list = id_list.reverse()
        for idx, i in reversed(list(enumerate(id_list))):
            print('iiiii========================', i, idx)
            typ, data = mail.fetch(i, '(RFC822)')
            raw_email = data[0][1]
            raw_email = str(raw_email, 'utf-8')
            mail_data = mailparser.parse_from_string(raw_email)
            data = {
                'id': mail_data.message_id,
                'subject': mail_data.subject,
                'from': mail_data.from_,
                'body': mail_data.body
            }
            email.insert(idx, data)
            if idx == 20:
                break

        return jsonify(email)

    except Exception as e:
        return jsonify({
            'success': 'false',
            'message': 'some thing is wrong with login credentials'
        })
Beispiel #25
0
 def searchMailBySubject(self, **args):
     """This function will search for a particular mail as per the given sender and subject"""
     try:
         output = ""
         attach_list = list()
         mail_list = list()
         FROM_EMAIL = args["username"]
         FROM_PWD = args["password"]
         subject = args["subject"]
         SMTP_SERVER = "imap.gmail.com"
         SMTP_PORT = 993
         mail = imaplib.IMAP4_SSL(SMTP_SERVER)
         try:
             mail.login(FROM_EMAIL, FROM_PWD)
         except:
             return ["Authentication failed, contact admin", []]
         mail.select('inbox')
         type, data = mail.search(
             None, '(SUBJECT "{subject}")'.format(subject=subject))
         # search and return uids instead
         mail_count = len(
             data[0].split())  # data[0] is a space separate string
         if (mail_count == 0):  # if there are no mails found, i==0
             return ["No mails!!", attach_list]
         latest_email_uid = data[0].split()[
             -1]  # unique ids wrt label selected
         result, email_data = mail.fetch(latest_email_uid, '(RFC822)')
         # fetch the email body (RFC822) for the given ID
         raw_email = email_data[0][1]
         # continue inside the same for loop as above
         raw_email_string = raw_email.decode('utf-8')
         email = mailparser.parse_from_string(raw_email_string)
         attach = email.attachments
         if (len(attach) > 0):
             for i in range(len(attach)):
                 attach_list.extend(attach[i]['filename'])
         output = "Email from " + email.from_[0][1] + " dated " + str(
             email.date
         ) + " on the subject " + email.subject + " is " + self.remove_html_tags(
             str(email.text_html))
         return [output, attach_list]
         mail.close()
         mail.logout()
     except:
         return ["Some error occured, try again!", []]
Beispiel #26
0
def read_email_from_gmail():
    try:
        args = request.args

        mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
        mail.login(args['email'], args['password'])
        mail.list()
        mail.select('inbox')

        # type, data = mail.search(None, '(UNSEEN)')
        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]

        id_list = mail_ids.split()
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])
        # print(id_list)

        # for i in reversed(range(first_email_id, latest_email_id)):
        email = []

        for idx, i in enumerate(id_list):
            # print('iiiii========================', i)
            typ, data = mail.fetch(i, '(RFC822)')
            raw_email = data[0][1]
            raw_email = str(raw_email, 'utf-8')
            mail_data = mailparser.parse_from_string(raw_email)
            data = {
                'id': mail_data.message_id,
                'subject': mail_data.subject,
                'from': mail_data.from_,
                'body': mail_data.body
            }
            email.insert(idx, data)
            if idx == 20:
                break

        return jsonify(email)
    except Exception as e:
        return jsonify({
            'success': 'false',
            'message': 'some thing is wrong with login credentials'
        })
def get_emails(mail_path):
    mbox = mailbox.mbox(mail_path)
    parsed_emails = []
    try:
        mbox.lock()
    except mailbox.ExternalClashError:
        logging.error(
            "Mailbox not consumed, it is being access by another program")
        return parsed_emails, 0

    keys = mbox.keys()
    for key in keys:
        email = mbox.pop(key)
        parsed_emails.append(mailparser.parse_from_string(str(email)))

    mbox.flush()
    mbox.unlock()
    mbox.close()
    return parsed_emails, 1
Beispiel #28
0
 async def handle_DATA(self, _, session, envelope):
     print('Recieved message...')
     # noinspection PyBroadException
     try:
         file_name = os.path.join('messages', str(uuid.uuid4().hex))
         with open(file_name, 'w') as open_file:
             content = envelope.original_content.decode('utf-8').replace(
                 "\r\r\n", "\r\n")
             message = mailparser.parse_from_string(content)
             json.dump(
                 {
                     'mail_from': envelope.mail_from,
                     'rcpt_tos': envelope.rcpt_tos,
                     'subject': message.subject,
                     'body': message.body,
                     'data': content,
                     'timestamp': datetime.utcnow().isoformat()
                 }, open_file)
         return '250 OK'
     except Exception:
         return '500 Could not process your message'
Beispiel #29
0
    def __init__(self, idd, text, label):
        mail = mailparser.parse_from_string(text)
        self.text = mail.body
        self.num_attach = len(mail.attachments)
        self.text_soup = BeautifulSoup(self.text, 'html.parser')

        self.idd = idd
        self.label = label
        raw_text = self.text_soup.get_text()
        self.sents = nltk.tokenize.sent_tokenize(raw_text, language='english')
        self.num_word = len(raw_text)

        # Preprocess text
        trans = str.maketrans('', '', string.punctuation)

        raw_text = raw_text.lower().translate(trans)  # strip puncuation
        self.tokens = nltk.tokenize.word_tokenize(raw_text, language='english')

        title = mail.subject.lower().translate(trans)
        self.title_tokens = nltk.tokenize.word_tokenize(title,
                                                        language='english')

        self.avg_word_len = reduce(lambda x, y: x + len(y), self.tokens,
                                   0) / (len(self.tokens) + 1)

        self.tokens = [
            w for w in self.tokens if not w in stopwords.words('english')
        ]
        stemmer = PorterStemmer()
        self.tokens = [stemmer.stem(w) for w in self.tokens]  # remove stems

        self.title_tokens = [
            w for w in self.title_tokens if not w in stopwords.words('english')
        ]
        self.title_tokens = [stemmer.stem(w)
                             for w in self.title_tokens]  # remove stems

        self.word_freq = Counter(self.tokens)
        self.title_word_freq = Counter(self.title_tokens)
Beispiel #30
0
def return_emails_from_imap(email_account,
                            password,
                            email_folder,
                            search_term="ALL",
                            url='imap.gmail.com'):

    M = imaplib.IMAP4_SSL(url)

    try:
        rv, data = M.login(email_account, password)
    except imaplib.IMAP4.error:
        print "Login Failed. Check Credentials and imap url."
        return

    rv, data = M.select(email_folder)

    if rv != 'OK':
        print "Email folder not found."
        return

    response = []
    rv, data = M.search(None, search_term)

    if rv != 'OK':
        print "No unSeen Messages"
        return

    for num in data[0].split():
        rv, data = M.fetch(num, '(RFC822)')
        if rv != 'OK':
            print "ERROR getting message", num
            #return

        msg = email.message_from_string(data[0][1])
        parsed_msg = mailparser.parse_from_string(msg.as_string())
        response.append(parsed_msg)
    M.logout()
    return response
Beispiel #31
0
    def getEmails(self, query, fromDate):
        cibusVoucherList = []
        status, messages = self.mail.select("INBOX")
        type, data = self.mail.search(None, query)
        mail_ids = data[0]
        id_list = mail_ids.split()
        ids_len = len(id_list)
        for i in range(ids_len):
            email_id = id_list[i]
            print("{}  -  {}/{}".format(email_id, i, ids_len))
            try:
                typ, data = self.mail.fetch(email_id, '(RFC822)')

                raw_email = data[0][1]
                raw_email_string = raw_email.decode('utf-8')
                email_msg = mailparser.parse_from_string(raw_email_string)
                email_date = email_msg.date.date()

                if (email_date >= fromDate):

                    barname = re.findall(
                        'https?:\/\/(?:www\.)?mysodexo\.co\.il\/b\?([-a-zA-Z0-9()@:%_\+.~#?&=]*)',
                        email_msg.body)[0]

                    # Get voucher value
                    subject = email_msg.headers['Subject']
                    decoded_subject, charset = decode_header(subject)[0]
                    voucher_value = re.findall('[\s\S]*?([0-9]+) ',
                                               decoded_subject)[0]

                    # Go to bar url and extract barcode
                    link = "https://www.mysodexo.co.il/b?" + barname
                    f = requests.get(link)
                    barcode = re.findall('alt="([0-9]+)"', f.text)[0]

                    # Build image url
                    imgurl = 'https://www.mysodexo.co.il/b/bar.ashx?' + barname
                    title = barcode + '(' + str(voucher_value) + ')'
                    cell = '<div style="text-align:center;flex:1;font-family:Arial;margin-top:2px;margin-bottom:2px;-webkit-transform: rotate(90deg);transform: rotate(90deg);"><img height=1300 width=6000 src="' + imgurl + '" style="padding-top:8px;padding-bottom:8px;display:block;image-rendering:pixelated;" />' + barcode + '(' + str(
                        voucher_value) + ')' + '</div>'
                    response = requests.get(imgurl).raw

                    png_format = "{}\{}.png".format(MAIN_OUTPUT_FOLDER,
                                                    barcode)
                    jpeg_format = "{}\{}.jpeg".format(MAIN_OUTPUT_FOLDER,
                                                      barcode)
                    (filename,
                     headers) = urllib.request.urlretrieve(imgurl, png_format)
                    img = Image.open(png_format).convert('RGB').resize(
                        (900, 450)).save(jpeg_format, "JPEG", optimize=True)
                    cibusVoucherList.append(
                        CibusVoucher(email_date, voucher_value, barcode,
                                     jpeg_format))
                    print("Voucher for {}, value {} barcode {}".format(
                        email_date, voucher_value, barcode))
                else:
                    break
            except Exception as e:
                print(e)
                continue

        return cibusVoucherList
Beispiel #32
0
  def handle(self, *args, **options):
    query = None
    group = str(options.get('group'))
    subject = settings.EMAILS['tag'] + ' [' + str.upper(group) + '] '
    emails = ()

    # get raw email message
    message = email.message_from_string(options.get('message').read())
    mail = mailparser.parse_from_string(options.get('message').read())

    # get email parts from raw source
#    body = message.get_body()
#    attachments = message.iter_attchments()
#    body = ''
#    attachments = []
#    if message.is_multipart():
#      for part in message.walk():
#        if part.get_content_maintype() == 'multipart':
#          if part.get('Content-Disposition') is None:
#            filename = part.get_filename()
#            payload = part.get_payload(decode=True)
#            attachments.append({'name':filename,'content':payload})
#          else:
#            body += part.get_payload()
#    else:
#      body = message.part.get_payload()

    sender = str(message['from'])
    dest = str(message['to'])
    try:
      message.replace_header('Reply-To', group+'@'+settings.EMAILS['domain'])
    except KeyError:
      message.add_header('Reply-To', group+'@'+settings.EMAILS['domain'])

    subject += str(message['subject']).replace(subject,'')
    message.replace_header('Subject', subject)

#    self.stdout.write(self.style.NOTICE('''Groupmail from <'''+str(sender)+'''> to group: <'''+str(group)+'''>'''))

    # get members based on requested "group"
    query = None
    if group == 'all':
      query = Member.objects.filter(Q(status=Member.ACT) | Q(status=Member.HON) | Q(status=Member.WBE) | Q(status=Member.STB)) 
    if group == 'members':
      query = Member.objects.filter(Q(status=Member.ACT) | Q(status=Member.WBE))
    if group == 'board':
      query = Member.objects.filter(role__year=getSaison())

    # send(forward) mail to people of selected group
    server = smtplib.SMTP('localhost')
    if query is not None:
      for m in query:
        message.replace_header("To", m.email)
        server.sendmail(sender, m.email, message.as_string())

#        notify_by_email(
#		sender,
#		m.email,
#		subject,
#		body,
#		False,
#		attachments,
#		False
#        )
#        emails += (
#	  (
#          	subject,
#          	msg.as_string(),
#        	sender,
#          	[m.email,],
#	  ),
#	)
#        self.stdout.write(self.style.NOTICE('Prepared message for <'+str(m)+'>'))
#        self.stdout.write(self.style.NOTICE('Sending message for <'+str(m)+'>'))

#    send_mass_mail(emails)
    server.quit()