Почему-то письма отправляются самому пользователю а не админу. Как перенастроить?
Смена setTo на setFrom не срабатывает.
Controller - IndexController (extends AppController)
Форма -СontactForm
Вид - views/index/contact.php
ContactForm
Код: Выделить всё
<spoiler title="">
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Expression;
error_reporting(E_ALL);
/**
* This is the model class for table "contactform".
*
* @property string $id
* @property string $name
* @property string $email
* @property integer $subject
* @property double $body
* @property string $verifyCode
*/
class Contactform extends ActiveRecord
{
public $id;
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
public static function tableName()
{
return 'contactform';
}
/**
* @return array the validation rules.
*/
public function rules()
{
return [
/* Поля обязательные для заполнения */
[ ['name', 'email', 'subject', 'body'], 'required'],
/* Поле электронной почты */
['email', 'email'],
/* Капча */
['verifyCode', 'captcha', 'captchaAction'=>'index/captcha'],
/* ['verifyCode', 'captcha','captchaAction'=>'/contactus/default/captcha'],*/
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Подтвердите код',
'name' => 'Имя',
'email' => 'Электронный адрес',
'subject' => 'Тема',
'body' => 'Сообщение',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
* @param string $email the target email address
* @return boolean whether the model passes validation
*/
public function contact($adminEmail)
{
if ($this->validate()) {
Yii::$app->mailer->compose()
->setFrom(Yii::$app->params['adminEmail']) /* от кого */
->setTo([$this->email => $this->name])
// ->setFrom([$this->email => $this->name])
// ->setFrom(Yii::$app->params['adminEmail'])
// ->setTo($adminEmail)
// ->setFrom([$this->email => $this->name])
// ->setTo([$this->email => $this->name])
// ->setFrom ($adminEmail)
// ->setTo([$adminEmail])
// ->setFrom ([$this->email => $this->name])
// ->setFrom([$this->email => $this->name])
// ->setTo(['[email protected]' => $this->name])
//->setTo($adminEmail)
->setSubject($this->subject) /* имя отправителя */
->setTextBody($this->body) /* текст сообщения */
->send();
return true;
}
return false;
}
}
</spoiler>
Код: Выделить всё
<spoiler title="">
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false, //если false то письма будут отпр. если true то в папке runtime
'transport'=>[
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.mail.ru',
'port' => '465', // для mail.ru
'encryption' => 'ssl', // tls
'username' => '[email protected]',
'password' => 'password',
// 'host' => 'smtp.gmail.com',
/* 'options' => array('hostname' => 'smtp.gmail.com',*/
// 'username' => '[email protected]',
// 'password' => 'password',
// 'port' => '25',
// "encryption" => ("tls"),
// 'viewPath' => '@common/mail',
],
],
</spoiler>
Код: Выделить всё
<spoiler title="">
<?php
namespace app\controllers;
use Yii;
use yii\App\Controller;
use app\models\ContactForm;
use yii\web\Request;
class IndexController extends AppController
{
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
]
];
}
public function actionContact()
{
$model = new ContactForm();
/*if ($model->load(Yii::$app->request->post()) && $model->contact(setting::ADMIN_EMAIL_ADDRESS))*/
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail']))
{
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
}
</spoiler>
SiteControler
Код: Выделить всё
<spoiler title="">
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
//use app\models\MailerForm;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
//'actions' => ['logout'],
'actions' => ['captcha','index','logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post','get'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(setting::ADMIN_EMAIL_ADDRESS)) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
} else {
return $this->render('default', [
'model' => $model,
]);
}
}
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
public function actionAbout()
{
return $this->render('about');
}
/* public function actionMailer()
{
$model = new MailerForm();
if ($model->load(Yii::$app->request->post()) && $model->sendEmail()) {
Yii::$app->session->setFlash('mailerFormSubmitted');
return $this->refresh();
}
return $this->render('mailer', [
'model' => $model,
]);
}*/
}
</spoiler>