=== EmailAutoPost ===
Contributors: dizwebdesign
Tags: email, imap, post, automation
Requires at least: 6.0
Tested up to: 6.7
Requires PHP: 7.4
Stable tag: 0.8.7
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Create WordPress posts from new IMAP mail using configurable templates. Optional MIME attachment import (PDFs, documents, images, text) stores images in DWD Gallery when that plugin is active (Media Library fallback) and appends allowed files to imported posts.

== Description ==

EmailAutoPost connects to one or more IMAP accounts, reads new (unseen) messages, and creates posts. You can control senders with separate allowlist and blocklist screens, per-sender post status, optional subject-based publish rules, post templates with categories and placeholders, featured images from email (with global and per-sender-rule fallbacks), optional **email attachments** (Mail & import → per-type toggles, size limits, `wp_check_filetype_and_ext` verification), replay template output on the Imports screen from each post’s saved email snapshot, browse recent mail in the admin inbox with import status, run a mail check per account, and use **Settings** for accounts and last-run stats. **Allow posting from email** can be toggled from **EmailAutoPost** (home), **Mail & import**, or **Settings** (one shared option).

== Installation ==

1. Upload the plugin folder to `/wp-content/plugins/` or install the zip from Plugins → Add New → Upload.
2. Activate the plugin through the **Plugins** menu.
3. Under **EmailAutoPost → Settings**, add at least one mail account (IMAP host, user, password, folder) and choose whether posting from email is on. Under **Mail & import**, choose a **post author** (users with the Author role only) and set shared import options as needed.
4. Under **EmailAutoPost → Sender lists**, add allowlist/blocklist entries and use **Template & status** for allowlisted senders. Under **Mail & import**, set **Sender → template** and **Sender → post status** rules. Under **Post templates**, define layouts and categories.
5. Use **Run mail check now** or wait for the scheduled check.

== Frequently Asked Questions ==

= Does the server need the PHP IMAP extension? =

Yes, for live mailbox access. The admin screen shows a notice if `imap` is missing.

= Activity says “Mail batch finished … unseen_in_batch=0” — why no posts? =

`unseen_in_batch` is the number of message UIDs PHP’s `imap_search( …, 'UNSEEN' )` returned for your configured folder(s), before any import. **Zero** means the server reported no unread messages there (often everything was already read in another mail client, or mail is in a different folder than the account’s **Folder** setting). It does not by itself mean templates or posting are broken; check **EmailAutoPost → Activity** for `mail_batch_skipped` or import errors, and confirm unread mail exists in that IMAP folder.

= What does “Allow posting from email” do? =

When unchecked, cron and **Run mail check now** / **Check mail now** do **not** open IMAP or create posts (`mail_batch_skipped` in Activity). It does not delete accounts or templates. New imports can still be **Published** (not draft) when posting is on, depending on **Subject → publish**, sender status rules, and allowlist profiles—see the next FAQ.

= I expected drafts but no drafts appear — where are my posts? =

Imports are not always **Draft**. Under **Mail & import**, **Subject → publish** can force **Published** when the subject matches a term; **Sender → post status** (same screen) and allowlist **Template & status** (Sender lists) can also set **Published**. The default status for everything else is **Default status** on Mail & import (often draft). Check **Posts → All Posts** (all statuses, not only Drafts) and sort by date. If nothing appears, open **EmailAutoPost → Activity** and look at the latest `mail_batch` line (`posted`, `skipped_sender`, `skipped_dup`, `unseen_in_batch`, `per_account_unseen`) plus any `import_skipped_batch`, `import_failed`, or `post_imported` entries.

== Changelog ==

= 0.8.7 =
* Multisite: network activation/deactivation, new-site bootstrap, per-site uninstall loop.

= 0.8.6 =
* Featured images and image attachments store in DWD Gallery first when that plugin is active; WordPress Media Library is the fallback.

= 0.8.3 =
* **Mail inbox:** bulk **Remove selected from list** / **Delete selected from server** send `eap_inbox_bulk_mode` via the submit button (`name`/`value`) so the server always receives `hide` or `delete_server` (fixes **Invalid action.** when JS did not update the old hidden field).

