PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.7.9
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.7.9
8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 All 535 releases
chatbot / addons / automator / includes / actions / mailboxlayer-actions.php

mailboxlayer-actions.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.7.9, at addons/automator/includes/actions/mailboxlayer-actions.php

169 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MailboxLayer Actions
4 *
5 * @package WPbot_Automator
6 */
7
8 namespace WPbot_Automator\Actions;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class MailboxLayer_Actions
16 */
17 class MailboxLayer_Actions extends Action {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'mailboxlayer';
24 $this->group = __( 'MailboxLayer', 'wpbot-automator' );
25 }
26
27 /**
28 * Execute the action
29 *
30 * @param array $action_data Configuration for this action from the workflow.
31 * @param array $trigger_data Data passed from the trigger.
32 * @return bool|array
33 */
34 public function execute( $action_data, $trigger_data ) {
35 $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
36
37 if ( empty( $action_id ) ) {
38 return false;
39 }
40
41 switch ( $action_id ) {
42 case 'validate_email':
43 return $this->validate_email( $action_data, $trigger_data );
44 case 'test_connection':
45 return $this->test_connection( $action_data, $trigger_data );
46 default:
47 return false;
48 }
49 }
50
51 /**
52 * Validate Email Address via MailboxLayer
53 */
54 private function validate_email( $action_data, $trigger_data ) {
55 $cfg = isset( $action_data['config'] ) ? $action_data['config'] : array();
56 $token = trim( $this->parse_tokens( $cfg['api_key'] ?? '', $trigger_data ) );
57 $email = trim( $this->parse_tokens( $cfg['email'] ?? '', $trigger_data ) );
58
59 // SMART FALLBACK: If token replacement didn't happen (still contains {{...}}) or is empty,
60 // try to find an email field in the trigger data.
61 if ( empty( $email ) || strpos( $email, '{{' ) !== false ) {
62 $found_email = $this->find_email_in_data( $trigger_data );
63 if ( $found_email ) {
64 //error_log( sprintf( 'MailboxLayer Action - Smart Fallback: Found email "%s" in trigger data.', $found_email ) );
65 $email = $found_email;
66 }
67 }
68
69 if ( empty( $token ) || empty( $email ) || strpos( $email, '{{' ) !== false ) {
70 //error_log( sprintf( 'MailboxLayer Action - FAILED: API Key or Email is empty/unresolved. Key length: %d, Email: "%s"', strlen( $token ), $email ) );
71 return array(
72 'success' => false,
73 'message' => 'API Key and Email are required. Could not resolve email from tokens.',
74 );
75 }
76
77 // Use HTTP instead of HTTPS as some free plans only support HTTP and it's what the user confirmed works.
78 $endpoint = sprintf( 'http://apilayer.net/api/check?access_key=%s&email=%s&smtp=1&format=1', urlencode( $token ), urlencode( $email ) );
79 //error_log( 'MailboxLayer Action - Requesting: ' . $endpoint );
80 if ( PHP_SAPI === 'cli' ) { echo "MailboxLayer Action - Requesting: $endpoint\n"; }
81
82 $response = wp_remote_get( $endpoint, array(
83 'timeout' => 20,
84 ) );
85
86 if ( is_wp_error( $response ) ) {
87 return array(
88 'success' => false,
89 'message' => $response->get_error_message(),
90 );
91 }
92
93 $body = json_decode( wp_remote_retrieve_body( $response ), true );
94 $code = wp_remote_retrieve_response_code( $response );
95 //error_log( sprintf( 'MailboxLayer Action - HTTP Code: %d, Response Body: %s', $code, wp_json_encode( $body ) ) );
96 if ( PHP_SAPI === 'cli' ) { echo sprintf( "MailboxLayer Action - HTTP Code: %d, Response Body: %s\n", $code, wp_json_encode( $body ) ); }
97
98 if ( $code >= 200 && $code < 300 && isset( $body['format_valid'] ) ) {
99 $is_valid = ! empty( $body['format_valid'] ) && ! empty( $body['smtp_check'] );
100 $valid_text = $is_valid ? 'Valid Email' : 'Invalid Email';
101 $log_msg = sprintf( 'Email validation completed: %s (%s).', $email, $valid_text );
102
103 return array(
104 'success' => $is_valid,
105 'message' => $log_msg,
106 'mailboxlayer_format_valid' => $body['format_valid'],
107 'mailboxlayer_mx_found' => isset( $body['mx_found'] ) ? $body['mx_found'] : false,
108 'mailboxlayer_smtp_check' => isset( $body['smtp_check'] ) ? $body['smtp_check'] : false,
109 'mailboxlayer_score' => isset( $body['score'] ) ? $body['score'] : 0,
110 'mailboxlayer_raw' => wp_json_encode( $body )
111 );
112 }
113
114 $error_msg = isset( $body['error']['info'] ) ? $body['error']['info'] : 'Unknown error from MailboxLayer API';
115 return array(
116 'success' => false,
117 'message' => 'MailboxLayer API Error: ' . $error_msg,
118 );
119 }
120
121 /**
122 * Test Connection helper to verify API Key
123 */
124 private function test_connection( $action_data, $trigger_data ) {
125 // A test connection typically just attempts to validate a known valid email.
126 // Testing "support@apilayer.com" gives a reliable answer.
127 $cfg = isset( $action_data['config'] ) ? $action_data['config'] : array();
128 $cfg['email'] = 'support@apilayer.com';
129 $action_data['config'] = $cfg;
130
131 $result = $this->validate_email( $action_data, $trigger_data );
132
133 if ( $result['success'] ) {
134 return array(
135 'success' => true,
136 'message' => 'Connection successful! API key is valid.',
137 );
138 }
139
140 return $result;
141 }
142
143 /**
144 * Recursively search for a valid email in trigger data.
145 *
146 * @param array $data Data to search.
147 * @return string|false
148 */
149 private function find_email_in_data( $data ) {
150 if ( ! is_array( $data ) ) {
151 return false;
152 }
153
154 foreach ( $data as $key => $value ) {
155 if ( is_string( $value ) && is_email( $value ) ) {
156 return $value;
157 }
158 if ( is_array( $value ) ) {
159 $found = $this->find_email_in_data( $value );
160 if ( $found ) {
161 return $found;
162 }
163 }
164 }
165
166 return false;
167 }
168 }
169