public function testDeliver() { if (self::TESTDELIVERY) { Yii::app()->settings->emailType = $this->method; $this->eml = new InlineEmail(); try { $this->eml->credId = $this->credentials('liveDeliveryTest')->id; } catch (Exception $e) { $this->markTestSkipped('You have not defined the liveDeliveryTest alias in protected/tests/fixtures/x2_credentials-local.php !'); } $this->eml->userProfile = Profile::model()->findByAttributes(array('username' => 'testuser')); $this->eml->mailingList = $this->recipient; $this->eml->subject = 'Test email'; $this->eml->message = '<html><head></head><body>Test email body</body></html>'; $this->eml->attachments = array(); $status = $this->eml->deliver(); $this->assertTrue(in_array('200', $status), 'Failed asserting successful return code. Status = ' . CJSON::encode($status)); X2_TEST_DEBUG_LEVEL > 1 && println("Check email at address " . TEST_EMAIL_TO . ' for the delivered test message.'); // No further assertions in this method. Chiggity check yo inbox. } }
/** * Send an email from X2Engine, returns an array with status code/message * * @param array addresses * @param string $subject the subject for the email * @param string $message the body of the email * @param array $attachments array of attachments to send * @param array|integer $from from and reply to address for the email array(name, address) * or, if integer, the ID of a email credentials record to use for delivery. * @return array */ public function sendUserEmail($addresses, $subject, $message, $attachments = null, $from = null) { $eml = new InlineEmail(); if (is_array($addresses) ? count($addresses) == 0 : true) { throw new Exception('Invalid argument 1 sent to x2base.sendUserEmail(); expected a non-empty array, got instead: ' . var_export($addresses, 1)); } // Set recipients: if (array_key_exists('to', $addresses) || array_key_exists('cc', $addresses) || array_key_exists('bcc', $addresses)) { $eml->mailingList = $addresses; } else { return array('code' => 500, 'message' => 'No recipients specified for email; array given for argument 1 of x2base.sendUserEmail does not have a "to", "cc" or "bcc" key.'); } // Resolve sender (use stored email credentials or system default): if ($from === null || in_array($from, Credentials::$sysUseId)) { $from = (int) Credentials::model()->getDefaultUserAccount($from); // Set to the user's name/email if no valid defaults found: if ($from == Credentials::LEGACY_ID) { $from = array('name' => Yii::app()->params->profile->fullName, 'address' => Yii::app()->params->profile->emailAddress); } } if (is_numeric($from)) { $eml->credId = $from; } else { $eml->from = $from; } // Set other attributes $eml->subject = $subject; $eml->message = $message; $eml->attachments = $attachments; return $eml->deliver(); }
public function actionInviteUsers() { if (isset($_POST['emails'])) { $list = $_POST['emails']; $body = "Hello,\n\nYou are receiving this email because your X2Engine administrator has invited you to create an account.\nPlease click on the link below to create an account at X2Engine!\n\n"; $subject = "Create Your X2Engine User Account"; $list = trim($list); $emails = explode(',', $list); foreach ($emails as &$email) { $key = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 16)), 0, 16); $user = new User('invite'); $email = trim($email); $user->inviteKey = $key; $user->temporary = 1; $user->emailAddress = $email; $user->status = 0; $userList = User::model()->findAllByAttributes(array('emailAddress' => $email, 'temporary' => 1)); foreach ($userList as $userRecord) { if (isset($userRecord)) { $userRecord->delete(); } } $user->save(); $link = CHtml::link('Create Account', (@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $this->createUrl('/users/users/createAccount', array('key' => $key))); $mail = new InlineEmail(); $mail->to = $email; // Get email password $cred = Credentials::model()->getDefaultUserAccount(Credentials::$sysUseId['systemResponseEmail'], 'email'); if ($cred == Credentials::LEGACY_ID) { $cred = Credentials::model()->getDefaultUserAccount(Yii::app()->user->id, 'email'); } if ($cred != Credentials::LEGACY_ID) { $mail->credId = $cred; } $mail->subject = $subject; $mail->message = $body . "<br><br>" . $link; $mail->contactFlag = false; if ($mail->prepareBody()) { $mail->deliver(); } else { } } $this->redirect('admin'); } $this->render('inviteUsers'); }