yii2 表单发送附件

yii2 form to send attachment

我有一些表格可以发送电子邮件,但我不知道该怎么做,我目前正在使用 yii2 这是我的表格

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use yii\mail\BaseMailer;

$this->title = 'Career';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin(['id' => 'career-form']); ?>

<?= $form->field($model, 'name')->textInput(['autofocus' => true, 'placeholder' => 'Name', 'class' => 'required'])->label(false) ?>

<?= $form->field($model, 'files')->fileInput() ?>

<input id="career-form-submit" type="submit" value="SUBMIT">
<?php if (Yii::$app->session->hasFlash('CareerFormSubmitted')): ?>
<?php ActiveForm::end(); ?>

这是我的模型

 <?php

 namespace app\models;

 use Yii; use yii\base\Model; use yii\web\UploadedFile;

 class CareerForm extends Model {
     public $name;
     public $files;
     public function rules()
     {
         return [

            [['name','files'], 'required'], ['files','file'],];
     }
     public function upload()
     {

         if ($this->validate()) {
             $this->files->saveAs('uploads/career/' . $this->file->baseName . '.' . $this->files->extension);
             $this->files = 'uploads/career/' . $this->file->baseName . '.' . $this->files->extension;
             return true;
         } else {
             return false;
         }
     }


    public function career($email)
    {
        if ($this->validate()) {

                Yii::$app->mailer->compose('mail.php' ,[
                    'name' => $this->name,
                    ])
                    ->setTo($email)
                    ->setFrom([$this->email => $this->name])
                    ->setSubject('subject, '.$this->name)
                    ->attach($this->files)
                    ->send();
                return true;
            }
            return false;
        }
    }

我的网站管理员是

public function actionCareer_2()
    {
        $model = new CareerForm();
        //$model->upload();

        if ($model->load(Yii::$app->request->post()) && $model->career(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('CareerFormSubmitted');
            $model->files = UploadedFile::getInstance($model, 'files');
            $model->upload();
            return $this->refresh();
        }
        return $this->render('career_2', [
            'model' => $model,
        ]);
    }

但是还是报错,有谁能帮帮我吗?纠正哪一个,我还是新手使用yii2

我想要的是使用我保存在邮件目录中的 mail.php 发送消息,它将保存用户上传的文件并将其附加到电子邮件中,感谢您的回答

编辑:我的 xampp 的错误只是说 "An internal server error occurred." 但是,它发送了电子邮件,我认为错误来自上传的文件,它没有将文件存储到目录 uploads/career 并且电子邮件没有附件

编辑:按照建议检查 app.log 后我发现了一些错误 错误:Class 'app\controllers\UploadedFile' 在站点控制器中找不到但是当我将错误更改为 "unknown properties" 时,这是我添加

后的完整错误
yii\base\UnknownPropertyException: Getting unknown property: app\models\CareerForm::file in C:\xampp\htdocs\project\vprojectr\yiisoft\yii2\base\Component.php:143

if (method_exists($this, 'set' . $name)) { throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); } else { throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); }

Stack trace:

#0 C:\xampp\htdocs\project\models\CareerForm.php(86): yii\base\Component->__get('file')

$this->files->saveAs('uploads/career/' . $this->file->baseName . '.' . $this->files->extension);

#1 C:\xampp\htdocs\project\controllers\SiteController.php(117): app\models\CareerForm->upload()

$model->upload();

#2 [internal function]: app\controllers\SiteController->actionCareer_2()
#3 C:\xampp\htdocs\project\vprojectr\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
#4 C:\xampp\htdocs\project\vprojectr\yiisoft\yii2\base\Controller.php(154): yii\base\InlineAction->runWithParams(Array)
#5 C:\xampp\htdocs\project\vprojectr\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('career_2', Array)
#6 C:\xampp\htdocs\project\vprojectr\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('site/career_2', Array)
#7 C:\xampp\htdocs\project\vprojectr\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#8 C:\xampp\htdocs\project\web\index.php(12): yii\base\Application->run()

终于在 2 天后,我成功解决了我的问题,这是我使用的更新和最终代码 在模型上

public function career($email,$filess)
    {if ($this->validate()) {

                Yii::$app->mailer->compose('mail.php' ,[
                    'name' => $this->name,])
                    ->setTo($email)
                    ->setFrom([$this->email => $this->name])
                    ->setSubject('subject, '.$this->name)
                    ->attach($filess)
                    ->send();
                    return true;
            }
            return false;
        }

在站点控制器上

public function actionCareer_2()
    {
        $model = new CareerForm();

        if (Yii::$app->request->isPost) {
            $model->files = UploadedFile::getInstance($model, 'files');
            $model->files->saveAs('uploads/career/' . $model->files->baseName . '.' . $model->files->extension);
            $model->path = 'uploads/career/' . $model->files->baseName . '.' . $model->files->extension;
        }
        if ($model->load(Yii::$app->request->post()) && $model->career(Yii::$app->params['adminEmail'],$model->path)) {
           Yii::$app->session->setFlash('CareerFormSubmitted');
           return $this->refresh();
        }
        return $this->render('career_2', ['model' => $model]);
    }

感谢所有对我的问题发表评论的人,美好的一天