=== Manage Email Rules ===
Contributors: virgial, freemius
Tags: email, redirect, wp_mail, quarantine, ai
Requires at least: 7.0
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.3.2
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Redirect, rewrite or block every outgoing WordPress email with rules: multiple conditions, wildcards & regex. Quarantine and AI email routing in Pro.

== Description ==

Manage Email Rules does for outgoing email what a redirection plugin does for URLs. Every email sent through `wp_mail()` - by WordPress core, WooCommerce, form plugins or anything else - is checked against an ordered list of rules.

**Multiple conditions · Wildcards & regex · AI email routing (Pro) · Multisite compatible**

**Conditions** can match on the WordPress email, To, Cc, Bcc, From, subject, body, any header, attachments and the email type, using equals, contains, starts/ends with, wildcards (`*@example.com`) or regular expressions.

**Actions**

* Send unchanged (an exception above broader rules)
* Redirect to other recipients (the original recipients are kept in an `X-Original-To` header)
* Add To, Cc or Bcc recipients
* Rewrite the subject, body, From or Reply-To (prefix/suffix, search and replace, placeholders)
* Block

**WordPress emails**: the plugin recognises the emails WordPress sends itself - new user, password reset and change, email address change, comments, automatic updates, privacy requests, recovery mode and the multisite emails - in any site language. Pick one on the WordPress emails screen and add a rule to block, redirect or change it.

Also included: a mail log with the sending plugin or theme, per rule hit counters, a test screen that dry-runs your rules on an example email, and automatic log cleanup.

**Multisite compatible**: on a multisite every site gets its own rules, log and settings. Pro adds one network-wide set of rules.

= Manage Email Rules Pro =

The free version gives you full control over outgoing email with rules. Pro adds a safety net and AI email routing: suspicious email waits in quarantine instead of going out, and the AI reads each email, decides what kind of email it is and routes it for you.

* **Quarantine** - hold email for review, then send or delete it, with an expiry per rule
* **AI email routing** - the AI sorts email into email types (finance, support, spam…) and one rule decides per type where it goes: redirect, Cc, hold or block
* **Email types** - accept, reject or merge the email types the AI suggests, and use them in rules
* **AI queue** - the AI reviews email in the background, so visitors never wait for it
* **Your own AI skills** - teach the AI your business: your jargon, product names, suppliers and customers, so it routes your email the way you would
* **Learning period** - register all outgoing email for 7, 14 or 30 days and let the AI suggest rules
* **Network-wide rules** - one set of rules with a central log and quarantine for all sites of a multisite

Privacy first: before anything goes to the AI, email addresses, names, phone numbers, links and codes are replaced by placeholders (see Privacy below).

**Quarantine**: hold email for review, then release (send) or delete it. Each rule sets an expiry (hours or days) and what happens then: send or delete. A global setting deletes all held email after a number of days.

**AI email routing**: one rule with the action "Process with AI", for example "To is info@example.com → Process with AI", says what happens per email type: finance → redirect to accounting@, spam → block, support → Cc helpdesk@. An email type without an action continues with the next rules. Spam only counts when the AI verdict confirms it; the built-in email types Spam and Other cannot be deleted.

**AI review** uses the AI Client built into WordPress 7.0 and the model you connect under Settings → Connectors. The model returns a verdict (allow, hold or block), an email type and a reason. Uncertain blocks are held for a human. When the AI is unavailable a configurable fallback applies.

**Background AI review** (recommended): email that needs the AI is put in an AI queue and reviewed by WP-Cron right after the page has been delivered, so a visitor who submits a form does not wait for the AI. The AI queue screen shows waiting email, with process now, delete and empty queue. Failed AI calls are retried up to 3 times before the fallback applies.

**Your own AI skills**: a general AI model does not know how your business talks. A skill is a text in which you explain it: the jargon and abbreviations of your trade, your product and project codes, your suppliers and customers, and what you want done with them. Think of an installation company whose customers write about a "callout" or a "service visit", a law firm with case numbers, or a wholesaler whose orders only mention article numbers. With a skill the AI recognises these emails and routes them correctly, for example "emails that mention a callout go to planning" or "emails from supplier ACME are always finance".

