1.下载拓展,解压至extesions/mailer
http://www.yiiframework.com/extension/mailer/
2.配置文件设置component
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'mailer' => array(
'class' => 'application.extensions.mailer.EMailer',
'pathViews' => 'application.views.email',
'pathLayouts' => 'application.views.email.layouts'
),
3.在Control中调用
$message = 'Hello World!';
$mailer = Yii::app()->mailer;
$mailer->Host = 'SMTP HOST';
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->From = 'xxxx';
$mailer->AddAddress('TargetEmailHost');
$mailer->FromName = 'xxx';
$mailer->Username = 'xxx'; //这里输入发件地址的用户名
$mailer->Password = 'xxx'; //这里输入发件地址的密码
$mailer->CharSet = 'UTF-8';
$mailer->Subject = 'Subject';
$mailer->SMTPDebug = false; //调试的时候打开发现错误
$mailer->Body = $message;
$mailer->Send();