yii2表单发送附件

时间:2017-10-10 06:51:27

标签: php email yii2 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的错误只是说“发生内部服务器错误”。但是,它发送了电子邮件,我认为错误来自上传文件,它不存储文件到目录上传/职业和电子邮件没有附件

编辑:检查app.log后,如建议我发现一些错误 错误:在站点控制器中找不到类'app\controllers\UploadedFile'但是当我将错误更改为“未知属性”时,添加

后出现完整错误
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)){               抛出新的InvalidCallException('获取只写属性:'。get_class($ this)。'::'。$ name);           } else {               抛出新的UnknownPropertyException('获取未知属性:'。get_class($ this)。'::'。$ name);           }

Stack trace:

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

$ this-&gt; files-&gt; saveAs('uploads / career /'。$ this-&gt; file-&gt; baseName。'。'。   $这 - &GT;文件 - &GT;扩展名);

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

$模型 - &GT;上传();

#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()

1 个答案:

答案 0 :(得分:0)

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

感谢任何评论我的问题的人,美好的一天