Pimcore\Tool\Newsletter::subscribe PHP Method

subscribe() public method

public subscribe ( $params ) : mixed
$params
return mixed
    public function subscribe($params)
    {
        $onlyCreateVersion = false;
        $className = $this->getClassName();
        $object = new $className();
        // check for existing e-mail
        $existingObject = $className::getByEmail($params["email"], 1);
        if ($existingObject) {
            // if there's an existing user with this email address, do not overwrite the contents, but create a new
            // version which will be published as soon as the contact gets verified (token/email)
            $object = $existingObject;
            $onlyCreateVersion = true;
            //throw new \Exception("email address '" . $params["email"] . "' already exists");
        }
        if (!array_key_exists("email", $params)) {
            throw new \Exception("key 'email' is a mandatory parameter");
        }
        $object->setValues($params);
        if (!$object->getParentId()) {
            $object->setParentId(1);
        }
        $object->setNewsletterActive(true);
        $object->setCreationDate(time());
        $object->setModificationDate(time());
        $object->setUserModification(0);
        $object->setUserOwner(0);
        $object->setPublished(true);
        $object->setKey(\Pimcore\File::getValidFilename($object->getEmail() . "~" . substr(uniqid(), -3)));
        if (!$onlyCreateVersion) {
            $object->save();
        }
        // generate token
        $token = base64_encode(\Zend_Json::encode(["salt" => md5(microtime()), "email" => $object->getEmail(), "id" => $object->getId()]));
        $token = str_replace("=", "~", $token);
        // base64 can contain = which isn't safe in URL's
        $object->setProperty("token", "text", $token);
        if (!$onlyCreateVersion) {
            $object->save();
        } else {
            $object->saveVersion(true, true);
        }
        $this->addNoteOnObject($object, "subscribe");
        return $object;
    }

Usage Example

 public function subscribeAction()
 {
     $this->enableLayout();
     $newsletter = new Newsletter("person");
     // replace "crm" with the class name you have used for your class above (mailing list)
     $params = $this->getAllParams();
     $this->view->success = false;
     if ($newsletter->checkParams($params)) {
         try {
             $params["parentId"] = 1;
             // default folder (home) where we want to save our subscribers
             $newsletterFolder = Model\Object::getByPath("/crm/newsletter");
             if ($newsletterFolder) {
                 $params["parentId"] = $newsletterFolder->getId();
             }
             $user = $newsletter->subscribe($params);
             // user and email document
             // parameters available in the email: gender, firstname, lastname, email, token, object
             // ==> see mailing framework
             $newsletter->sendConfirmationMail($user, Model\Document::getByPath("/en/advanced-examples/newsletter/confirmation-email"), ["additional" => "parameters"]);
             // do some other stuff with the new user
             $user->setDateRegister(new \DateTime());
             $user->save();
             $this->view->success = true;
         } catch (\Exception $e) {
             echo $e->getMessage();
         }
     }
 }