yii2 form to send attachment

懵懂的女人 提交于 2019-12-12 05:03:32

问题


I have some form to send a email, but I didn't know how to do it, I'm currently using yii2 here is my form

<?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(); ?>

and here is my models

 <?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;
        }
    }

and my site controller is

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

but it still error, does anyone can help me? which one to correct, I'm still newbie to use yii2.

what I want is to send the message using mail.php which I save on mail directory and it will save the file that user upload and attach it on the email, thanks for the answer

EDIT: the error from my xampp just said "An internal server error occurred." but, it sent email, i think the error is from uploaded file, it is doesn't store the file to directory uploads/career and the email didn't have attachment

EDIT: after checking the app.log like suggested i found some error Error: Class 'app\controllers\UploadedFile' not found in site controller but when I put that the error change to "unknown properties", here is the full error after I adding the

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

回答1:


finally after 2 days, I've successfully solve my problem, here is the update and final code I used on models

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;
        }

and on the site controller

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

thanks for anyone who commented to my question, good day



来源:https://stackoverflow.com/questions/46660315/yii2-form-to-send-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!