Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php yii2发送附件的表格_Php_Email_Yii2_Attachment - Fatal编程技术网

Php yii2发送附件的表格

Php yii2发送附件的表格,php,email,yii2,attachment,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

我有一些表格可以发送电子邮件,但我不知道如何发送,我目前正在使用
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(); ?>
但还是有错误,有人能帮我吗?要更正哪一个,我仍然是使用
yii2
的新手

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

编辑:我的
xampp
中的错误只是说“发生了一个内部服务器错误”。但是,它发送了电子邮件,我认为错误来自上传的文件,它没有将文件存储到目录uploads/career,并且电子邮件没有附件

编辑:在检查app.log之后,我发现了一些错误 错误:在site controller中找不到Class
'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
如果(方法_存在($this,'set'.$name)){ 抛出新的InvalidCallException('Getting write only property:'。get_class($this)。':'。$name); }否则{ 抛出新的UnknownPropertyException('Getting UnknownProperty:'。get_class($this)。':'。$name); }

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

$model->upload()


两天后,我终于成功地解决了我的问题,下面是我使用的更新和最终代码 关于模型

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

感谢所有对我的问题发表评论的人,再见

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

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

感谢所有对我的问题发表评论的人,早安

错误是什么?分享错误详细信息我不知道错误,但我的xampp localhost只显示“发生了内部服务器错误”。请查看您的xampp服务器错误日志(或
yourapp/frontend/runtime/logs/app.log
),让我们知道发生了什么。否则,我们无法帮助您。错误是什么?共享错误详细信息我不知道错误,但我的xampp localhost仅显示“发生了内部服务器错误”。请检查您的xampp服务器错误日志(或
yourapp/frontend/runtime/logs/app.log
),让我们知道发生了什么。否则我们帮不了你。
#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()
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]);
    }