Skip to content
← Back to Snippets
Code

Transactional Email Envelope Builder

Builds a normalized transactional email envelope with recipient validation, subject trimming, and headers.

Purpose

Builds a normalized transactional email envelope with recipient validation, subject trimming, and headers.

Snippet details

ContextEmailLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Transactional Email Envelope Builder.
 *
 * Purpose:
 * Builds a normalized transactional email envelope with recipient validation, subject trimming, and headers.
 *
 * @param string $recipient_email Recipient email address.
 * @param string $subject Email subject.
 * @param string $body_text Plain-text body.
 * @return array Transactional email envelope.
 */
function ogSnippetTransactionalEmailEnvelopeBuilder(string $recipient_email, string $subject, string $body_text): array {
	$valid = filter_var($recipient_email, FILTER_VALIDATE_EMAIL) !== false;
	$clean_subject = trim(preg_replace('/[
]+/', ' ', $subject));
	$headers = array('Content-Type: text/plain; charset=UTF-8', 'X-Auto-Response-Suppress: All');
	return array('valid_recipient' => $valid, 'to' => strtolower(trim($recipient_email)), 'subject' => $clean_subject, 'body' => $body_text, 'headers' => $headers);
}

$email_envelope = ogSnippetTransactionalEmailEnvelopeBuilder('captain@enterprise.example', 'Bridge Alert', 'Transporter cycle completed.');
echo $email_envelope['subject'];