Graph Mail Rules¶
More information can be found in the official Graph documentation:
IMessageRule, IMessageRules¶
Get User's Message Rules for their Inbox¶
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";
const graph = graphfi(...);
// This can be any folder id or Well-known folder names
const currentUserInbox = graph.me.mailFolders.getById("inbox");
const rules = await currentUserInbox.messageRules();
Get a Message Rules for a User¶
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";
const graph = graphfi(...);
// This can be any folder id or Well-known folder names
const currentUserInbox = graph.me.mailFolders.getById("inbox");
const rule = await currentUserInbox.messageRules.getById({ruleId})();
Add a Message Rule¶
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";
import { MessageRule as IMessageRuleType } from "@microsoft/microsoft-graph-types";
const graph = graphfi(...);
const draftRule: IMessageRuleType = {
displayName: "PnPjs Test Rule",
sequence: 2,
isEnabled: true,
conditions: {
senderContains: [
"adele",
],
},
actions: {
forwardTo: [
{
emailAddress: {
name: "Alex Wilbur",
address: "AlexW@contoso.onmicrosoft.com",
},
},
],
stopProcessingRules: true,
},
};
// This can be any folder id or Well-known folder names
const currentUserInbox = graph.me.mailFolders.getById("inbox");
const rule = await currentUserInbox.messageRules.add(draftRule);
Update a Message Rule¶
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";
const graph = graphfi(...);
// This can be any folder id or Well-known folder names
const currentUserInbox = graph.me.mailFolders.getById("inbox");
const newRuleName = "My Mail Rule";
const rule = await currentUserInbox.messageRules.getById({ruleId}).update({ displayName: newRuleName });
Delete a Message Rule¶
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";
const graph = graphfi(...);
// This can be any folder id or Well-known folder names
const currentUserInbox = graph.me.mailFolders.getById("inbox");
const rule = await currentUserInbox.messageRules.getById({ruleId}).delete();