= 0.8.2 =
* **Master posting toggle:** same **Allow posting from email** control on **EmailAutoPost** (home), **Mail & import**, and **Settings**; save returns to the screen you used. `EAP_Sanitize::enabled_flag()` aliases checkbox sanitization. Readme FAQ clarifies pause vs draft/published behavior.

= 0.8.1 =
* **Admin:** **Sender → template** and **Sender → post status** are edited again on **Mail & import** (with media pickers on that screen). **Sender lists** is allowlist/blocklist plus per-allowlist **Template & status** only. **Settings** section order: importing toggle, mail accounts, then Plugin update and last run stats.

= 0.8.0 =
* **Mail inbox (message view):** attachment filenames (and an **Open** button) load a secure **admin-ajax** preview in a modal (PDF, images, plain text/csv inline when the browser allows; other types as download / browser handler). Respects **max size per attachment** from Mail & import.
* **Docs:** Installation step clarifies **Sender lists** vs **Post templates** (layouts).

= 0.7.9 =
* **Mail inbox:** **Remove from list** (per row, message view, and bulk) hides messages in the plugin only—they stay on the IMAP server. **Show hidden messages again** clears that memory for the account. Server delete still removes mail from the server and also drops those UIDs from the hidden list. Option `eap_inbox_hidden_uids` is included in Tools export/uninstall.

= 0.7.8 =
* **Mail inbox:** after deleting on the server, the list hides messages that still carry the IMAP “deleted” flag until the server removes them; the IMAP stream is closed with **CL_EXPUNGE** so expunge is applied reliably. Redirects add a cache-busting query arg and the inbox screen sends no-cache headers so the list is not a stale browser snapshot.

= 0.7.7 =
* **Mail inbox:** when manual delete is allowed for the account, the message list includes a checkbox per row, “select all” in the header, and **Delete selected from server** (one IMAP session).

= 0.7.6 =
* **Tools:** export all plugin settings to a JSON file (mail accounts including stored credentials, templates, lists, options) and import that file to restore after reinstall or on another site. Activity log is not included in the export.

= 0.7.5 =
* **Mail inbox:** message view lists MIME attachment parts (name, type, part number, approximate size). No file download from the server in this screen.

= 0.7.4 =
* **Mail batch / Activity:** when UNSEEN messages were selected but `posted` is still 0, the `mail_batch` log message adds guidance to compare counters and review Activity (`import_skipped_batch`, `import_failed`). **readme.txt** FAQ explains draft vs published imports and using **Posts → All Posts** plus Activity.

= 0.7.3 =
* **Activity / mail batch:** `mail_batch` log includes `per_account_unseen` (UNSEEN UID count per account). When nothing was selected and nothing posted, the message adds a short explanation. **Settings → Last mail run** clarifies that “Unseen in batch” is the IMAP UNSEEN count, not posts created. **readme.txt** FAQ explains `unseen_in_batch=0`.

= 0.7.2 =
* **Mail batch:** one message failing template resolution no longer aborts the entire run (remaining UNSEEN messages in the batch can still import). Persistent admin warning when posting from email is off. Guard if `wp_insert_post` returns no ID.

= 0.7.1 =
* **Admin:** new **Settings** submenu (mail accounts, importing on/off, last mail run, Plugin update); **Mail & import** keeps policy, subject rules, and global import form (posting toggle removed from that save). **Sender → template** and **Sender → post status** moved to **Sender lists**. Dashboard is quick links only. Redirects and debug links updated.

= 0.7.0 =
* **Sender → template:** optional **Subject contains** per row (substring match; case follows Subject → publish). Among rows that match the sender, subject-specific rows win in table order, then rows with an empty subject act as the **default template for that sender**. Sender lists rules run before Allowlist “Template & status”; Imports preview uses the same resolution.

