【问题标题】:Yii Mailer doesn't workYii Mailer 不工作
【发布时间】:2014-10-17 03:17:52
【问题描述】:

我尝试使用 yii simple mailer 发送邮件...

我按照步骤:

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);

然后浏览器给了我错误: 未定义属性“PhpMailer.server”。

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

【问题讨论】:

  • 你的配置文件中添加了扩展类吗?
  • @nosthertus 'class' => 'ext.mailer.SmtpMailer', 不考虑添加?
  • 在你的配置文件中尝试import=>array('ext.mailer.*');
  • @nosthertus 你的意思是 config/console.php 吗?
  • 你的配置main.php

标签: php yii


【解决方案1】:

在 config/main.php 中

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

在组件/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();  
}

}

进程正常但没有收到邮件

【讨论】:

  • 你必须从你的控制器调用这个函数 $this->mailsend($to,$from,$from_name,$subject,$message,$cc,$attachment);不在邮件发送中。这将进入无限循环。所以从这里移除并从你需要的控制器调用。
  • @YatinMistry 是的,我放入控制器不起作用
  • @YatinMistry 实际上我想做电子邮件验证...用户单击提交按钮然后系统将自动为他们发送电子邮件
  • @YatinMistry 我编辑了代码...浏览器给了我这条消息 RegisterController 及其行为没有名为“mailTemplate”的方法或闭包
  • 更改这一行 $mail->MsgHTML($this->mailTemplate($message));到 $mail->MsgHTML($message);这是我的自定义方法,它为我的应用程序中的所有邮件程序提供了通用模板。所以你不需要使用它。
【解决方案2】:

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'
    ),

【讨论】:

  • 没有 main/config.php 之类的东西
  • @noc2spamツ 输入错误。
  • 感谢您的修复。我撤消了我的反对票。干杯:)
猜你喜欢
  • 2015-04-27
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
相关资源
最近更新 更多