Add support for the List-Unsubscribe header to Email
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 29 Jun 2020 12:18:51 +0000 (14:18 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 29 Jun 2020 14:13:02 +0000 (16:13 +0200)
see #3379

wcfsetup/install/files/lib/system/email/Email.class.php

index 71ec1ac093b60f6511fc8c9b90d2e5c88d57825b..e1bb8979ea1141a19aecab5e1881f0d145511e69 100644 (file)
@@ -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;