Skip to content
← Back to Snippets
Code

Unsubscribe Link Token Plan

Builds unsubscribe token metadata with route, expiry, scope, and one-click email compliance fields.

Purpose

Builds unsubscribe token metadata with route, expiry, scope, and one-click email compliance fields.

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.
 */

/**
 * Unsubscribe Link Token Plan.
 *
 * Purpose:
 * Builds unsubscribe token metadata with route, expiry, scope, and one-click email compliance fields.
 *
 * @param int $subscriber_id Subscriber identifier.
 * @param string $scope Unsubscribe scope.
 * @param string $base_url Public unsubscribe route.
 * @return array Unsubscribe link token plan.
 */
function ogSnippetUnsubscribeLinkTokenPlan(int $subscriber_id, string $scope, string $base_url): array {
	$token = bin2hex(random_bytes(20));
	$route = rtrim($base_url, '/').'/'.$token;
	return array('subscriber_id' => $subscriber_id, 'scope' => $scope, 'token_hash' => hash('sha256', $token), 'url' => $route, 'expires_at' => time() + 2592000, 'one_click' => true);
}

$unsubscribe = ogSnippetUnsubscribeLinkTokenPlan(1701, 'mission-briefings', '//example.com/unsubscribe');
echo $unsubscribe['scope'];