yii中发送邮件组件mailer的配置和使用
经过自己的摸索发现在yii中有一个很实用的e-mail发送插件——malier。下载地址是http://www.yiiframework.com/extension/mailer/。下载后,将文件解压在protected/extensions文件夹下。然后有两种方法使用
方法一:当做系统组件使用。
现在配置文件main.php中做一下配置。
<?php 'components'=>array( 'mailer' => array( 'class' => 'application.extensions.mailer.EMailer', 'pathViews' => 'application.views.email', 'pathLayouts' => 'application.views.email.layouts' ), // ...}
然后在控制器中使用的方法如下:
<?php $message = 'Hello World!'; Yii::app()->mailer->Host = 'smtp.yiiframework.com'; Yii::app()->mailer->IsSMTP(); Yii::app()->mailer->From = 'wei@pjkui.com'; Yii::app()->mailer->FromName = 'Wei'; Yii::app()->mailer->AddReplyTo('wei@pjkui.com'); Yii::app()->mailer->AddAddress('qian@pjkui.com'); Yii::app()->mailer->Subject = 'Yii rulez!'; Yii::app()->mailer->Body = $message; Yii::app()->mailer->Send();
方法二,直接当做一种组件使用。
<?php $message = 'Hello World!'; $mailer = Yii::createComponent('application.extensions.mailer.EMailer'); $mailer->Host = <your smtp host>; $mailer->IsSMTP(); $mailer->From = 'wei@pjkui.com'; $mailer->AddReplyTo('wei@pjkui.com'); $mailer->AddAddress('qiang@pjkui.com'); $mailer->FromName = 'Wei Yard'; $mailer->CharSet = 'UTF-8'; $mailer->Subject = Yii::t('demo', 'Yii rulez!'); $mailer->Body = $message; $mailer->Send();
配置好后,就可以用挂上面的发送一个测试的小邮件了。