欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

yII2 邮件发送

程序员文章站 2022-05-24 18:48:56
...
1.在配置文件mail.php

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



controller 代码

 $mail= Yii::$app->mailer->compose();
  
        $mail->setTo('1401619705@qq.com');
        $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 邮件发送
            
    
    博客分类: Yii2 yii2mail邮件发送

xiaoma.php里面的代码

<?php
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $user common\models\User */

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


< a href="#" ><?php echo $resetLink ?></a>


加载模板的邮件代码:

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



希望对你们有帮助!
  • yII2 邮件发送
            
    
    博客分类: Yii2 yii2mail邮件发送
  • 大小: 14.9 KB