Write or paste the text under Settings → AI skills (a SKILL.md file works too) and choose the skill per rule, next to "Process with AI". Each rule can use its own skill, so the sales inbox and the service inbox can each get their own knowledge. Skills go through the same privacy filter as the email before they are sent to the AI.

**Email types**: the AI sorts email into email types such as finance, bug report or feature request. Email types it invents appear as suggestions that you accept, reject or merge. Accepted email types can be used in rules, for example "email type is finance → redirect to accounting@example.com".

**Learning period**: not sure which rules you need? Let Manage Email Rules register every outgoing email for 7, 14 or 30 days: which plugin, theme or WordPress itself sends it and to what kind of recipient. Afterwards the AI suggests rules; add one with a click (switched off, so you can check it first) or edit it before saving.

**Network-wide rules**: the free version keeps rules per site. With Pro, network-activate the plugin and choose in Network Admin between rules per site or one network-wide set of rules, with a central log and quarantine for all sites.

Pro is sold and delivered through Freemius.

= Privacy =

On activation you are asked to opt in to sharing basic usage data with Freemius (the platform used for licensing and updates). This is optional: choose "Skip" and nothing is sent. See https://freemius.com/privacy/.


Pro: the AI features only work after an administrator accepts the AI terms of use in Settings → AI settings. AI can make mistakes; the terms explain the data sharing and exclude liability. When a rule uses AI review, the subject and (a configurable part of) the body of the email are sent to the AI provider connected in WordPress, never the recipients or the sender. Before anything is sent, email addresses, phone numbers, IP addresses, links (only the domain is kept), long codes such as reset keys, and the names Manage Email Rules recognises (of the recipients, the sender, WordPress users and form fields such as "Name:") are replaced by placeholders. Names written elsewhere in the text cannot always be recognised. The learning period sends only a summary per kind of email with one example that is cleaned the same way. The email body is only stored in the log when you enable that option. Held email, including attachments, is stored in the database and in `wp-content/uploads/vber-quarantine/` until released or deleted.

= For developers =

* `vber_skip_rules` (bool, $atts) - skip the rules for a mail.
* `vber_stopped_mail_return` (bool, $decision) - return value of `wp_mail()` for blocked or held mail. Default true.
* `vber_ai_prompt_builder` ($builder, $mail) - adjust the AI prompt builder.
* `vber_ai_system_instruction` ($instruction) - adjust the AI system instruction.
* `vber_mail_evaluated` ($result, $mail, $log_id) - action after the rules ran.
* `vber_wp_emails` ($entries) - add emails to the WordPress emails list; mark the mail with the header `X-Vber-WP-Email: <key>` (see `Vber_WP_Emails::add_header()`).
* `vber_show_upgrade_promo` (bool) - show or hide the upgrade promotion in the free version.
* `vber_pre_ai_review` (null, $mail, $skill_id) - Pro: return a review array to replace the AI call (own classifier).
* `vber_ai_queue_loopback_timeout` (0.01) - Pro: timeout of the loopback request that starts the AI queue.

= About the developer =

