public function getSource($name)
 {
     $currentLanguage = \Yii::$app->language;
     $defaultLanguage = ArrayHelper::getValue(EmailManager::getInstance()->languages, 0, 'en-US');
     /** @var Template $model */
     $model = Template::find()->where(['shortcut' => $name])->andWhere('language = :currentLanguage OR language = :defaultLanguage OR language = :systemDefaultLanguage', [':currentLanguage' => $currentLanguage, ':defaultLanguage' => $defaultLanguage, ':systemDefaultLanguage' => 'en-US'])->one();
     if (!$model) {
         \Yii::error("Missing template {$name}, current language {$currentLanguage}, default language {$defaultLanguage}", 'email');
         return "!!! UNKNOWN TEMPLATE {$name} !!!";
     }
     return $model->getAttribute($this->attributeName);
 }
 /**
  * Send one email from queue
  * @return bool
  * @throws \Exception
  * @throws \yii\db\Exception
  */
 public function sendOne()
 {
     $db = \Yii::$app->db;
     $transaction = $db->beginTransaction();
     try {
         $id = $db->createCommand('SELECT id FROM {{%email_message}} WHERE status=:status ORDER BY priority DESC, id ASC LIMIT 1 FOR UPDATE', ['status' => Message::STATUS_NEW])->queryScalar();
         if ($id === false) {
             $transaction->rollback();
             return false;
         }
         /** @var Message $model */
         $model = Message::findOne($id);
         $model->status = Message::STATUS_IN_PROGRESS;
         $model->updateAttributes(['status']);
         $transaction->commit();
     } catch (\Exception $e) {
         $transaction->rollback();
         throw $e;
     }
     $transaction = $db->beginTransaction();
     try {
         $result = EmailManager::getInstance()->send($model->from, $model->to, $model->subject, $model->text, $model->files, $model->bcc);
         if ($result) {
             $model->sentAt = new Expression('NOW()');
             $model->status = Message::STATUS_SENT;
         } else {
             $model->status = Message::STATUS_ERROR;
         }
         $model->updateAttributes(['sentAt', 'status']);
         $transaction->commit();
     } catch (\Exception $e) {
         $transaction->rollback();
         throw $e;
     }
     return true;
 }
Beispiel #3
0
 * @var yarcode\email\models\Template $model
 * @var yii\widgets\ActiveForm $form
 */
?>

<div class="email-template-form">

    <?php 
$form = ActiveForm::begin();
?>

    <?php 
echo $form->field($model, 'shortcut')->textInput(['maxlength' => 255]);
?>
    <?php 
echo $form->field($model, 'language')->dropDownList(array_combine(\yarcode\email\EmailManager::getInstance()->languages, \yarcode\email\EmailManager::getInstance()->languages));
?>
    <?php 
echo $form->field($model, 'from')->textInput(['maxlength' => 255]);
?>
    <?php 
echo $form->field($model, 'subject')->textInput(['maxlength' => 255]);
?>

    <?php 
echo $form->field($model, 'text', ['enableClientValidation' => false])->widget('trntv\\aceeditor\\AceEditor', ['mode' => 'html']);
?>

    <div class="form-group">
        <?php 
echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
 /**
  * Queues current template for sending with the given priority
  * @param $to
  * @param array $params
  * @param int $priority
  * @param array $files
  * @param null $bcc
  * @return bool
  * @throws \yii\base\InvalidConfigException
  */
 public function queue($to, array $params = [], $priority = 0, $files = [], $bcc = null)
 {
     $text = nl2br($this->renderAttribute('text', $params));
     $subject = $this->renderAttribute('subject', $params);
     EmailManager::getInstance()->queue($this->from, $to, $subject, $text, $priority, $files, $bcc);
     return true;
 }