false, 'provider' => 'sendgrid', 'status' => null, 'error' => 'Missing SENDGRID_API_KEY', 'from_email' => null, ]; } $from_email = adaire_blocks_get_sendgrid_from_email(); if (!$from_email) { return [ 'sent' => false, 'provider' => 'sendgrid', 'status' => null, 'error' => 'Missing feedback sender email in configuration', 'from_email' => null, ]; } $from_email = apply_filters('adaire_blocks_deactivation_feedback_from', $from_email, []); $from_name = apply_filters('adaire_blocks_deactivation_feedback_from_name', 'Adaire Blocks', []); $payload = [ 'personalizations' => [ [ 'to' => is_array($to) ? array_map(fn($t) => ['email' => $t], $to) : [['email' => $to]], 'subject' => $subject, ], ], 'from' => [ 'email' => $from_email, 'name' => $from_name, ], 'content' => [ [ 'type' => 'text/plain', 'value' => $message, ], ], ]; if ($reply_to && is_email($reply_to)) { $payload['reply_to'] = ['email' => $reply_to]; } $response = wp_remote_post('https://api.sendgrid.com/v3/mail/send', [ 'timeout' => 15, 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ], 'body' => wp_json_encode($payload), ]); if (is_wp_error($response)) { return [ 'sent' => false, 'provider' => 'sendgrid', 'status' => null, 'error' => $response->get_error_message(), 'from_email' => $from_email, 'message_id' => null, ]; } $status = (int) wp_remote_retrieve_response_code($response); $body = (string) wp_remote_retrieve_body($response); if ($status >= 200 && $status < 300) { return [ 'sent' => true, 'provider' => 'sendgrid', 'status' => $status, 'error' => null, 'from_email' => $from_email, ]; } return [ 'sent' => false, 'provider' => 'sendgrid', 'status' => $status, 'error' => substr($body, 0, 800) ?: ('HTTP ' . $status), 'from_email' => $from_email, ]; }