Search code examples
emailyii

Yii 2 Call to a member function saveAs() on string


new to Yii and I am getting this error on a Send Email page. The string in question is the path to the attachment and the attachment name and i am presuming that the saveAs function would expect a string. Any ideas what i am missing?

The form:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\emails */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="emails-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

    <?= $form->field($model, 'reciever_name')->textInput(['maxlength' => 50]) ?>

    <?= $form->field($model, 'receiver_email')->textInput(['maxlength' => 200]) ?>

    <?= $form->field($model, 'subject')->textInput(['maxlength' => 255]) ?>

    <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'attachment')->fileInput(['maxlength' => 255]) ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

and the controller:

public function actionCreate()
    {
        $model = new emails();

        if ($model->load(Yii::$app->request->post())) {

            // upload the attachment
            $model->attachment = UploadedFile::getInstance($model, 'attachment');

            if($model->attachment)
            {
                parent::init();
                $time = time();
                //$model->attachment->saveAs('attachments/'.$time.'.'.$model->attachment->extension);
                //$model->attachment = 'attachments/'.$time.'.'.$model->attachment->extension;
            }
            if($model->attachment)
            {
                $value = Yii::$app->mailer->compose()
                ->setFrom(['my_email@gmail.com' => 'Paul'])
                ->setTo ($model->receiver_email)
                ->setSubject ($model->subject)
                ->setHtmlBody ($model->content);
                foreach ($model->attachment as $file) {
                    //$filename = 'attachments/'.$time.'.'.$model->attachment->extension;
                    $filename = 'attachments/file.jpg';
                    //var_dump($filename);die();
                    $file->saveAs($filename);
                    $value->attach('attachments/file.jpg');
                    //$value->attach('attachments/'.$time.'.'.$model->attachment->extension);
                }
                $value->send();
            }else{
             $value = Yii::$app->mailer->compose()
             ->setFrom(['my_email@gmail.com' => 'Paul'])
             ->setTo($model->receiver_email)
             ->setSubject($model->subject)
             ->setHtmlBody($model->content)
             ->send();
         }

         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     }

     return $this->render('create', [
        'model' => $model,
    ]);
 }

I have tried absolute paths as well as dymanic paths, but all has the same output, i am stuck


Solution

  • Save as() function require the actual path. So that is issue. Please make sure your path should be correct and accessible.