Beispiel #1
0
 /**
  * @inheritdoc
  */
 public function __construct($config = [])
 {
     parent::__construct($config);
     if (!\Yii::$app->has(Component::COMPONENT)) {
         throw new LetterException(\Yii::t('app', 'You need to configure the component `{component}`.', ['component' => Component::COMPONENT]));
     }
     $this->Postman = \rmrevin\yii\postman\Component::get();
     $this->setFrom($this->Postman->default_from);
     $this->code = \Yii::$app->getSecurity()->generateRandomString();
 }
 public function testErrorCronSending()
 {
     $Postman = \rmrevin\yii\postman\Component::get();
     $Mailer = $Postman->getMailerObject();
     $Mailer->IsSMTP();
     $Letter = (new postman\RawLetter())->setSubject('Test error cron sending')->setBody('body')->setFrom(['*****@*****.**', 'Test'])->addAddress(\Yii::$app->params['demo_email'], ['*****@*****.**'], ['*****@*****.**'], ['*****@*****.**']);
     $Letter->send();
     $Letter->send();
     $Letter->send();
     $Letter->send();
     $Letter->send();
     $count = postman\models\LetterModel::find()->where(['date_send' => null])->count();
     $this->assertEquals(5, $count);
     $this->expectException('yii\\base\\Exception');
     postman\models\LetterModel::cron(1);
 }
 public function testErrorCronSending()
 {
     $Postman = \rmrevin\yii\postman\Component::get();
     $Mailer = $Postman->getMailerObject();
     $Mailer->IsSMTP();
     $Mailer->Host = 'smtp.google.com';
     $Mailer->Username = '******';
     $Letter = (new postman\RawLetter())->setSubject('Test cron sending')->setBody('body')->setFrom(['*****@*****.**', 'Test'])->addAddress(\Yii::$app->params['demo_email'], ['*****@*****.**'], ['*****@*****.**'], ['*****@*****.**']);
     $Letter->send();
     $Letter->send();
     $Letter->send();
     $Letter->send();
     $Letter->send();
     $count = postman\models\LetterModel::find()->where(['date_send' => null])->count();
     $this->assertEquals(5, $count);
     postman\models\LetterModel::cron(1);
     $this->expectOutputString('The following From address failed: test@domain.com : Called Mail() without being connected' . "\n");
 }
 public function safeDown()
 {
     $this->dropColumn(\rmrevin\yii\postman\Component::get()->table, 'code');
 }
Beispiel #5
0
<?php

/**
 * main.php
 * @author Roman Revin http://phptime.ru
 */
return ['id' => 'testapp', 'basePath' => realpath(__DIR__ . '/..'), 'components' => ['db' => ['class' => 'yii\\db\\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2postman', 'username' => 'root', 'password' => '', 'tablePrefix' => 'yii_'], 'postman' => ['class' => \rmrevin\yii\postman\Component::className(), 'driver' => 'sendmail', 'default_from' => ['no-reply@localhost', 'Mailer'], 'table' => '{{%postman_letter}}', 'view_path' => '/email', 'smtp_config' => ['host' => 'smtp.domain.com', 'port' => 465, 'auth' => true, 'user' => '*****@*****.**', 'password' => 'password', 'secure' => 'ssl', 'debug' => false]]], 'params' => []];
 public function down()
 {
     $this->dropTable(\rmrevin\yii\postman\Component::get()->table);
 }
Beispiel #7
0
 /**
  * the method sends a letter
  * @param bool $immediately
  *
  * @return bool
  */
 public function send($immediately = false)
 {
     $this->beforeSend();
     $LetterModel = $this->_dataToModel();
     $LetterModel->code = $this->code;
     $LetterModel->date_create = new Expression('NOW()');
     if ($LetterModel->validate()) {
         $result = $LetterModel->save();
         if ($immediately === true) {
             $LetterModel->setMailer(Component::get()->getCloneMailerObject())->sendImmediately();
         }
         $this->_error = $LetterModel->getLastError();
     } else {
         $result = false;
         $firstErrors = $LetterModel->getFirstErrors();
         $this->_error = array_shift($firstErrors);
     }
     $this->afterSend();
     return $result === true ? (int) $LetterModel->id : false;
 }
Beispiel #8
0
 /**
  * @expectedException \rmrevin\yii\postman\Exception
  */
 public function testDriverError()
 {
     $Postman = \rmrevin\yii\postman\Component::get();
     $Postman->driver = 'unknown';
     $Postman->reconfigureDriver();
 }
 /**
  * @depends testMain
  * @param \rmrevin\yii\postman\ViewLetter $Letter
  * @throws \rmrevin\yii\postman\Exception
  */
 public function testSendSMTP(postman\ViewLetter $Letter)
 {
     $Postman = \rmrevin\yii\postman\Component::get();
     $Postman->driver = 'smtp';
     $Postman->reconfigureDriver();
     $Letter->setSubject('SMTP html letter');
     $this->assertInternalType('integer', $Letter->send());
     $this->assertInternalType('integer', $Letter->send(true));
     $this->assertEmpty($Letter->getLastError(), $Letter->getLastError());
     $count = postman\models\LetterModel::find()->where(['subject' => 'SMTP html letter'])->count();
     $this->assertEquals(2, $count);
 }
Beispiel #10
0
 /**
  * @param int $num_letters_per_step
  * @return int
  * @throws \yii\base\Exception
  */
 public static function cron($num_letters_per_step = 10)
 {
     $send = 0;
     $Postman = Component::get();
     /** @var static[] $LetterModels */
     $LetterModels = static::find()->onlyNotSend()->orderBy(['id' => SORT_ASC])->limit($num_letters_per_step)->all();
     foreach ($LetterModels as $LetterModel) {
         $LetterModel->setMailer($Postman->getCloneMailerObject());
         $LetterModel->sendImmediately();
         $err = $LetterModel->getLastError();
         if (!empty($err)) {
             throw new \yii\base\Exception($err);
         } else {
             $send++;
         }
     }
     return $send;
 }