Manage Email Rules is made by the developer of [Manage Notification E-mails](https://wordpress.org/plugins/manage-notification-emails/), used on more than 100,000 WordPress sites with over 850,000 downloads. Manage Email Rules builds on that experience: instead of switching notification emails on or off, you decide with rules what happens to every email your site sends.

== Installation ==

1. Install and activate the plugin.
2. Go to Email Rules → Rules in the admin menu and add rules.
3. Pro: connect an AI provider under Settings → Connectors to use AI review.

== Frequently Asked Questions ==

= Can I switch off WordPress emails like password change notifications? =

Yes. Go to Email Rules → WordPress emails, pick the email and click "Add rule", then choose Block (or redirect it).

= Does it work with SMTP plugins? =

Yes. Rules run in the `wp_mail` and `pre_wp_mail` filters, before the mail is handed to PHPMailer or an SMTP/API plugin.

= What happens to held email without an active Pro license? =

It is sent, with a note in the log, so no email is lost. Email that was already in the quarantine stays stored.

= What does wp_mail() return for blocked mail? =

`true` by default, so forms do not show an error for mail that was stopped on purpose. Use the `vber_stopped_mail_return` filter to change this.

= Is a released mail checked by the rules again? =

No. It is sent exactly as shown in the quarantine.

= How do I send all email of a staging or development site to myself? =

Add a rule without conditions and the action "Redirect to other recipients" with your own address. Every email then goes to you instead of the real recipients; the original recipients are kept in an `X-Original-To` header. To recognise them, set the rule to continue with the next rule and add a second rule "Rewrite" with a subject prefix such as `[TEST]`.

= Can I handle email of one specific plugin, for example a contact form? =

Yes. Use the condition "Sent by" and pick the plugin, theme or WordPress core. Manage Email Rules sees which code called `wp_mail()`, so it works with any plugin without special support. Combine it with a subject condition when a plugin sends several kinds of email.

= Can I switch off parts of the plugin I do not use? =

Yes. Every feature is a module with its own switch on Email Rules → Settings → General: WordPress emails, Email log and Test, and in Pro Quarantine, Process with AI, AI queue, AI skills and Learning period. A module that is off adds nothing to the menu, the settings or the handling of email, and its data is kept. Rules that use a module that is off do nothing for that part until you switch it on again.

= How can I try a rule without sending email? =

Use Email Rules → Test. Fill in an example email (you can simulate a WordPress email, the sending plugin and the AI result) and see which rule matches and what the email would look like. Nothing is sent.

= Does the email log store the content of my emails? =

Only when you switch on "Log email body" under Settings → Logs. By default the log keeps the date, subject, recipients, status and matched rules. Entries are removed after 30 days, or the number of days you choose.

= Which AI does Manage Email Rules Pro use, and what does it send? =

The AI provider you connect in WordPress under Settings → Connectors (WordPress 7.0 and later). Your API key stays there. For "Process with AI" the subject and body of the email are sent, never the recipients or the sender. Email addresses, phone numbers, IP addresses, links (only the domain is kept), long codes and the names Manage Email Rules recognises are replaced by placeholders before anything is sent; names written elsewhere in the text cannot always be recognised. The learning period only sends a summary per kind of email with one example that is cleaned the same way. You accept the AI terms of use before the AI is used.

= Can I teach the AI the jargon of my business? =

Yes, with AI skills (Pro). A skill is a text in which you explain the terms, abbreviations, product codes, suppliers and customers of your trade, and what should happen with their email. You choose a skill per "Process with AI" rule, so the AI sorts your email with the knowledge of your own business instead of only general knowledge.

= Does the AI review slow down my site? =

Not with the AI queue module (Pro): email that needs the AI waits in the queue and is reviewed right after the page has been delivered, so visitors never wait. Rules without AI are handled immediately.

= What is the learning period? =

A Pro feature that registers every outgoing email for 7, 14 or 30 days: which plugin, theme or WordPress itself sends it, the subject and who receives it. Afterwards the AI suggests rules for the email your site actually sends. Suggested rules are added switched off, so you decide when they take effect.

= Does it work on a multisite? =

Yes. By default every site has its own rules. With Pro you can manage one set of rules with a central log and quarantine for the whole network.

= What is removed when I delete the plugin? =

All rules, settings, logs, the quarantine and the other plugin data, on every site of a multisite.

== Screenshots ==

1. The rules list: every outgoing email is checked from top to bottom, with a switch per rule.
2. A rule for the contact form: "Sent by" Contact Form 7, and per email type what happens (Pro, Process with AI).
3. WordPress emails: the emails WordPress sends itself, with the rules that apply and "Add rule".
4. The email log: what happened to every email, with the sending plugin and the AI result.
5. Test: try the rules on an example email without sending anything.
6. The quarantine: held email waits until you release or delete it, or until it expires (Pro).
7. The learning period: suggested rules for the email your site actually sends (Pro).
8. Settings: switch each module on or off.

== Changelog ==

= 1.3.2 =
* First release.
