Browse Source

updated apps to use the mail component.

Qiang Xue 12 years ago
parent
commit
e1561bc3d1
3 changed files with 11 additions and 7 deletions
  1. 1 0
      composer.json
  2. 3 0
      config/web.php
  3. 7 7
      models/ContactForm.php

+ 1 - 0
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 - 0
config/web.php

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

+ 7 - 7
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;