Example #1
0
def get_bulk_email(site, filename):
    """ Show a specific bulk email saved on the disk """
    save_path = get_log_dir(site)
    join = os.path.join

    if save_path:
        save_path = join(save_path, 'sent-bulk')
        if os.path.isdir(save_path):
            message_path = join(save_path, filename)

            try:
                message_file = open(message_path, 'r+')
            except IOError:
                return None
            mail = message_from_file(message_file)
            message_file.close()

            # Prepare the date to be formatted with utShowFullDateTime
            date = email_utils.parsedate_tz(mail.get('Date', ''))
            date = email_utils.mktime_tz(date)
            date = datetime.fromtimestamp(date)

            return {
                'subject': mail.get('Subject', '(no-subject)'),
                'content': mail.get_payload(decode=True).replace(
                    '\n\n', '</p><p>').replace('\n', '<br/>'),
                'recipients': mail.get_all('To'),
                'sender': mail.get('From'),
                'date': date,
            }
Example #2
0
def get_webex_email(site, filename, where_to_read="sent-webex"):
    """ Show a specific webex email saved on the disk """
    save_path = get_log_dir(site)
    join = os.path.join

    if save_path:
        save_path = join(save_path, where_to_read)
        if os.path.isdir(save_path):
            message_path = join(save_path, filename)

            try:
                message_file = open(message_path, "r+")
            except IOError:
                return None
            mail = message_from_file(message_file)
            message_file.close()

            # Prepare the date to be formatted with utShowFullDateTime
            date = email_utils.parsedate_tz(mail.get("Date", ""))
            date = email_utils.mktime_tz(date)
            date = datetime.fromtimestamp(date)

            return {
                "subject": mail.get("Subject", "(no-subject)"),
                "content": mail.get_payload(decode=True).replace("\n\n", "</p><p>").replace("\n", "<br/>"),
                "recipients": mail.get_all("To"),
                "sender": mail.get("From"),
                "date": date,
                "webex": mail.get("X-Accept-Webex-Data", ""),
            }
Example #3
0
def get_webex_email(site, filename, where_to_read='sent-webex'):
    """ Show a specific webex email saved on the disk """
    save_path = get_log_dir(site)
    join = os.path.join

    if save_path:
        save_path = join(save_path, where_to_read)
        if os.path.isdir(save_path):
            message_path = join(save_path, filename)

            try:
                message_file = open(message_path, 'r+')
            except IOError:
                return None
            mail = message_from_file(message_file)
            message_file.close()

            # Prepare the date to be formatted with utShowFullDateTime
            date = email_utils.parsedate_tz(mail.get('Date', ''))
            date = email_utils.mktime_tz(date)
            date = datetime.fromtimestamp(date)

            return {
                'subject': mail.get('Subject', '(no-subject)'),
                'content': mail.get_payload(decode=True).replace(
                    '\n\n', '</p><p>').replace('\n', '<br/>'),
                'recipients': mail.get_all('To'),
                'sender': mail.get('From'),
                'date': date,
                'webex': mail.get('X-Accept-Webex-Data', '')
            }
Example #4
0
def _get_message_path(site, where_to_read):
    save_path = get_log_dir(site)
    if not save_path:
        return None
    save_path = os.path.join(save_path, where_to_read)
    if not os.path.isdir(save_path):
        return None
    return save_path
Example #5
0
def _get_message_path(site, where_to_read):
    save_path = get_log_dir(site)
    if not save_path:
        return None
    save_path = os.path.join(save_path, where_to_read)
    if not os.path.isdir(save_path):
        return None
    return save_path
Example #6
0
def save_webex_email(site,
                     addr_to,
                     addr_from,
                     subject,
                     content,
                     where_to_save='sent-webex',
                     others=None):
    """
    Save webex email on disk.
    `addr_to` is a list; if there is more than one recipient,
    adds a 'To' header with each email address.

    """
    save_path = get_log_dir(site)
    join = os.path.join
    filename = None

    if save_path:
        save_path = join(save_path, where_to_save)
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        # Generate email filename according to zope.sendmail.maildir
        # but instead of hostname use site_id
        randmax = 0x7fffffff
        timestamp = int(time.time())
        unique = '%d.%d.%s.%d' % (timestamp, os.getpid(), site.getId(),
                                  random.randrange(randmax))
        filename = join(save_path, unique)
        message_file = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY,
                               0600)
        generator = email_generator.Generator(os.fdopen(message_file, 'w'))

        # Add multiple 'To' headers if there is more than one recipient
        if len(addr_to) > 1:
            email_message = create_message(content, addr_to[0], addr_from,
                                           subject)
            addr_to.remove(addr_to[0])
            for mail in addr_to:
                email_message['To'] = mail
        else:
            email_message = create_message(content, addr_to, addr_from,
                                           subject)
        # hide meeting info in header field
        email_message['X-Accept-Webex-Data'] = json.dumps(others)

        # Save email in specified file
        generator.flatten(email_message)
    else:
        mail_logger.warning("The webex email could not be saved on the disk."
                            "There is missing configuration(SITES_LOG_PATH)."
                            "Please contact the platform maintainers.")
    return filename
