Gdn_Email::send PHP Method

send() public method

public send ( string $EventName = '' ) : boolean
$EventName string
return boolean
    public function send($EventName = '')
    {
        $this->formatMessage($this->emailTemplate->toString());
        $this->fireEvent('BeforeSendMail');
        if (c('Garden.Email.Disabled')) {
            throw new Exception('Email disabled', self::ERR_SKIPPED);
        }
        if (c('Garden.Email.UseSmtp')) {
            $this->PhpMailer->isSMTP();
            $SmtpHost = c('Garden.Email.SmtpHost', '');
            $SmtpPort = c('Garden.Email.SmtpPort', 25);
            if (strpos($SmtpHost, ':') !== false) {
                list($SmtpHost, $SmtpPort) = explode(':', $SmtpHost);
            }
            $this->PhpMailer->Host = $SmtpHost;
            $this->PhpMailer->Port = $SmtpPort;
            $this->PhpMailer->SMTPSecure = c('Garden.Email.SmtpSecurity', '');
            $this->PhpMailer->Username = $Username = c('Garden.Email.SmtpUser', '');
            $this->PhpMailer->Password = $Password = c('Garden.Email.SmtpPassword', '');
            if (!empty($Username)) {
                $this->PhpMailer->SMTPAuth = true;
            }
        } else {
            $this->PhpMailer->isMail();
        }
        if ($EventName != '') {
            $this->EventArguments['EventName'] = $EventName;
            $this->fireEvent('SendMail');
        }
        if (!empty($this->Skipped) && count($this->PhpMailer->getAllRecipientAddresses()) == 0) {
            // We've skipped all recipients.
            throw new Exception('No valid email recipients.', self::ERR_SKIPPED);
        }
        $this->PhpMailer->setThrowExceptions(true);
        if (!$this->PhpMailer->send()) {
            throw new Exception($this->PhpMailer->ErrorInfo);
        }
        return true;
    }

Usage Example

コード例 #1
0
ファイル: class.usermodel.php プロジェクト: RodSloan/vanilla
 /**
  * Send forgot password email.
  *
  * @param $Email
  * @return bool
  * @throws Exception
  */
 public function passwordRequest($Email)
 {
     if (!$Email) {
         return false;
     }
     $Users = $this->getWhere(array('Email' => $Email))->resultObject();
     if (count($Users) == 0) {
         // Check for the username.
         $Users = $this->getWhere(array('Name' => $Email))->resultObject();
     }
     $this->EventArguments['Users'] =& $Users;
     $this->EventArguments['Email'] = $Email;
     $this->fireEvent('BeforePasswordRequest');
     if (count($Users) == 0) {
         $this->Validation->addValidationResult('Name', "Couldn't find an account associated with that email/username.");
         return false;
     }
     $NoEmail = true;
     foreach ($Users as $User) {
         if (!$User->Email) {
             continue;
         }
         $Email = new Gdn_Email();
         // Instantiate in loop to clear previous settings
         $PasswordResetKey = BetterRandomString(20, 'Aa0');
         $PasswordResetExpires = strtotime('+1 hour');
         $this->saveAttribute($User->UserID, 'PasswordResetKey', $PasswordResetKey);
         $this->saveAttribute($User->UserID, 'PasswordResetExpires', $PasswordResetExpires);
         $AppTitle = c('Garden.Title');
         $Email->subject(sprintf(t('[%s] Password Reset Request'), $AppTitle));
         $Email->to($User->Email);
         $Email->message(sprintf(t('PasswordRequest'), $User->Name, $AppTitle, ExternalUrl('/entry/passwordreset/' . $User->UserID . '/' . $PasswordResetKey)));
         $Email->send();
         $NoEmail = false;
     }
     if ($NoEmail) {
         $this->Validation->addValidationResult('Name', 'There is no email address associated with that account.');
         return false;
     }
     return true;
 }
All Usage Examples Of Gdn_Email::send