yii-mail yii 发送邮件
参考网址: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, )); } }