コード例 #1
0
def extract_command_from_subject(message):
    """
    Returns a command found in the subject of the email.

    :param message: An email message.
    :type message: :py:class:`email.message.Message` or an object with
        an equivalent interface
    """
    subject = decode_header(message.get('Subject'))
    if not subject:
        return []
    match = re.match(r'(?:Re\s*:\s*)?(.*)$', subject, re.IGNORECASE)
    return ['# Message subject', match.group(1) if match else subject]
コード例 #2
0
def send_response(original_message, message_text, recipient_email, cc=None):
    """
    Helper function which sends an email message in response to a control
    message.

    :param original_message: The received control message.
    :type original_message: :py:class:`email.message.Message` or an object with
        an equivalent interface
    :param message_text: The text which should be included in the body of the
        response.
    :param cc: A list of emails which should receive a CC of the response.
    """
    subject = unfold_header(decode_header(original_message.get('Subject', '')))
    if not subject:
        subject = 'Your mail'
    message_id = unfold_header(original_message.get('Message-ID', ''))
    references = unfold_header(original_message.get('References', ''))
    if references:
        references += ' '
    references += message_id
    message = EmailMessage(
        subject='Re: ' + subject,
        to=[unfold_header(original_message['From'])],
        cc=cc,
        from_email=DISTRO_TRACKER_BOUNCES_EMAIL,
        headers={
            'From': DISTRO_TRACKER_CONTACT_EMAIL,
            'X-Loop': DISTRO_TRACKER_CONTROL_EMAIL,
            'References': references,
            'In-Reply-To': message_id,
        },
        body=message_text,
    )

    logger.info("control => %(to)s %(cc)s", {
        'to': recipient_email,
        'cc': " ".join(cc) if cc else "",
    })
    message.send()