【问题标题】:how to send mail using CRUD in YII2如何在 YII2 中使用 CRUD 发送邮件
【发布时间】:2017-12-27 08:55:11
【问题描述】:

我有任务分配器的 CRUD。以及使用选择 2 小部件创建的任务下拉列表。 发送邮件我在 controller

中编写了代码
public function actionCreate()
{
 $model =new TaskDistributor();
 $modelcompany = new Companies();

 $companies = Companies::find()->all();
 $companiesList = ArrayHelper::map($companies,'id','name');

 if ($model->load(Yii::$app->request->post()))
  {
   $model->task = UploadedFile::getInstance($model,'task');       
       if( $model->task )
       {
        $value = Yii::$app->mailer->compose()
        ->setFrom(['pharmadarshu@gmail.com' => 'task_distributor'])
        ->setTo($model->sendto)
        ->setSubject($model->subject)
        ->setHtmlBody($model->comment)
        ->attach($model->task)
        ->send();
       }
       else{
        $value = Yii::$app->mailer->compose()
        ->setFrom(['pharmadarshu@gmail.com' => 'task_distributor'])
        ->setTo($model->sendto)
        ->setSubject($model->subject)
        ->setHtmlBody($model->task)
        ->send();
       }
       $model->save();
      // print_r($model->getErrors()); die(); it gives task can not be blank
       return $this->redirect(['view', 'id' => $model->no]);
      }else{
return $this->render('create',[
'model' => $model ,
'companiesList' => $companiesList,
]);
}
}

当我运行代码时,它会发送邮件。但是邮件中的任务字段为空并给出错误=> 400 错误请求。 那么如何获取邮件中select2下拉选择值的值呢?

【问题讨论】:

  • 发帖后检查$model->task的值。
  • 它是空白的..为什么会这样??我从下拉列表中选择了为什么它是空白的
  • 也检查Yii::$app->request->post()。添加您的模型和表单。

标签: email yii2


【解决方案1】:
public function actionCreate()
{
 $model =new TaskDistributor();
 $modelcompany = new Companies();

 $companies = Companies::find()->all();
 $companiesList = ArrayHelper::map($companies,'id','name');

 if ($model->load(Yii::$app->request->post()))
  {

        $value = Yii::$app->mailer->compose()
        ->setFrom(['pharmadarshu@gmail.com' => 'task_distributor'])
        ->setTo($model->sendto)
        ->setSubject($model->subject)
        ->setHtmlBody($model->task)
        ->send();

       $model->save();

       return $this->redirect(['view', 'id' => $model->no]);
      }else{
    return $this->render('create',[
    'model' => $model ,
    'companiesList' => $companiesList,
]);
}
}

【讨论】:

  • “任务”不是附件。我想从下拉列表中选择它
  • 你的问题是没有得到 $model->task ?
  • 谢谢...这是我的错误...刚刚删除... $model->task = UploadedFile::getInstance($model,'task'); ...而且效果很好
  • 如果我想再发送一个字段怎么办->setHtmlBody($model->comment)
  • 你想在html正文中结合commaent和task .?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多