Created
February 5, 2026 22:50
-
-
Save stingray82/2451942655535ff608bd038ecbecbbe3 to your computer and use it in GitHub Desktop.
Pipe Email example using fluentsupport and flowmattic IMAP data (Simulate)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Fluent Support Piping Text #6 */ | |
| if (!defined('ABSPATH')) exit; | |
| add_action('admin_bar_menu', function ($wp_admin_bar) { | |
| if (!current_user_can('manage_options') || !is_admin_bar_showing()) { | |
| return; | |
| } | |
| $url = wp_nonce_url( | |
| admin_url('admin-post.php?action=fs_pipe_rest_test_box2'), | |
| 'fs_pipe_rest_test_box2' | |
| ); | |
| $wp_admin_bar->add_node([ | |
| 'id' => 'fs-pipe-rest-test-box2', | |
| 'title' => 'FS Pipe Test (Box 2 • REST)', | |
| 'href' => $url, | |
| 'meta' => [ | |
| 'title' => 'Apply email_piping_raw_data filter and POST the curl payload to Box 2 piping endpoint', | |
| ], | |
| ]); | |
| }, 100); | |
| add_action('admin_post_fs_pipe_rest_test_box2', function () { | |
| if (!current_user_can('manage_options')) { | |
| wp_die('Forbidden', 403); | |
| } | |
| check_admin_referer('fs_pipe_rest_test_box2'); | |
| // Core Fluent Support MailBox model (not Pro). | |
| $mailboxClass = '\FluentSupport\App\Models\MailBox'; | |
| if (!class_exists($mailboxClass)) { | |
| fs_pipe_rest_test_notice('error', 'Fluent Support MailBox model not found. Is Fluent Support (free/core) active?'); | |
| wp_safe_redirect(wp_get_referer() ?: admin_url()); | |
| exit; | |
| } | |
| // Load mailbox #2 (fallback to first email mailbox). | |
| $box = $mailboxClass::find(2); | |
| if (!$box) { | |
| $box = $mailboxClass::where('box_type', 'email')->first(); | |
| } | |
| if (!$box) { | |
| fs_pipe_rest_test_notice('error', 'No email mailbox found to test with.'); | |
| wp_safe_redirect(wp_get_referer() ?: admin_url()); | |
| exit; | |
| } | |
| // Ensure token exists (same logic as controller’s getBoxSecret()). | |
| $token = $box->getMeta('_webhook_token'); | |
| if (!$token) { | |
| $token = substr(md5(wp_generate_uuid4()) . '_' . $box->id . '_' . mt_rand(100, 10000), 0, 16); | |
| $box->saveMeta('_webhook_token', $token); | |
| } | |
| // REST endpoint shape you used successfully. | |
| $endpoint = rest_url('fluent-support/v2/mail-piping/' . $box->id . '/push/' . $token); | |
| // Note could actually create my own API endpoint mimicing the orginal here | |
| // --- SAME "decoded payload" as my curl example --- | |
| $data = [ | |
| 'date' => '2026-02-05 21:58:30', | |
| 'subject' => 'Example Including Ingestion', | |
| 'body_text' => '<p>Test this?? it includes an image and an attachment</p><p><img src="https://fluentsupport.wpcode.dev/wp-content/uploads/qy0ovkii.png" /></p>', | |
| 'messageId' => 'em9cacba6f-2cc5-4e74-887a-95dacddb6f23@gmail.com', | |
| 'from' => [ | |
| 'value' => [ | |
| [ | |
| 'name' => 'Nathan Foley', | |
| 'address' => 'nathanfoley@gmail.com', | |
| ] | |
| ], | |
| 'text' => 'Nathan Foley <nathanfoley@gmail.com>', | |
| ], | |
| 'to' => [ | |
| 'value' => [], | |
| 'text' => '', | |
| ], | |
| 'forwarded' => null, | |
| 'attachments' => [ | |
| [ | |
| 'url' => 'https://fluentsupport.wpcode.dev/wp-content/uploads/qy0ovkii.png', | |
| 'cid' => null, | |
| 'filename' => 'qy0ovkii.png', | |
| 'contentType' => 'image/png', | |
| 'contentDisposition'=> 'attachment', | |
| ], | |
| [ | |
| 'url' => 'https://fluentsupport.wpcode.dev/wp-content/uploads/readme.txt', | |
| 'cid' => null, | |
| 'filename' => 'readme.txt', | |
| 'contentType' => 'text/plain', | |
| 'contentDisposition'=> 'attachment', | |
| ], | |
| ], | |
| 'isMarkDown' => false, | |
| ]; | |
| // Apply the SAME filter as EmailBoxController::pipePayload() | |
| $data = apply_filters('fluent_support_pro/email_piping_raw_data', $data, $box); | |
| // The controller expects payload as a JSON string inside a request param named "payload". | |
| $payloadString = wp_json_encode($data); | |
| // IMPORTANT: Send as x-www-form-urlencoded (because $request->get('payload') needs request params). | |
| $resp = wp_remote_post($endpoint, [ | |
| 'timeout' => 20, | |
| 'headers' => [ | |
| 'Content-Type' => 'application/x-www-form-urlencoded', | |
| ], | |
| 'body' => [ | |
| 'payload' => $payloadString, | |
| ], | |
| ]); | |
| if (is_wp_error($resp)) { | |
| fs_pipe_rest_test_notice('error', 'Request failed: ' . $resp->get_error_message()); | |
| wp_safe_redirect(wp_get_referer() ?: admin_url()); | |
| exit; | |
| } | |
| $code = wp_remote_retrieve_response_code($resp); | |
| $body = wp_remote_retrieve_body($resp); | |
| // Try to keep the notice readable. | |
| $short = $body; | |
| if (is_string($short) && strlen($short) > 3000) { | |
| $short = substr($short, 0, 3000) . '…(truncated)'; | |
| } | |
| fs_pipe_rest_test_notice( | |
| ($code >= 200 && $code < 300) ? 'success' : 'error', | |
| 'HTTP ' . $code . ' Response: ' . $short | |
| ); | |
| wp_safe_redirect(wp_get_referer() ?: admin_url()); | |
| exit; | |
| }); | |
| add_action('admin_notices', function () { | |
| if (!current_user_can('manage_options')) { | |
| return; | |
| } | |
| $notice = get_transient('fs_pipe_rest_test_notice'); | |
| if (!$notice || !is_array($notice)) { | |
| return; | |
| } | |
| delete_transient('fs_pipe_rest_test_notice'); | |
| $type = in_array($notice['type'], ['success','error','warning','info'], true) ? $notice['type'] : 'info'; | |
| echo '<div class="notice notice-' . esc_attr($type) . ' is-dismissible"><p>' | |
| . esc_html($notice['message']) | |
| . '</p></div>'; | |
| }); | |
| function fs_pipe_rest_test_notice($type, $message) { | |
| set_transient('fs_pipe_rest_test_notice', [ | |
| 'type' => $type, | |
| 'message' => $message, | |
| ], 60); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment