class MessageRulePredicates
A data class representing conditions and exceptions for email message rules in Microsoft Outlook, inheriting from ClientValue.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/messages/rules/predicates.py
7 - 37
simple
Purpose
This class encapsulates the various predicates (conditions) that can be used to define rules for processing incoming email messages in Microsoft Outlook. It allows specification of criteria such as body content, subject lines, categories, sender addresses, attachment presence, and header content. These predicates are used to determine when a mail rule should be triggered or when exceptions should apply. The class is designed to work within the Office365 SDK for Python, specifically for Outlook mail rule management.
Source Code
class MessageRulePredicates(ClientValue):
"""Represents the set of conditions and exceptions that are available for a rule."""
def __init__(
self,
body_contains=None,
body_or_subject_contains=None,
categories=None,
from_addresses=None,
has_attachments=None,
header_contains=None,
):
"""
:param list[str] body_contains: Represents the strings that should appear in the body of an incoming message
in order for the condition or exception to apply.
:param list[str] body_or_subject_contains: Represents the strings that should appear in the body or subject
of an incoming message in order for the condition or exception to apply.
:param list[str] categories: Represents the categories that an incoming message should be labeled with in
order for the condition or exception to apply.
:param list[Recipient] from_addresses: Represents the specific sender email addresses of an incoming message
in order for the condition or exception to apply.
:param bool has_attachments: Indicates whether an incoming message must have attachments in order for the
condition or exception to apply.
:param list[str] header_contains:
"""
self.bodyContains = StringCollection(body_contains)
self.bodyOrSubjectContains = StringCollection(body_or_subject_contains)
self.categories = StringCollection(categories)
self.fromAddresses = ClientValueCollection(Recipient, from_addresses)
self.hasAttachments = has_attachments
self.headerContains = StringCollection(header_contains)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ClientValue | - |
Parameter Details
body_contains: A list of strings that should appear in the body of an incoming message for the condition or exception to apply. Can be None if this condition is not needed. Each string in the list represents a keyword or phrase to match against the message body.
body_or_subject_contains: A list of strings that should appear in either the body or subject line of an incoming message for the condition or exception to apply. Can be None if not needed. This provides a broader matching criterion than body_contains alone.
categories: A list of category names (strings) that an incoming message should be labeled with for the condition or exception to apply. Can be None if category-based filtering is not required. Categories are user-defined labels in Outlook.
from_addresses: A list of Recipient objects representing specific sender email addresses. The condition or exception applies when the incoming message is from one of these addresses. Can be None if sender filtering is not needed.
has_attachments: A boolean value indicating whether an incoming message must have attachments for the condition or exception to apply. Can be None if attachment presence is not a criterion. True requires attachments, False requires no attachments.
header_contains: A list of strings that should appear in the email headers for the condition or exception to apply. Can be None if header-based filtering is not required. This allows matching against technical email header fields.
Return Value
Instantiation returns a MessageRulePredicates object that encapsulates all the specified conditions. The object contains six attributes (bodyContains, bodyOrSubjectContains, categories, fromAddresses, hasAttachments, headerContains) that store the predicates in appropriate collection types (StringCollection or ClientValueCollection). This object is typically used as part of a larger mail rule configuration in the Office365 Outlook API.
Class Interface
Methods
__init__(self, body_contains=None, body_or_subject_contains=None, categories=None, from_addresses=None, has_attachments=None, header_contains=None)
Purpose: Initializes a MessageRulePredicates instance with the specified conditions for email message filtering
Parameters:
body_contains: List of strings to match in message body, or Nonebody_or_subject_contains: List of strings to match in body or subject, or Nonecategories: List of category names to match, or Nonefrom_addresses: List of Recipient objects representing sender addresses, or Nonehas_attachments: Boolean indicating if attachments are required, or Noneheader_contains: List of strings to match in email headers, or None
Returns: None (constructor)
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
bodyContains |
StringCollection | Collection of strings that should appear in the message body for the condition to apply | instance |
bodyOrSubjectContains |
StringCollection | Collection of strings that should appear in either the body or subject for the condition to apply | instance |
categories |
StringCollection | Collection of category names that the message should be labeled with for the condition to apply | instance |
fromAddresses |
ClientValueCollection[Recipient] | Collection of Recipient objects representing sender email addresses for the condition to apply | instance |
hasAttachments |
bool | Boolean flag indicating whether the message must have attachments for the condition to apply | instance |
headerContains |
StringCollection | Collection of strings that should appear in the email headers for the condition to apply | instance |
Dependencies
office365
Required Imports
from office365.outlook.mail.message_rule_predicates import MessageRulePredicates
from office365.outlook.mail.recipient import Recipient
Usage Example
from office365.outlook.mail.message_rule_predicates import MessageRulePredicates
from office365.outlook.mail.recipient import Recipient
# Create a simple predicate with body content matching
predicates = MessageRulePredicates(
body_contains=['urgent', 'important'],
has_attachments=True
)
# Create predicates with sender filtering
sender1 = Recipient()
sender1.emailAddress = {'address': 'boss@company.com', 'name': 'Boss'}
sender2 = Recipient()
sender2.emailAddress = {'address': 'client@external.com', 'name': 'Client'}
predicates_with_senders = MessageRulePredicates(
from_addresses=[sender1, sender2],
categories=['Work', 'Priority'],
body_or_subject_contains=['meeting', 'deadline']
)
# Create predicates for header-based filtering
header_predicates = MessageRulePredicates(
header_contains=['X-Priority: 1'],
has_attachments=False
)
# Access the stored predicates
print(predicates.bodyContains) # StringCollection with ['urgent', 'important']
print(predicates.hasAttachments) # True
Best Practices
- Always instantiate with only the predicates you need; pass None for unused conditions to keep the rule definition clean
- When using from_addresses, ensure Recipient objects are properly constructed with valid email addresses
- Use body_or_subject_contains for broader matching when you don't need to distinguish between body and subject
- The class is immutable after instantiation in typical usage; create a new instance if you need different predicates
- This class is typically used as part of a MessageRule object, not standalone
- All list parameters are converted to specialized collection types (StringCollection or ClientValueCollection) internally
- Empty lists and None are treated differently; None means the condition is not evaluated, while an empty list may have different semantics
- When integrating with Office365 API, this object will be serialized to JSON, so ensure all data is JSON-serializable
- The has_attachments parameter is a simple boolean, not a collection like other parameters
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class MessageRule 72.5% similar
-
class QueryCondition 58.6% similar
-
class Message 58.2% similar
-
class Recipient 57.1% similar
-
class SPListRule 54.7% similar