И так.
Загрузка файла из формы обратной связи.
Вид:
Код: Выделить всё
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data', 'id' => 'contact-form']]); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'phone')->textInput(['placeholder' => "+375(29)348-76-88"]) ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'file_load')->fileInput() ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
<?php ActiveForm::end(); ?>
</div>
</div>
Контроллер:
Код: Выделить всё
public function actionOneForm()
{
$model = new OneForm();
if ($model->load(Yii::$app->request->post())) {
$model->file_load = UploadedFile::getInstance($model, 'file_load');//загрузка файла
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('success', 'Спасибо за Ваше письмо. Мы постараемся как можно быстрее Вам ответить!');
return $this->refresh();
} else {
Yii::$app->session->setFlash('error', 'Внимание! Ваше письмо по каким-то причинам не отправлено!!!');
}
}
return $this->render('one-form', compact('model'));
}
Модель:
Код: Выделить всё
class OneForm extends Model
{
public $name;
public $email;
public $phone;
public $subject;
public $body;
public $file_load;
public function rules()
{
return [
[['name', 'email', 'subject', 'body'], 'required'],
['email', 'email'],
['name', 'string', 'length' => [2, 100]],
[['name', 'email'], 'trim'],
['phone', 'match', 'pattern' => '/\+\d{1,3}\(?\d{1,3}\)\d{1,3}\-\d{2}-\d{2}$/'],
[['file_load'], 'file'],
];
}
public function attributeLabels()
{
return [
'name' => 'Имя',
'email' => 'E-mail',
'phone' => 'Телефон',
'subject' => 'Тема',
'body' => 'Текст',
'file_load' => 'Файл',
];
}
public function sendEmail($email)
{
if ($this->validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom(['[email protected]' => 'My Company'])
->setReplyTo($this->email)
->setSubject($this->subject)
->setTextBody($this->body)
->setHtmlBody(
'<h3>Здравствуйте, меня зовут '.$this->name.'</h3>'
.$this->body
.'<p style="font-weight: bold;">Телефон: ' . $this->phone . '</p>'
.'<p style="font-weight: bold;">Почта: ' . $this->email . '</p>'
)
->attach($this->file_load->tempName, ['fileName' => $this->file_load->baseName . '.' . $this->file_load->extension])
->send();
return true;
} else {
return false;
}
}
}
Ну и соответственно в config/main.php настраивается 'mailer'
Всем, кто принимал непосредственное участие, огромное спасибо!