Yii Mailer不起作用

时间:2014-10-17 03:17:53

标签: php yii

我尝试使用简单的邮件发送邮件......

我按照步骤:

http://www.yiiframework.com/extension/yii-simple-mailer/

下载扩展程序并输入yii扩展名文件夹:

https://github.com/tlikai/YiiMailer

然后将代码放到config / main.php:

'mailer' => array(
        // for smtp
        'class' => 'ext.mailer.SmtpMailer',
        'server' => 'theserver',
        'port' => '25',
        'username' => 'theadmin',
        'password' => 'thepassword',

        // for php mail
        'class' => 'ext.mailer.PhpMailer',
     ),

然后在我的控制器中我写了这段代码来发送邮件:

$to = 'wahaha@gmail.com';

$subject = 'Hello Mailer';
$content = 'Some content';

Yii::app()->mailer->send($to, $subject, $content);

然后浏览器给了我错误: Property" PhpMailer.server"未定义。

我的代码中是否遗漏了一些内容?

2 个答案:

答案 0 :(得分:1)

在config / main.php

'Smtpmail'=>array(
        'class'=>'ext.smtpmail.PHPMailer',
        'Host'=>"localhost",
        'Username'=>'thesmile1019@gmail.com',
        'Password'=>'wakakaka',
        'Mailer'=>'smtp',
        'Port'=>25,
        'SMTPAuth'=>true,
    ),

在Component / controller.php中

public function mailsend($to,$from,$from_name,$subject,$message)
{
    $mail   = Yii::app()->Smtpmail;
    $mail->SetFrom($from,$from_name);
    $mail->Subject  = $subject;
    $mail->MsgHTML($message);
    $mail->AddAddress($to, "");

    // Add CC
    if(!empty($cc)){
        foreach($cc as $email){
            $mail->AddCC($email);
        }
    }

    // Add Attchments
    if(!empty($attachment)){
        foreach($attachment as $attach){
            $mail->AddAttachment($attach);
        }
    }

    if(!$mail->Send()) {
        return false; // Fail echo "Mailer Error: " . $mail->ErrorInfo;
    }else {
        return true;    // Success
    }
}

在控制器中

public function actionSendMail(){
    $token = $_POST['YII_CSRF_TOKEN'];              
    if ($token !== Yii::app()->getRequest()->getCsrfToken()){

        Yii::app()->end();
    }
        $to = 'thesmile1019@gmail.com';
        $from = 'localhost';
        $from_name = 'mface';
        $subject = 'testing';
        $message = 'testing';


if($token == true){
        $util = new Utility();      

        $util->detectMobileBrowser();

        $util->checkWebSiteLanguageInCookies();

        $this->layout = "masterLayout";
        $this->render('mailsend');

        $this->mailsend($to,$from,$from_name,$subject,$message);
}else{

        print_r("Not Sent");
        die();  
}

}

处理问题不正确但没有收到邮件

答案 1 :(得分:1)

pathtowebroot/protected/config/main.php

中更改您的端口
'Smtpmail'=>array(
        'class'=>'ext.smtpmail.PHPMailer',
        'Host'=>"smtp.gmail.com",
        'Username'=>'thesmile1019@gmail.com',
        'Password'=>'wakakaka',
        'Mailer'=>'smtp',
        'Port'=>465,
        'SMTPAuth'=>true,
        'SMTPSecure' => 'ssl'
    ),