如何在YII中包含PEAR的邮件和SMTP功能?

时间:2012-12-03 14:33:48

标签: php yii pear

我已经设置了梨,之前卡住了:

require_once('Mail.php');

我设法通过修复PHP.ini中的路径来解决这个问题,但现在YII抱怨:

include(LOGIN.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such  file or directory

我不知道安装哪个库来让PEAR接收LOGIN.php,如果这是问题所在。也可能是YII不允许导入LOGIN.PHP,因为它可能有自己的?我抓住了吸管。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

这是因为Yii的自动加载器,它设置为找到Yii的类,这与PEAR的标准不同,你需要做什么,它注册PEAR的自动加载器

您可以在the guide

中了解相关信息

您可以使用如下所示的自动加载器:

static function loadClass($className){
  include str_replace('_','/',$className).'.php';
  return class_exists($className, false) || interface_exists($className, false);
  return false;
 }

PEAR扩展应该已经在PHP的包含路径中才能工作

请参阅Zend Autoloader extension,了解如何实施此

答案 1 :(得分:1)

YiiMail 是一个可用于发送带或不带smtp邮件的扩展程序,这是一封包裹SwiftMailer的电子邮件扩展程序。此扩展程序还允许您从视图文件创建电子邮件。 从here

下载

在您的配置文件中,在组件部分

中包含以下代码
'mail' => array(
                'class' => 'application.extensions.yii-mail.YiiMail',
                'transportType'=>'smtp',
                'transportOptions'=>array(
                    'host'=>'smtp.googlemail.com',
                    'username'=>'test@gmail.com',//
                    'password'=>'passwd',
                    'port'=>'465',
                    'encryption'=>'ssl',
                ),
                'viewPath' => 'application.views.mail',
                'logging' => true,
                'dryRun' => false
        ),

在控制器操作部分中使用类似下面的内容

$message = new YiiMailMessage;
$message->view = 'registrationFollowup';

//userModel is passed to the view
$message->setBody(array('userModel'=>$userModel), 'text/html');


$message->addTo($userModel->email);
$message->addBcc('someone@gmail.com');
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);

视图registrationFollowup位于视图文件夹内的邮件文件夹中,视图路径可从配置文件('viewPath' => 'application.views.mail')中理解