InvitationModel::send PHP Method

send() public method

public send ( $InvitationID )
$InvitationID
    public function send($InvitationID)
    {
        $Invitation = $this->GetByInvitationID($InvitationID);
        $Session = Gdn::session();
        if ($Invitation === false) {
            throw new Exception(t('ErrorRecordNotFound'));
        } elseif ($Session->UserID != $Invitation->SenderUserID) {
            throw new Exception(t('InviteErrorPermission', t('ErrorPermission')));
        } else {
            // Some information for the email
            $RegistrationUrl = ExternalUrl("entry/registerinvitation/{$Invitation->Code}");
            $AppTitle = Gdn::config('Garden.Title');
            $Email = new Gdn_Email();
            $Email->subject(sprintf(t('[%s] Invitation'), $AppTitle));
            $Email->to($Invitation->Email);
            $emailTemplate = $Email->getEmailTemplate();
            $message = t('Hello!') . ' ' . sprintf(t('%s has invited you to join %s.'), $Invitation->SenderName, $AppTitle);
            $emailTemplate->setButton($RegistrationUrl, t('Join this Community Now'))->setMessage($message)->setTitle(sprintf(t('Join %s'), $AppTitle));
            $Email->setEmailTemplate($emailTemplate);
            try {
                $Email->send();
            } catch (Exception $e) {
                if (debug()) {
                    throw $e;
                }
            }
        }
    }

Usage Example

 /**
  * Let user send an invitation.
  *
  * @since 2.0.0
  * @access public
  * @param int $InvitationID Unique identifier.
  */
 public function sendInvite($InvitationID = '')
 {
     if (!$this->Form->authenticatedPostBack()) {
         throw forbiddenException('GET');
     }
     $this->permission('Garden.SignIn.Allow');
     $InvitationModel = new InvitationModel();
     $Session = Gdn::session();
     try {
         $Email = new Gdn_Email();
         $InvitationModel->send($InvitationID, $Email);
     } catch (Exception $ex) {
         $this->Form->addError(strip_tags($ex->getMessage()));
     }
     if ($this->Form->errorCount() == 0) {
         $this->informMessage(t('The invitation was sent successfully.'));
     }
     $this->View = 'Invitations';
     $this->invitations();
 }