/** * @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'); }
public function down() { $this->dropTable(\rmrevin\yii\postman\Component::get()->table); }
/** * 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; }
/** * @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); }
/** * @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; }