官方扩展链接:
http://www.yiiframework.com/extension/mailer/这个扩展配置十分方便,如果有问题的话,可以打开Debug:
- <?php
- $message = ‘Hello World!‘;
- $mailer = Yii::createComponent(‘application.extensions.mailer.EMailer‘);
- $mailer->Host = <your smtp host>;
- $mailer->IsSMTP();
- $mailer->From = ‘wei@example.com‘;
- $mailer->AddReplyTo(‘wei@example.com‘);
- $mailer->AddAddress(‘qiang@example.com‘);
- $mailer->FromName = ‘Wei Yard‘;
- $mailer->SMTPDebug = true;//设置SMTPDebug为true,就可以打开Debug功能,根据提示去修改配置
- $mailer->CharSet = ‘UTF-8‘;
- $mailer->Subject = Yii::t(‘demo‘, ‘Yii rulez!‘);
- $mailer->Body = $message;
- $mailer->Send();
因为我使用163邮箱作为发件地址,这个邮件服务是需要验证功能的。必须输入
用户名和密码才能发送成功!
- <?php
- $message = ‘Hello World!‘;
- $mailer = Yii::createComponent(‘application.extensions.mailer.EMailer‘);
- $mailer->Host = ‘smtp.163.com‘;
- $mailer->IsSMTP();
- $mailer->SMTPAuth = true;
- $mailer->From = ‘from@163.com‘;
- $mailer->AddReplyTo(‘from@163.com‘);
- $mailer->AddAddress(‘to@qq.com‘);
- $mailer->FromName = myName‘;
- $mailer->Username = ‘username‘; //这里输入发件地址的用户名
- $mailer->Password = ‘password‘; //这里输入发件地址的密码
- $mailer->SMTPDebug = true;//设置SMTPDebug为true,就可以打开Debug功能,根据提示去修改配置
- $mailer->CharSet = ‘UTF-8‘;
- $mailer->Subject = Yii::t(‘demo‘, ‘Yii rulez!‘);
- $mailer->Body = $message;
- $mailer->Send();
如果大家使用的服务器没有安装邮件服务器的话,我们可以使用这种方法去发送邮件!