]*>.*?#is', '', $html); $text = (string) preg_replace('##s', '', (string) $text); // Links: "label (url)" zodat de URL ook in het tekstdeel zichtbaar blijft. $text = (string) preg_replace_callback( '#]*href=(["\'])(.*?)\1[^>]*>(.*?)#is', static function (array $match): string { $url = trim(sp_sendpier_decode_entities($match[2])); $label = trim((string) preg_replace('#\s+#', ' ', sp_sendpier_decode_entities(wp_strip_all_tags($match[3])))); if (stripos($url, 'mailto:') === 0) { $url = substr($url, 7); } if ($url === '') { return $label; } if ($label === '' || strcasecmp($label, $url) === 0) { return $url; } return $label . ' (' . $url . ')'; }, (string) $text ); // Afbeeldingen vallen terug op hun alt-tekst. $text = (string) preg_replace('#]*\balt=(["\'])(.*?)\1[^>]*>#is', '$2', $text); // Blokstructuur naar regeleindes. $text = (string) preg_replace('##i', "\n", $text); $text = (string) preg_replace('#]*>#i', "\n- ", $text); $text = (string) preg_replace('##i', ' ', $text); $text = (string) preg_replace('##i', "\n", $text); $text = wp_strip_all_tags((string) $text); $text = sp_sendpier_decode_entities($text); // Witruimte normaliseren, maar alinea-scheiding behouden. $text = str_replace(["\r\n", "\r"], "\n", $text); $text = (string) preg_replace('#[ \t]+#', ' ', $text); $text = (string) preg_replace('#[ \t]*\n[ \t]*#', "\n", $text); $text = (string) preg_replace('#\n{3,}#', "\n\n", $text); return trim($text); } /** * Decodeer HTML-entiteiten zonder afhankelijkheid van WordPress. */ function sp_sendpier_decode_entities(string $value): string { return html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8'); } /** * Lees content-type en Reply-To uit de headers die `wp_mail()` meegeeft. * * @param mixed $headers String of array, zoals WordPress ze aanlevert. * @return array{content_type: string, reply_to: string} */ function sp_sendpier_parse_wp_headers($headers): array { $result = ['content_type' => 'text/plain', 'reply_to' => '']; if (is_string($headers)) { $headers = preg_split('#\r\n|\r|\n#', $headers) ?: []; } if (!is_array($headers)) { return $result; } foreach ($headers as $header) { if (!is_string($header) || strpos($header, ':') === false) { continue; } [$name, $value] = explode(':', $header, 2); $name = strtolower(trim($name)); $value = trim($value); if ($name === 'content-type' && stripos($value, 'text/html') !== false) { $result['content_type'] = 'text/html'; continue; } if ($name === 'reply-to' && $value !== '') { $result['reply_to'] = sp_sendpier_extract_email_address($value); } } return $result; } /** * Haal het kale e-mailadres uit een header-waarde als "Naam ". */ function sp_sendpier_extract_email_address(string $value): string { if (preg_match('#<([^>]+)>#', $value, $match)) { $value = $match[1]; } $value = trim($value); return filter_var($value, FILTER_VALIDATE_EMAIL) ? $value : ''; } /** * Bouw de payload voor `POST /v1/messages`. * * Verwachte sleutels in $args: * from_email (verplicht), from_name, recipients (lijst van ['email'=>..]), * subject, html, text, content_type, reply_to, list_unsubscribe, mailer, * attachments. * * Regels: * - Bij een HTML-body wordt altijd een text-deel meegestuurd (afgeleid als * er geen expliciete tekstversie is) → multipart/alternative in de API. * - Een text-only bericht blijft text-only; daar is geen HTML voor nodig. * - Bijlagen komen als `attachments[]` mee (base64, zie * includes/attachments.php); een leeg veld sturen we niet mee, zodat een * bericht zonder bijlage er in de API precies zo uitziet als voorheen. */ function sp_sendpier_build_payload(array $args): array { $from_email = trim((string) ($args['from_email'] ?? '')); $from_name = trim((string) ($args['from_name'] ?? '')); $subject = (string) ($args['subject'] ?? ''); $content_type = (string) ($args['content_type'] ?? 'text/html'); $html = (string) ($args['html'] ?? ''); $text = (string) ($args['text'] ?? ''); $recipients = []; foreach ((array) ($args['recipients'] ?? []) as $recipient) { if (is_string($recipient)) { $recipient = ['email' => $recipient]; } if (!is_array($recipient)) { continue; } $email = trim((string) ($recipient['email'] ?? '')); if ($email === '') { continue; } $entry = ['email' => $email]; $name = trim((string) ($recipient['name'] ?? '')); if ($name !== '') { $entry['name'] = $name; } $recipients[] = $entry; } $payload = [ 'from' => ['email' => $from_email], 'to' => $recipients, 'subject' => $subject, ]; if ($from_name !== '') { $payload['from']['name'] = $from_name; } if ($content_type === 'text/html' && trim($html) !== '') { $payload['html'] = $html; $payload['text'] = trim($text) !== '' ? $text : sp_sendpier_html_to_text($html); } elseif (trim($text) !== '') { $payload['text'] = $text; } elseif (trim($html) !== '') { // Content-type zegt text/plain maar er is alleen HTML aangeleverd. $payload['html'] = $html; $payload['text'] = sp_sendpier_html_to_text($html); } $reply_to = trim((string) ($args['reply_to'] ?? '')); if ($reply_to !== '') { $payload['reply_to'] = $reply_to; } $headers = []; $mailer = trim((string) ($args['mailer'] ?? '')); if ($mailer !== '') { $headers['X-Mailer'] = $mailer; } // Alleen meesturen als de aanroeper er een heeft: voor door de gebruiker // uitgelokte transactionele mail (testmail, wachtwoord-reset) is een // afmeldlink niet zinvol. Zie docs/SEND_API_SPEC.md §7 over de // header-whitelist van de Send API. $list_unsubscribe = trim((string) ($args['list_unsubscribe'] ?? '')); if ($list_unsubscribe !== '') { $headers['List-Unsubscribe'] = $list_unsubscribe; $headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click'; } if (!empty($headers)) { $payload['headers'] = $headers; } $attachments = sp_sendpier_payload_attachments($args['attachments'] ?? []); if (!empty($attachments)) { $payload['attachments'] = $attachments; } return $payload; } /** * Houd van de meegegeven bijlagen alleen de velden over die de Send API kent. * * @param mixed $attachments * @return array */ function sp_sendpier_payload_attachments($attachments): array { if (!is_array($attachments)) { return []; } $prepared = []; foreach ($attachments as $attachment) { if (!is_array($attachment)) { continue; } $filename = trim((string) ($attachment['filename'] ?? '')); $content = (string) ($attachment['content'] ?? ''); if ($filename === '' || trim($content) === '') { continue; } $entry = ['filename' => $filename, 'content' => $content]; $content_type = trim((string) ($attachment['content_type'] ?? '')); if ($content_type !== '') { $entry['content_type'] = $content_type; } $content_id = trim((string) ($attachment['content_id'] ?? '')); if ($content_id !== '') { $entry['content_id'] = $content_id; } $prepared[] = $entry; } return $prepared; } /** * Referentiecode voor een testmail; maakt de mail terugvindbaar in * `GET /v1/messages` en in het controlepaneel. */ function sp_sendpier_test_reference(string $seed): string { $reference = strtoupper(substr(preg_replace('#[^a-zA-Z0-9]#', '', md5($seed)) ?: '', 0, 6)); return $reference !== '' ? $reference : 'TEST01'; } /** * Onderwerpregel van de testmail, inclusief referentiecode. */ function sp_sendpier_test_subject(string $reference): string { /* translators: %s is de referentiecode van de testmail. */ return sprintf(__('SendPier testmail (ref %s)', 'sendpier'), $reference); } /** * Bouw onderwerp, HTML- en tekstversie van de testmail. * * Bewuste deliverability-keuzes: * - geen externe afbeeldingen (het oude logo-URL gaf een 404) en geen * verborgen tekst; * - volwaardig tekstalternatief met dezelfde inhoud en dezelfde links; * - alleen werkende, expliciet uitgeschreven links. * * Verwachte sleutels in $context: to, reference, sent_at, site_name, site_url, * domain, from_email. */ function sp_sendpier_build_test_mail(array $context): array { $to = (string) ($context['to'] ?? ''); $reference = (string) ($context['reference'] ?? ''); $sent_at = (string) ($context['sent_at'] ?? ''); $site_name = (string) ($context['site_name'] ?? ''); $site_url = (string) ($context['site_url'] ?? ''); $domain = (string) ($context['domain'] ?? ''); $from_email = (string) ($context['from_email'] ?? ''); $rows = [ __('Verzonden aan', 'sendpier') => $to, __('Afzender', 'sendpier') => $from_email, __('Tijdstip', 'sendpier') => $sent_at, __('WordPress-site', 'sendpier') => $site_name !== '' ? $site_name . ' — ' . $site_url : $site_url, __('Verzenddomein', 'sendpier') => $domain, __('Referentie', 'sendpier') => $reference, ]; $rows_html = ''; $rows_text = ''; foreach ($rows as $label => $value) { if (trim((string) $value) === '') { continue; } $rows_html .= '' . '' . sp_sendpier_escape_html($label) . '' . '' . sp_sendpier_escape_html((string) $value) . '' . ''; $rows_text .= $label . ': ' . $value . "\n"; } $html = '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . sp_sendpier_escape_html(__('SendPier testmail', 'sendpier')) . '' . "\n" . '' . "\n" . '' . "\n" . '' . '
' . '

