UserModel::sendEmailConfirmationEmail PHP Method

sendEmailConfirmationEmail() public method

Send the confirmation email.
public sendEmailConfirmationEmail ( integer | string | null $User = null, boolean $Force = false )
$User integer | string | null
$Force boolean
    public function sendEmailConfirmationEmail($User = null, $Force = false)
    {
        if (!$User) {
            $User = Gdn::session()->User;
        } elseif (is_numeric($User)) {
            $User = $this->getID($User);
        } elseif (is_string($User)) {
            $User = $this->getByEmail($User);
        }
        if (!$User) {
            throw notFoundException('User');
        }
        $User = (array) $User;
        if (is_string($User['Attributes'])) {
            $User['Attributes'] = dbdecode($User['Attributes']);
        }
        // Make sure the user needs email confirmation.
        if ($User['Confirmed'] && !$Force) {
            $this->Validation->addValidationResult('Role', 'Your email doesn\'t need confirmation.');
            // Remove the email key.
            if (isset($User['Attributes']['EmailKey'])) {
                unset($User['Attributes']['EmailKey']);
                $this->saveAttribute($User['UserID'], $User['Attributes']);
            }
            return;
        }
        // Make sure there is a confirmation code.
        $Code = valr('Attributes.EmailKey', $User);
        if (!$Code) {
            $Code = randomString(8);
            $Attributes = $User['Attributes'];
            if (!is_array($Attributes)) {
                $Attributes = ['EmailKey' => $Code];
            } else {
                $Attributes['EmailKey'] = $Code;
            }
            $this->saveAttribute($User['UserID'], $Attributes);
        }
        $AppTitle = Gdn::config('Garden.Title');
        $Email = new Gdn_Email();
        $Email->subject(sprintf(t('[%s] Confirm Your Email Address'), $AppTitle));
        $Email->to($User['Email']);
        $EmailUrlFormat = '{/entry/emailconfirm,exurl,domain}/{User.UserID,rawurlencode}/{EmailKey,rawurlencode}';
        $Data = [];
        $Data['EmailKey'] = $Code;
        $Data['User'] = arrayTranslate((array) $User, ['UserID', 'Name', 'Email']);
        $url = formatString($EmailUrlFormat, $Data);
        $message = formatString(t('Hello {User.Name}!'), $Data) . ' ' . t('You need to confirm your email address before you can continue.');
        $emailTemplate = $Email->getEmailTemplate()->setTitle(t('Confirm Your Email Address'))->setMessage($message)->setButton($url, t('Confirm My Email Address'));
        $Email->setEmailTemplate($emailTemplate);
        try {
            $Email->send();
        } catch (Exception $e) {
            if (debug()) {
                throw $e;
            }
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Send email confirmation message to user.
  *
  * @access public
  * @since 2.0.?
  *
  * @param int $UserID
  */
 public function emailConfirmRequest($UserID = '')
 {
     if ($UserID && !Gdn::session()->checkPermission('Garden.Users.Edit')) {
         $UserID = '';
     }
     try {
         $this->UserModel->sendEmailConfirmationEmail($UserID);
     } catch (Exception $Ex) {
     }
     $this->Form->setValidationResults($this->UserModel->validationResults());
     $this->render();
 }
UserModel