无法使用yii2打开文件以读取[attachments / .jpg]

时间:2017-11-06 12:34:12

标签: php email-attachments yii2-advanced-app

此程序是带有附件的电子邮件,但是当我尝试单击提交按钮时,它会说。

Swift_IoException 无法打开文件进行阅读[attachments / .jpg]

这是我的EmailController.php代码

public function actionCreate()
{
    $model = new emails();

    if ($model->load(Yii::$app->request->post())) 
    {    
         $model->attachment=UploadedFile::getInstance($model,'attachment');

         if($model->attachment)
         {
            $time=time();
            $model->attachment->saveAs('attachments/'.$time.'.'.$model->attachment->extension);
            $model->attachment='attachments/'.'.'.$model->attachment->extension;
         }

         if($model->attachment) 
         {
            $value=Yii::$app->mailer->compose()
            ->setFrom([ 'rodulfoalmeda@gmail.com'=>'Sample' ])//->setFrom([ $company->company_email=>$company->company_name ])
            ->setTo($model->receiver_email)
            ->setSubject($model->subject)
            ->setHtmlBody($model->content)
            ->attach($model->attachment)
            ->send();

         }else
         {
            $value=Yii::$app->mailer->compose()
            ->setFrom([ 'rodulfoalmeda@gmail.com'=>'Sample' ])//->setFrom([ $company->company_email=>$company->company_name ])
            ->setTo($model->receiver_email)
            ->setSubject($model->subject)
            ->setHtmlBody($model->content)
            ->send();
         } 

         $model->save(); 
         return $this->redirect(['view', 'id' => $model->id]);

    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

请帮助..

高级帮助,谢谢你。

1 个答案:

答案 0 :(得分:0)

您在分配$time属性时忽略了$model->attachment

$time = time();
$path = 'attachments/' . $time . '.' . $model->attachment->extension;
$model->attachment->saveAs($path);
$model->attachment = $path;