Browse Source

updated apps to use the mail component.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
998cf7e675
  1. 1
      apps/advanced/backend/config/main.php
  2. 4
      apps/advanced/common/config/params.php
  3. 1
      apps/advanced/composer.json
  4. 1
      apps/advanced/console/config/main.php
  5. 1
      apps/advanced/frontend/config/main.php
  6. 1
      apps/advanced/frontend/controllers/SiteController.php
  7. 14
      apps/advanced/frontend/models/ContactForm.php
  8. 1
      apps/basic/composer.json
  9. 3
      apps/basic/config/web.php
  10. 14
      apps/basic/models/ContactForm.php

1
apps/advanced/backend/config/main.php

@ -22,6 +22,7 @@ return [
],
'db' => $params['components.db'],
'cache' => $params['components.cache'],
'mail' => $params['components.mail'],
'user' => [
'identityClass' => 'common\models\User',
],

4
apps/advanced/common/config/params.php

@ -12,6 +12,10 @@ return [
'class' => 'yii\caching\FileCache',
],
'components.mail' => [
'class' => 'yii\swiftmailer\Mailer',
],
'components.db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2advanced',

1
apps/advanced/composer.json

@ -16,6 +16,7 @@
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "dev-master",
"yiisoft/yii2-swiftmailer": "dev-master",
"yiisoft/yii2-bootstrap": "dev-master",
"yiisoft/yii2-debug": "dev-master",
"yiisoft/yii2-gii": "dev-master"

1
apps/advanced/console/config/main.php

@ -19,6 +19,7 @@ return [
'components' => [
'db' => $params['components.db'],
'cache' => $params['components.cache'],
'mail' => $params['components.mail'],
'log' => [
'targets' => [
[

1
apps/advanced/frontend/config/main.php

@ -23,6 +23,7 @@ return [
],
'db' => $params['components.db'],
'cache' => $params['components.cache'],
'mail' => $params['components.mail'],
'user' => [
'identityClass' => 'common\models\User',
],

1
apps/advanced/frontend/controllers/SiteController.php

@ -159,6 +159,7 @@ class SiteController extends Controller
$user->password_reset_token = Security::generateRandomKey();
if ($user->save(false)) {
// todo: refactor it with mail component. pay attention to the arrangement of mail view files
$fromEmail = \Yii::$app->params['supportEmail'];
$name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?=';
$subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?=';

14
apps/advanced/frontend/models/ContactForm.php

@ -2,6 +2,7 @@
namespace frontend\models;
use Yii;
use yii\base\Model;
/**
@ -48,13 +49,12 @@ class ContactForm extends Model
public function contact($email)
{
if ($this->validate()) {
$name = '=?UTF-8?B?' . base64_encode($this->name) . '?=';
$subject = '=?UTF-8?B?' . base64_encode($this->subject) . '?=';
$headers = "From: $name <{$this->email}>\r\n" .
"Reply-To: {$this->email}\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-type: text/plain; charset=UTF-8";
mail($email, $subject, $this->body, $headers);
Yii::$app->mail->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
} else {
return false;

1
apps/basic/composer.json

@ -16,6 +16,7 @@
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "dev-master",
"yiisoft/yii2-swiftmailer": "dev-master",
"yiisoft/yii2-bootstrap": "dev-master",
"yiisoft/yii2-debug": "dev-master",
"yiisoft/yii2-gii": "dev-master"

3
apps/basic/config/web.php

@ -17,6 +17,9 @@ $config = [
'errorHandler' => [
'errorAction' => 'site/error',
],
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [

14
apps/basic/models/ContactForm.php

@ -2,6 +2,7 @@
namespace app\models;
use Yii;
use yii\base\Model;
/**
@ -48,13 +49,12 @@ class ContactForm extends Model
public function contact($email)
{
if ($this->validate()) {
$name = '=?UTF-8?B?' . base64_encode($this->name) . '?=';
$subject = '=?UTF-8?B?' . base64_encode($this->subject) . '?=';
$headers = "From: $name <{$this->email}>\r\n" .
"Reply-To: {$this->email}\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-type: text/plain; charset=UTF-8";
mail($email, $subject, $this->body, $headers);
Yii::$app->mail->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
} else {
return false;

Loading…
Cancel
Save