Example #7
0
def save_bulk_email(site,
                    addr_to,
                    addr_from,
                    subject,
                    content,
                    where_to_save='sent-bulk',
                    addr_cc=[]):
    """
    Save bulk email on disk.
    `addr_to` is a list; if there is more than one recipient,
    adds a 'To' header with each email address.

    """
    save_path = get_log_dir(site)
    join = os.path.join
    filename = None

    if save_path:
        save_path = join(save_path, where_to_save)
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        # Generate email filename according to zope.sendmail.maildir
        # but instead of hostname use site_id
        randmax = 0x7fffffff
        timestamp = int(time.time())
        unique = '%d.%d.%s.%d' % (timestamp, os.getpid(), site.getId(),
                                  random.randrange(randmax))
        filename = join(save_path, unique)
        message_file = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY,
                               0600)
        generator = email_generator.Generator(os.fdopen(message_file, 'w'))

        # Add multiple 'To' headers if there is more one receipent
        if len(addr_to) > 1:
            email_message = create_message(content, addr_to[0], addr_from,
                                           subject)
            addr_to.remove(addr_to[0])
            for mail in addr_to:
                email_message['To'] = mail
        else:
            email_message = create_message(content, addr_to, addr_from,
                                           subject)
        for mail in addr_cc:
            email_message['Cc'] = mail
        # Save email in specified file
        generator.flatten(email_message)
    else:
        mail_logger.warning("The bulk email could not be saved on the disk."
                            " Missing configuration for SITES_LOG_PATH?")
    return filename
Example #8
0
def save_webex_email(site, addr_to, addr_from, subject, content,
                     where_to_save='sent-webex', others=None):
    """
    Save webex email on disk.
    `addr_to` is a list; if there is more than one recipient,
    adds a 'To' header with each email address.

    """
    save_path = get_log_dir(site)
    join = os.path.join
    filename = None

    if save_path:
        save_path = join(save_path, where_to_save)
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        # Generate email filename according to zope.sendmail.maildir
        # but instead of hostname use site_id
        randmax = 0x7fffffff
        timestamp = int(time.time())
        unique = '%d.%d.%s.%d' % (timestamp, os.getpid(), site.getId(),
                                  random.randrange(randmax))
        filename = join(save_path, unique)
        message_file = os.open(filename,
                               os.O_CREAT | os.O_EXCL | os.O_WRONLY,
                               0600)
        generator = email_generator.Generator(os.fdopen(message_file, 'w'))

        # Add multiple 'To' headers if there is more than one recipient
        if len(addr_to) > 1:
            email_message = create_message(content, addr_to[0], addr_from,
                                           subject)
            addr_to.remove(addr_to[0])
            for mail in addr_to:
                email_message['To'] = mail
        else:
            email_message = create_message(content, addr_to, addr_from,
                                           subject)
        # hide meeting info in header field
        email_message['X-Accept-Webex-Data'] = json.dumps(others)

        # Save email in specified file
        generator.flatten(email_message)
    else:
        mail_logger.warning("The webex email could not be saved on the disk."
                            "There is missing configuration(SITES_LOG_PATH)."
                            "Please contact the platform maintainers.")
    return filename
Example #9
0
def save_bulk_email(site, addr_to, addr_from, subject, content,
                    where_to_save='sent-bulk', addr_cc=[]):
    """
    Save bulk email on disk.
    `addr_to` is a list; if there is more than one recipient,
    adds a 'To' header with each email address.

    """
    save_path = get_log_dir(site)
    join = os.path.join
    filename = None

    if save_path:
        save_path = join(save_path, where_to_save)
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        # Generate email filename according to zope.sendmail.maildir
        # but instead of hostname use site_id
        randmax = 0x7fffffff
        timestamp = int(time.time())
        unique = '%d.%d.%s.%d' % (timestamp, os.getpid(), site.getId(),
                                  random.randrange(randmax))
        filename = join(save_path, unique)
        message_file = os.open(filename,
                               os.O_CREAT|os.O_EXCL|os.O_WRONLY,
                               0600)
        generator = email_generator.Generator(os.fdopen(message_file, 'w'))

        # Add multiple 'To' headers if there is more one receipent
        if len(addr_to) > 1:
            email_message = create_message(content, addr_to[0], addr_from,
                                           subject)
            addr_to.remove(addr_to[0])
            for mail in addr_to:
                email_message['To'] = mail
        else:
            email_message = create_message(content, addr_to, addr_from,
                                           subject)
        for mail in addr_cc:
            email_message['Cc'] = mail
        # Save email in specified file
        generator.flatten(email_message)
    else:
        mail_logger.warning("The bulk email could not be saved on the disk."
                            " Missing configuration for SITES_LOG_PATH?")
    return filename
Example #10
0
def get_bulk_emails(site):
    """ Show all bulk emails saved on the disk """
    save_path = get_log_dir(site)
    join = os.path.join
    emails = []

    if save_path:
        save_path = join(save_path, 'sent-bulk')
        if os.path.isdir(save_path):
            # Get all messages files
            messages = [join(save_path, filename)
                        for filename in os.listdir(save_path)
                        if not filename.startswith('.')]

            # Sort them descending by the last modification time
            sorted_messages = [(message, os.path.getmtime(message))
                               for message in messages]
            sorted_messages.sort(key=lambda x: x[1], reverse=True)
            messages = [message[0] for message in sorted_messages]

            for message in messages:
                message_file = open(message, 'r+')
                mail = message_from_file(message_file)
                message_file.close()

                # Prepare the date to be formatted with utShowFullDateTime
                date = email_utils.parsedate_tz(mail.get('Date', ''))
                date = email_utils.mktime_tz(date)
                date = datetime.fromtimestamp(date)

                emails.append({
                    'subject': mail.get('Subject', '(no-subject)'),
                    'content': mail.get_payload(decode=True),
                    'recipients': mail.get_all('To'),
                    'sender': mail.get('From'),
                    'date': date,
                    'filename': os.path.split(message)[-1]
                })

    return emails