如何在YII2中使用CRUD发送邮件

时间:2017-12-27 08:55:11

标签: email yii2

enter image description here

我有任务经销商的CRUD。以及使用select 2小部件创建的任务下拉列表。 发送邮件我在控制器

中编写了代码
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下拉选择值的值?

1 个答案:

答案 0 :(得分:0)

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,
]);
}
}