UserModel::sendWelcomeEmail PHP Method

sendWelcomeEmail() public method

Send welcome email to user.
public sendWelcomeEmail ( integer $UserID, string $Password, string $RegisterType = 'Add', array | null $AdditionalData = null )
$UserID integer
$Password string
$RegisterType string
$AdditionalData array | null
    public function sendWelcomeEmail($UserID, $Password, $RegisterType = 'Add', $AdditionalData = null)
    {
        $Session = Gdn::session();
        $Sender = $this->getID($Session->UserID);
        $User = $this->getID($UserID);
        if (!ValidateEmail($User->Email)) {
            return;
        }
        $AppTitle = Gdn::config('Garden.Title');
        $Email = new Gdn_Email();
        $Email->subject(sprintf(t('[%s] Welcome Aboard!'), $AppTitle));
        $Email->to($User->Email);
        $emailTemplate = $Email->getEmailTemplate();
        $Data = [];
        $Data['User'] = arrayTranslate((array) $User, ['UserID', 'Name', 'Email']);
        $Data['Sender'] = arrayTranslate((array) $Sender, ['Name', 'Email']);
        $Data['Title'] = $AppTitle;
        if (is_array($AdditionalData)) {
            $Data = array_merge($Data, $AdditionalData);
        }
        $Data['EmailKey'] = valr('Attributes.EmailKey', $User);
        $message = '<p>' . formatString(t('Hello {User.Name}!'), $Data) . ' ';
        $message .= $this->getEmailWelcome($RegisterType, $User, $Data, $Password);
        // Add the email confirmation key.
        if ($Data['EmailKey']) {
            $emailUrlFormat = '{/entry/emailconfirm,exurl,domain}/{User.UserID,rawurlencode}/{EmailKey,rawurlencode}';
            $url = formatString($emailUrlFormat, $Data);
            $message .= '<p>' . t('You need to confirm your email address before you can continue.') . '</p>';
            $emailTemplate->setButton($url, t('Confirm My Email Address'));
        } else {
            $emailTemplate->setButton(externalUrl('/'), t('Access the Site'));
        }
        $emailTemplate->setMessage($message);
        $emailTemplate->setTitle(t('Welcome Aboard!'));
        $Email->setEmailTemplate($emailTemplate);
        try {
            $Email->send();
        } catch (Exception $e) {
            if (debug()) {
                throw $e;
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Captcha-authenticated registration. Used by default.
  *
  * Allows immediate access upon successful registration, and optionally requires
  * email address confirmation.
  *
  * Events: RegistrationSuccessful
  *
  * @access private
  * @since 2.0.0
  */
 private function registerBasic()
 {
     $this->View = 'registerbasic';
     Gdn::userModel()->addPasswordStrength($this);
     if ($this->Form->isPostBack() === true) {
         // Add validation rules that are not enforced by the model
         $this->UserModel->defineSchema();
         $this->UserModel->Validation->applyRule('Name', 'Username', $this->UsernameError);
         $this->UserModel->Validation->applyRule('TermsOfService', 'Required', t('You must agree to the terms of service.'));
         $this->UserModel->Validation->applyRule('Password', 'Required');
         $this->UserModel->Validation->applyRule('Password', 'Strength');
         $this->UserModel->Validation->applyRule('Password', 'Match');
         // $this->UserModel->Validation->applyRule('DateOfBirth', 'MinimumAge');
         $this->fireEvent('RegisterValidation');
         try {
             $Values = $this->Form->formValues();
             $Values = $this->UserModel->filterForm($Values, true);
             unset($Values['Roles']);
             $AuthUserID = $this->UserModel->register($Values);
             $this->setData('UserID', $AuthUserID);
             if ($AuthUserID == UserModel::REDIRECT_APPROVE) {
                 $this->Form->setFormValue('Target', '/entry/registerthanks');
                 $this->_setRedirect();
                 return;
             } elseif (!$AuthUserID) {
                 $this->Form->setValidationResults($this->UserModel->validationResults());
             } else {
                 // The user has been created successfully, so sign in now.
                 Gdn::session()->start($AuthUserID);
                 if ($this->Form->getFormValue('RememberMe')) {
                     Gdn::authenticator()->setIdentity($AuthUserID, true);
                 }
                 try {
                     $this->UserModel->sendWelcomeEmail($AuthUserID, '', 'Register');
                 } catch (Exception $Ex) {
                 }
                 $this->fireEvent('RegistrationSuccessful');
                 // ... and redirect them appropriately
                 $Route = $this->redirectTo();
                 if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
                     $this->RedirectUrl = url($Route);
                 } else {
                     if ($Route !== false) {
                         redirect($Route);
                     }
                 }
             }
         } catch (Exception $Ex) {
             $this->Form->addError($Ex);
         }
     }
     $this->render();
 }
UserModel