动态配置smtp在yii中发送邮件


dynamic configure smtp send mail in yii

我在Yii上使用SMTP PHPMailer扩展http://www.yiiframework.com/extension/smtp-mail/并通过在Controller中创建一个函数sentMail成功地发送了邮件。但总的来说,它需要我在main/config.php中进行如下配置:

'Smtpmail'=>array(
        'class'=>'application.extensions.smtpmail.PHPMailer',
        'Host'=>"smtp.gmail.com",
        'Username'=>'your@gmail.com',
        'Password'=>'password here',
        'Mailer'=>'smtp',
        'Port'=>587,
        'SMTPAuth'=>true,
        'SMTPSecure' => 'tls',
    ),

如何从数据库获取所有配置以设置SMTP?我试着写进它的函数init()和_constructor(),但它不起作用。我甚至在控制器中配置函数mailSend,如下所示:

public function mailsend($to, $from = '', $from_name, $subject, $message, $cc = array(), $attachment = array()) {
    $mail = Yii::app()->Smtpmail;
    $mail->Host = Smtpconfig::model()->findByPk(1)->host;
    $mail->Username = Smtpconfig::model()->findByPk(1)->username;
    $mail->Password = Smtpconfig::model()->findByPk(1)->matkhau;
    $mail->IsSMTP = true;
    $mail->Port = Smtpconfig::model()->findByPk(1)->port;
    $mail->SMTPSecure = Smtpconfig::model()->findByPk(1)->phuongthuc;
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->MsgHTML($message);
    $mail->AddAddress($to, "");
    $mail->CharSet = "utf-8";
    // 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
    }
}

但也不起作用。(Smtpconfig是型号的名称,包括管理员smtp邮件信息)

我敢打赌这是您的问题:$mail->IsSMTP = trueIsSMTP是一个方法,您正在为它赋值。这个名称具有误导性,因为它实际上指示mailer使用smtp。尝试$mail->IsSMTP()$mail->Mailer = 'smtp'。否则,我认为你是在正确的轨道上。