yII2 邮件发送

阅读更多
1.在配置文件mail.php

 'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
                                    'class' => 'Swift_SmtpTransport',
                                    'host' => 'smtp.163.com',
                                    'username' => '[email protected]',
                                    'password' => '*******',
                                    'port' => '25',
                                    'encryption' => 'tls',
                                  
                            ], 
            'messageConfig'=>[
                'charset'=>'UTF-8',
                'from'=>['[email protected]'=>'admin']
                ],
        ],



controller 代码

 $mail= Yii::$app->mailer->compose();
  
        $mail->setTo('[email protected]');
        $mail->setSubject("邮件测试");
        $mail->setTextBody('zheshisha ');
        $mail->setHtmlBody("问我我我我我");
        var_dump($mail->send());
        if($mail->send())
            echo "success";
        else
            echo "failse"; 
      die();


ok,这样就可以发送邮件了

如需加载模板 把$mail= Yii::$app->mailer->compose();
修改成 $mail= Yii::$app->mailer->compose('xiaoma',['aa'=>222]);
xiaoma这个模板在:
yII2 邮件发送_第1张图片

xiaoma.php里面的代码

urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $aa]);
?>


< a href="#" >


加载模板的邮件代码:

 $mail= Yii::$app->mailer->compose('xiaoma',['aa'=>222]);
  
        $mail->setTo('[email protected]');
        $mail->setSubject("邮件测试");
        $mail->setTextBody('zheshisha ');
        var_dump($mail->send());
        if($mail->send())
            echo "success";
        else
            echo "failse"; 
      die();



希望对你们有帮助!
  • yII2 邮件发送_第2张图片
  • 大小: 14.9 KB
  • 查看图片附件

你可能感兴趣的:(yii2,mail,邮件发送)