Created
March 30, 2016 15:34
-
-
Save korniychuk/59368c73ea0161e195d73aba7eed4972 to your computer and use it in GitHub Desktop.
An easy example how to send feedback emails in yii2 (Advanced template)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'components' => [ | |
... | |
'mailer' => [ | |
'class' => 'yii\swiftmailer\Mailer', | |
'viewPath' => '@common/mail', | |
// 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. | |
'transport' => [ | |
'class' => 'Swift_SmtpTransport', | |
'host' => 'smtp-pulse.com', | |
'username' => '[email protected]', | |
'password' => 'aSDASDASDASDAS', // your password | |
'port' => '2525', | |
// 'encryption' => 'tls', | |
], | |
// 'useFileTransport' => true, | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
... | |
'supportEmail' => '[email protected]', | |
'siteName' => 'My Super site', | |
... | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use yii\helpers\Html; | |
/* @var $this yii\web\View */ | |
/* @var $name string user name*/ | |
/* @var $email string user email */ | |
/* @var $body string the review */ | |
?> | |
<div class="admin-feedback"> | |
<p><?= Html::encode($name) ?>(<?= Html::encode($email) ?>) wrote new review.</p> | |
<blockquote> | |
<?= Html::encode($body) ?> | |
</blockquote> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* @var $this yii\web\View */ | |
/* @var $name string user name*/ | |
/* @var $email string user email */ | |
/* @var $body string the review */ | |
?> | |
<?= $name ?>(<?= $email ?>) wrote new review. | |
The review: | |
<?= $body ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use yii\helpers\Html; | |
/* @var $this yii\web\View */ | |
/* @var $name string user name*/ | |
/* @var $email string user email */ | |
/* @var $body string the review */ | |
?> | |
<div class="user-feedback"> | |
<p><?= Html::encode($name) ?>, thank you for send the review.</p> | |
<p>This very helpful for us</p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* @var $this yii\web\View */ | |
/* @var $name string user name*/ | |
/* @var $email string user email */ | |
/* @var $body string the review */ | |
?> | |
<?= $name ?>, thank you for send the review. | |
This is very helpful for us. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace frontend\controllers; | |
use Yii; | |
... | |
/** | |
* Site controller | |
*/ | |
class SiteController extends Controller | |
{ | |
... | |
/** | |
* Displays contact page. | |
* | |
* @return mixed | |
*/ | |
public function actionContact() | |
{ | |
$model = new ContactForm(); | |
if (...) { | |
if ($model->sendEmail(Yii::$app->params['supportEmail'])) { | |
... | |
} else { | |
... | |
} | |
... | |
} | |
... | |
} | |
// Other code was not changed. Only adminEmail to supportEmail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace frontend\models; | |
... | |
/** | |
* ContactForm is the model behind the contact form. | |
*/ | |
class ContactForm extends Model | |
{ | |
... | |
public function sendEmail($supportEmail) | |
{ | |
$adminSuccess = Yii::$app | |
->mailer | |
->compose( | |
['html' => 'adminFeedback-html', 'text' => 'adminFeedback-text'], | |
[ | |
'email' => $this->email, | |
'body' => $this->body, | |
'name' => $this->name, | |
'supportEmail' => $supportEmail, | |
] | |
) | |
->setFrom([$supportEmail => Yii::$app->params['siteName'] . ' bot']) | |
->setTo($supportEmail) | |
->setSubject('There is new question :' . $this->subject) | |
->send(); | |
$userSuccess = Yii::$app | |
->mailer | |
->compose( | |
['html' => 'userFeedback-html', 'text' => 'userFeedback-text'], | |
[ | |
'email' => $this->email, | |
'body' => $this->body, | |
'name' => $this->name, | |
'supportEmail' => $supportEmail, | |
] | |
) | |
->setFrom([$supportEmail => Yii::$app->params['siteName'] . ' bot']) | |
->setTo($this->email) | |
->setSubject('Than you for wrote review on ' . Yii::$app->params['siteName']) | |
->send(); | |
return $userSuccess && $adminSuccess; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment