文章出處
文章列表
參考網址:http://shoukii0721.iteye.com/blog/1576225
有很多時候我們需要給用戶發送郵件,作留言,或者是激活郵件。等用途。
需要注意的是,設置發送的郵件得有SMTP 協議。
參考網址:http://jingyan.baidu.com/article/0f5fb099dffe7c6d8334ea31.html
百度知道中的,有提示。
這里的host 是普通郵箱之間的發送郵件。需要在qq郵箱中設置下。
再者,若是 企業郵箱的話,'host'=>'smtp.exmail.qq.com',
在yii中有單獨封裝好的yii-mail 。
1.在main.php中添加
'import'=>array( 'application.extensions.yii-mail.*', ), 'components'=>array( 'mail'=>array( 'class' => 'application.extensions.yii-mail.YiiMail', 'viewPath' => 'application.views.mail', 'logging' => true, 'dryRun' => false, 'transportType'=>'smtp', // case sensitive! 'transportOptions'=>array( 'host'=>'smtp.qq.com', // smtp服務器 'username'=>'*******' // 驗證用戶 'password'=>'*******', // 驗證密碼 'port'=>'25', // 端口號 //'encryption'=>'ssl', ), ), ),
2.在 extentsions文件夾下,將yii-mail 文件包 復制下邊。
3.Model: MailForm.php
<?php class MailForm extends CFormModel { public $from; public $to; public $subject; public $body; public function rules() { return array( array('from, to, subject, body', 'required'), array('from, to, subject, body ','safe'), ); } public function attributeLabels() { return array( 'from' => '送信人', 'to' => '收信人', 'subject' => '標題', 'body' => '郵件內容', ); } } ?>
4 View: index.php
<?php $this->pageTitle=Yii::app()->name . ' - 送信'; $this->breadcrumbs=array('送信',); ?> <h1>郵件送信</h1> <!-- ▼顯示提示信息▼ --> <?php if(Yii::app()->user->hasFlash('success')){ ?> <div class="flash-success"> <?php echo Yii::app()->user->getFlash('success'); ?> </div> <?php } ?> <?php if(Yii::app()->user->hasFlash('failed')){ ?> <div class="flash-error"> <?php echo Yii::app()->user->getFlash('failed'); ?> </div> <?php } ?> <!-- ▲顯示提示信息▲ --> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'mail-form', 'method'=>'post', 'enableClientValidation'=>true, 'clientOptions'=>array(), )); ?> <?php //echo $form->errorSummary($model); ?> <!-- 送信元 --> <div class="row"> <?php echo $form->labelEx($model,'from'); ?> <?php echo $form->textField($model,'from'); ?> <?php echo $form->error($model,'from'); ?> </div> <!-- 送信先 --> <div class="row"> <?php echo $form->labelEx($model,'to'); ?> <?php echo $form->textField($model,'to'); ?> <?php echo $form->error($model,'to'); ?> </div> <!-- 件名 --> <div class="row"> <?php echo $form->labelEx($model,'subject'); ?> <?php echo $form->textField($model,'subject'); ?> <?php echo $form->error($model,'subject'); ?> </div> <!-- 內容 --> <div class="row"> <?php echo $form->labelEx($model,'body'); ?> <?php echo $form->textArea($model,'body', array('cols'=>'80', 'rows'=>'10',)); ?> <?php echo $form->error($model,'body'); ?> </div> <div class="row"> <?php echo CHtml::Button('送信', array( 'submit'=>array(), 'params'=>array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken), )); ?> </div> <?php $this->endWidget(); ?> </div>
5 Controller: MailController.php
<?php class MailController extends Controller { public function actionIndex() { $model = new MailForm(); if (isset($_POST["MailForm"])){ $model->attributes=$_POST['MailForm']; if($model->validate()) { $message = new YiiMailMessage(); $message->setFrom(array($model->from => '送信人')); $message->setTo(array($model->to => '收信人')); $message->setSubject($model->subject); $message->setBody($model->body); $sendmail = Yii::app()->mail->send($message) ; if ($sendmail) { Yii::app()->user->setFlash("success", "Emails sent: OK \n" ); $this->refresh(); } else { Yii::app()->user->setFlash("failed", "Emails sent: NG \n"); } } } $this->render('index', array( 'model' => $model, )); } }
文章列表
全站熱搜
留言列表