SendPier

' . '

' . sp_sendpier_escape_html(__('Je testmail is aangekomen', 'sendpier')) . '

' . '

' . sp_sendpier_escape_html(__('Deze e-mail is verstuurd vanuit de SendPier-plugin voor WordPress. Zie je dit bericht, dan werkt de verbinding tussen je WordPress-site en SendPier.', 'sendpier')) . '

' . '' . $rows_html . '
' . '

' . sprintf( /* translators: %s is de link naar het controlepaneel. */ sp_sendpier_escape_html(__('De afleverstatus van deze mail staat in het controlepaneel op %s, en in WordPress onder SendPier › Diagnose.', 'sendpier')), 'https://cp.sendpier.com' ) . '

' . '

' . sprintf( /* translators: 1: het e-mailadres van support, 2: de link naar de website. */ sp_sendpier_escape_html(__('Vragen? Mail %1$s of kijk op %2$s.', 'sendpier')), 'support@sendpier.com', 'https://sendpier.com' ) . '

' . '
' . "\n" . '' . "\n" . ''; $text = __('SendPier testmail', 'sendpier') . "\n" . "=================\n\n" . __('Deze e-mail is verstuurd vanuit de SendPier-plugin voor WordPress. Zie je dit bericht, dan werkt de verbinding tussen je WordPress-site en SendPier.', 'sendpier') . "\n\n" . $rows_text . "\n" . sprintf( /* translators: %s is de link naar het controlepaneel. */ __('De afleverstatus van deze mail staat in het controlepaneel op %s, en in WordPress onder SendPier › Diagnose.', 'sendpier'), 'https://cp.sendpier.com' ) . "\n\n" . sprintf( /* translators: 1: het e-mailadres van support, 2: de link naar de website. */ __('Vragen? Mail %1$s of kijk op %2$s.', 'sendpier'), 'support@sendpier.com', 'https://sendpier.com' ) . "\n"; return [ 'subject' => sp_sendpier_test_subject($reference), 'html' => $html, 'text' => $text, ]; } /** * HTML-escape zonder WordPress-afhankelijkheid (esc_html is hier niet * beschikbaar in de losse testrun). */ function sp_sendpier_escape_html(string $value): string { return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }