Answer the question
In order to leave comments, you need to log in
Yii2 on Nginx. How to configure the server so that the appeal through the contact form gets to the email?
Welcome all.
I study Yii2, Linux and Nginx closely.
I installed Nginx, installed Yii2, created separate domains for Frontend and Backend, pulled an HTML template over a couple of pages, installed the PHPMailer extension, and when I started setting up the Contact Form, I realized that mail was not being sent.
I looked through the code 4 times up and down, like there are no errors. Due to the fact that in principle there is no experience, the thoughts are that Nginx is not configured to send mail as such. Can someone please tell me how to set up this matter or in which direction to dig?
Yii2 version: 2.0.10
Linux distribution: Ubuntu 16.10
Nginx version: 1.10.1
PHP version: 7.0.8-3ubuntu3
I'll leave the code just in case:
MainController function:
<?php
namespace app\modules\main\controllers;
use frontend\models\ContactForm;
use frontend\models\Image;
use frontend\models\SignupForm;
use yii\base\Response;
use yii\bootstrap\ActiveForm;
public function actionContact()
{
$model = new ContactForm();
if($model->load(\Yii::$app->request->post()) && $model->validate())
{
$body = "<div>Body: <b> " . $model->body . "</b></div>";
$body = "<div>Email: <b> " . $model->email . "</b></div>";
\Yii::$app->common->sendMail($model->subject,$body);
print "Send success!";
die;
}
return $this->render("contact", ['model' => $model]);
}
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* @inheritdoc
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha', 'captchaAction' => \yii\helpers\Url::to(['main/captcha'])], // site/captcha
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
*
* @param string $email the target email address
* @return bool whether the email was sent
*/
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
}
}
'components' => [
'mail' => [
'class' => 'zyx\phpmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'config' => [
'mailer' => 'smtp',
'host' => 'smtp.yandex.ru',
'port' => '465',
'smtpsecure' => 'ssl',
'smtpauth' => true,
'username' => '*MYEMAIL*@yandex.com',
'password' => '*MYPASSWORD*',
'ishtml' => true,
'charset' => 'UTF-8',
],
], ],
<div class="row contact">
<div class="col-lg-6 col-sm-6 ">
<?php
$form = yii\bootstrap\ActiveForm::begin();
?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(\yii\captcha\Captcha::className(), [
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
'captchaAction' => \yii\helpers\Url::to(['main/captcha'])
]) ?>
<?= \yii\helpers\Html::submitButton('Send Message',['class' => 'btn btn-success']) ?>
<?php
$form = yii\bootstrap\ActiveForm::end();
?>
</div>
<div class="col-lg-6 col-sm-6 ">
<div class="well"><iframe width="100%" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="../../../maps.google.com/fi000001.002642&t=m&z=14&output=embed" ></iframe></div>
</div>
</div>
<?php
namespace frontend\components;
use yii\base\Component;
class Common extends Component{
const EVENT_NOTIFY = 'notify_admin';
public function sendMail($subject,$text,$emailFrom='[email protected]',$nameFrom='Advert'){
if(\Yii::$app->mail->compose()
->setFrom(['[email protected]' => 'Advert'])
->setTo([$emailFrom => $nameFrom])
->setSubject($subject)
->setHtmlBody($text)
->send()){
$this->trigger(self::EVENT_NOTIFY);
return true;
}
}
public function notifyAdmin($event) {
print "Notify Admin";
}
}
Answer the question
In order to leave comments, you need to log in
An example nginx configuration is here https://github.com/yiisoft/yii2/blob/master/docs/g...
check ports. I installed lamp locally and also the mail did not go away, but uploaded it to the hosting and it all worked
In theory, you need to install and configure a mail server with SMTP such as Postfix
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question