From 2edc191b1cc33eb81d6aa6e1274ea4aa2dbb61d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 29 Jun 2020 14:18:51 +0200 Subject: [PATCH] Add support for the List-Unsubscribe header to Email see #3379 --- .../files/lib/system/email/Email.class.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/wcfsetup/install/files/lib/system/email/Email.class.php b/wcfsetup/install/files/lib/system/email/Email.class.php index 71ec1ac093..e1bb8979ea 100644 --- a/wcfsetup/install/files/lib/system/email/Email.class.php +++ b/wcfsetup/install/files/lib/system/email/Email.class.php @@ -71,6 +71,20 @@ class Email { */ protected $listIdHuman = null; + /** + * List-Unsubscribe URI + * @var string + * @since 5.3 + */ + protected $listUnsubscribe = null; + + /** + * Whether the listUnsubscribe URI has One-Click support + * @var boolean + * @since 5.3 + */ + protected $listUnsubscribeOneClick = false; + /** * Date header * @var \DateTime @@ -301,6 +315,37 @@ class Email { return ($this->listIdHuman ? $this->listIdHuman.' ' : '').'<'.$this->listId.'.list-id.'.self::getHost().'>'; } + /** + * Sets the URI for the 'List-Unsubscribe' header. + * + * If $supportsOneClick is set to true the 'List-Unsubscribe-Post' header + * with the value 'List-Unsubscribe=One-Click' is added. + * + * @param string $uri + * @param boolean $supportsOneClick + * @since 5.3 + */ + public function setListUnsubscribe($uri, $supportsOneClick = false) { + if ($uri === null) { + $this->listUnsubscribe = null; + return; + } + + $this->listUnsubscribe = $uri; + $this->listUnsubscribeOneClick = $supportsOneClick; + } + + /** + * Returns the email's full 'List-Id' including the host. Returns 'null' + * if no 'List-Id' is set. + * + * @return ?string + * @since 5.3 + */ + public function getListUnsubscribeUri() { + return $this->listUnsubscribe; + } + /** * Sets the email's 'From'. * @@ -457,6 +502,12 @@ class Email { if ($this->getListID()) { $headers[] = ['list-id', $this->getListID()]; } + if ($this->getListUnsubscribeUri()) { + $headers[] = ['list-unsubscribe', '<'.$this->getListUnsubscribeUri().'>']; + if ($this->listUnsubscribeOneClick) { + $headers[] = ['list-unsubscribe-post', 'List-Unsubscribe=One-Click']; + } + } $headers[] = ['mime-version', '1.0']; if (!$this->body) { @@ -491,6 +542,12 @@ class Email { case 'list-id': $name = 'List-ID'; break; + case 'list-unsubscribe-post': + // This case is identical to the default case below. + // It is special cased, because the grammar of this header is defined + // to be pretty tight. + $name = 'List-Unsubscribe-Post'; + break; case 'mime-version': $name = 'MIME-Version'; break; -- 2.20.1