= 0.6.9 =
* **Post templates:** category picker now uses WordPress core `wp_terms_checklist()` (hierarchical tree, popular terms, full category list) instead of a flat capped list.

= 0.6.8 =
* **Security:** email HTML and plain bodies are normalized and sanitized (`EAP_Sanitize::import_email_html` / `import_email_plain`) before templates, `_eap_import_vars` meta, featured-image URL extraction, Imports template preview, and Mail inbox message view; filter `eap_import_email_html` for advanced sites.

= 0.6.7 =
* **Attachments:** optional MIME attachment import (DB 7): decode parts on the open IMAP connection, caps per file / per message / count; Mail & import checkboxes for **images**, **PDFs** (Media Library + download link; no iframe), **other allowed files** (Office, ZIP, media, etc.), and **text/csv** (escaped pre block); dangerous extensions rejected; uninstall deletes new options.

= 0.6.6 =
* **Activity log:** option `eap_activity_log` (last 300 events): mail batches, post imports, IMAP tests, and admin saves; **EmailAutoPost → Activity** screen with **Clear log**; uninstall removes the option.

= 0.6.5 =
* **Allowlist sender:** submenu link is hidden with CSS (page stays registered) so **Template & status** URLs still work; bare profile URL redirects to Sender lists.
* **Mail check:** last run stores **unseen messages in batch**; success notices and dashboard **Last mail run** show the count.
* **Importing:** option **Allow posting from email** (on/off) on Dashboard and Mail & import; when off, cron and manual checks do not connect or create posts.

= 0.6.4 =
* **Allowlist sender:** keep the admin submenu registered (do not remove it) so **Template & status** / direct `page=email-autopost-allowlist-sender` URLs pass WordPress capability checks.

= 0.6.3 =
* **Allowlist senders:** per-entry **Template & status** (hidden admin page): choose post template, Draft or Published, optional featured image when sender policy is Allowlist; Mail & import sender rules remain fallback when fields are unset. Profiles removed when an allowlist line is deleted.

= 0.6.2 =
* **Imported posts:** **Template preview** replays placeholders from the email snapshot saved at import (older posts use current title/content as a fallback). Choose any template on the preview panel without changing the post.

= 0.6.1 =
* **Sender → template:** optional **featured image per sender rule** (fallback when the email has no usable inline image; Mail & import default still applies otherwise).

= 0.6.0 =
* **Sender lists:** separate options `eap_sender_allowlist` and `eap_sender_blocklist`; **Sender lists** admin screen (add/remove); legacy `eap_sender_list` migrates on upgrade (DB 5). Mail & import links here instead of a single textarea. Inbox add-sender respects policy (policy “none” offers both lists).

= 0.5.0 =
* **Mail inbox:** columns for imported post / status; **Actions** with Open and policy-labeled add-to-list; list uses same secure handler as message view.
* **Sender → post status** rules (after subject checks); **Check mail now** per enabled account; **default featured image** + first image from email HTML on import.
* **Templates:** assign **post categories** per template (DB migration 4).

= 0.4.0 =
* Admin **Mail inbox** (read messages, add parsed sender to the sender list, optional delete on server when enabled per account).
* **Sender → template** rules, per-template **placeholder parts** (strip unused fields before templating), per-account IMAP delete options (blocked senders, after import, manual inbox delete).
* DB migration 3: removes legacy public front page sync; uninstall cleans `eap_sender_template_rules`.

= 0.3.0 =
* Post author picker (Author role only), global fingerprint dedup across mail accounts when Message-ID is missing, template **Preview** with sample data, **Plugin Update** button with version notices.

= 0.2.0 =
* Multiple IMAP mail accounts with migration from legacy single-account options; per-account test and batch processing across accounts.

= 0.1.1 =
* Align with workspace standards: dashboard **Run update & sync pages**, central `eap_sanitize_request_*` helpers with invalid UTF-8 handling, readme/changelog, Cursor workspace overlay.

= 0.1.0 =
* Initial release: IMAP fetch, templates, imports list, cron, encrypted password storage, duplicate-install warning, debug mode.
