Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: 🚀 Add InboxRule implement #1272

Merged
merged 16 commits into from
Mar 3, 2024
Merged

Conversation

a76yyyy
Copy link
Contributor

@a76yyyy a76yyyy commented Feb 10, 2024

Feat #233

Example:

from exchangelib import (
    IMPERSONATION,
    Account,
    Build,
    Configuration,
    Credentials,
    Version,
)
from exchangelib.properties import Actions, Conditions, Exceptions, Rule

SERVER_DOMAIN = 'mail.example.com'
USERNAME = r'example\username'
PASSWORD = 'password123!'

TO_USER = '[email protected]'
ACCESS_TYPE = IMPERSONATION


def test_inbox_rule():
    # Create Credentials object
    credentials = Credentials(username=USERNAME, password=PASSWORD)

    # Create Configuration object, set server address and credentials
    config = Configuration(
        server=SERVER_DOMAIN,
        credentials=credentials,
        version=Version(build=Build(15, 1)),
        auth_type="basic"
    )

    # Create Account object, set email address, access type and configuration
    account = Account(
        primary_smtp_address=TO_USER,
        access_type=ACCESS_TYPE,  # Choose one from (IMPERSONATION, DELEGATE)
        config=config
    )

    print("Rules of account before creation:", account.rules, '\n')

    # Create rule conditions, sender contains 'sender_example'
    conditions = Conditions(
        contains_sender_strings=[
            "sender_example",
        ],
    )

    # Create rule actions, automatically delete emails
    actions = Actions(
        delete=True
    )

    # Create Rule instance
    rule = Rule(
        display_name='test_exchangelib_rule',
        priority=1,
        is_enabled=True,
        conditions=conditions,
        exceptions=Exceptions(),
        actions=actions,
    )

    # Create rule
    rule_id = account.create_rule(rule)
    print("Rule:", rule)
    print("Created rule with ID:", rule_id, '\n')

    if rule_id:
        # Get rule list
        print("Get Rules of account after creation:", account.rules, '\n')

        # Modify rule
        rule.rule_id = rule_id
        rule.display_name = 'test_exchangelib_rule(modified)'
        res = account.set_rule(rule)
        print("Modified rule with ID:", rule.rule_id)
        print("Response:", res.text if hasattr(res, 'text') else res)
        print("Rules of account after modification:", account.rules, '\n')

        # Delete rule
        res = account.delete_rule(rule=rule)
        print("Deleted rule with ID:", rule.rule_id)
        print("Response:", res.text if hasattr(res, 'text') else res)
        print("Rules of account after deletion:", account.rules)


if __name__ == "__main__":
    test_inbox_rule()

@a76yyyy a76yyyy changed the title Feature: 🚀 Add InboxRule implement Feat ecederstrand/exchangelib#233 Feature: 🚀 Add InboxRule implement Feb 10, 2024
@a76yyyy
Copy link
Contributor Author

a76yyyy commented Feb 10, 2024

image

I did not find a cycle import

@ecederstrand
Copy link
Owner

ecederstrand commented Feb 26, 2024

Thanks for the contribution!

We'll need to add a test case to this MR. Your example code is a really good start.

Also, I usually update the generated docs in a different MR because they get published immediately. I want to postpone that until a release hits PyPI. However, a new section in https://github.com/ecederstrand/exchangelib/blob/master/docs/index.md describing the new feature would be good.

Copy link
Owner

@ecederstrand ecederstrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, solid work. Thanks!

exchangelib/account.py Outdated Show resolved Hide resolved
exchangelib/account.py Outdated Show resolved Hide resolved
exchangelib/account.py Outdated Show resolved Hide resolved
exchangelib/account.py Show resolved Hide resolved
exchangelib/account.py Outdated Show resolved Hide resolved
@a76yyyy
Copy link
Contributor Author

a76yyyy commented Feb 27, 2024

Thanks for the contribution!

We'll need to add a test cate to this MR. Your example code is a really good start.

Also, I usually update the generated docs in a different MR because they get published immediately. I want to postpone that until a release hits PyPI. However, a new section in https://github.com/ecederstrand/exchangelib/blob/master/docs/index.md describing the new feature would be good.

I have updated index.md, but I did not continue to update the generated docs.If you think this PR is ready to merge, you can ask me to generate the latest docs (or do it by myself)

@ecederstrand
Copy link
Owner

I added a test case, made the API on Account a bit more consistent, and reverted the automatic generation of docs.

@ecederstrand ecederstrand merged commit 7401f51 into ecederstrand:master Mar 3, 2024
7 of 10 checks passed
@kingfer30
Copy link

Hello, I would like to ask if there is an interface for creating junk rules through the API?

I saw that the rule created by this rule is only valid in the rule list and not in the junk list.
image

By reading the code, I found a method "bulk_mark_as_junk", but the official document states that blocked emails need to already exist, and my scenario is that certain the person or domain is not sending me emails, but I need to configure it so that I receive emails properly and not get marked as junk.
image

That like this:
image

Is it possible to implement? I'm a newbie, very sorry if this question is stupid

@a76yyyy
Copy link
Contributor Author

a76yyyy commented Dec 4, 2024

By reading the code, I found a method "bulk_mark_as_junk", but the official document states that blocked emails need to already exist, and my scenario is that certain the person or domain is not sending me emails, but I need to configure it so that I receive emails properly and not get marked as junk.

Maybe you can configure an Inbox Rule: When you receive a mail with a specified sending address, automatically move it into junk mail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants