yii2表格发送附件 [英] yii2 form to send attachment

查看:87
本文介绍了yii2表格发送附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有某种发送电子邮件的表格,但是我不知道该怎么办,我目前正在使用yii2这是我的表格

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

这是我的模特

 <?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.

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

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

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

我的xampp中的错误只是说发生内部服务器错误".但是,它发送了电子邮件,我认为错误是来自上传的文件,它没有将文件存储到目录上传/职业,并且电子邮件没有附件

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

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

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)){ 抛出新的InvalidCallException('获取仅写属性:'.get_class($ this).'::'.$ name); } 别的 { 抛出新的UnknownPropertyException('获取未知属性:'.get_class($ this).'::'.$ name); }

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

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

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

推荐答案

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

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

并在站点控制器上

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

这篇关于yii2表格发送附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