yii2 - 邮件发送-示例

---运行时配置---

You can initialize application components using set() method available through application object Yii::$app:

use Yii;

...

// Get config from db here

Yii::$app->set('mailer', [
    'class' => 'yii\swiftmailer\Mailer',
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        // Values from db
        'host' => ... 
        'username' => ...
        'password' => ...
        'port' => ...
        'encryption' => ...
    ],
]);

Then use it as usual:

use Yii;

...

Yii::$app->mailer->...

----------------------------------------------------------------------

1.在配置文件web.php,  components=>[]里面配置

'mailer' => [  
   'class' => 'yii\swiftmailer\Mailer',  
    'useFileTransport' =>false,//false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
   'transport' => [  
       'class' => 'Swift_SmtpTransport',  
       'host' => 'smtp.163.com',  
       'username' => 'xxxxx@163.com',  
       'password' => '*******',  
       'port' => '25',  
       'encryption' => 'tls',  
                           
                   ],   
   'messageConfig'=>[  
       'charset'=>'UTF-8',  
       'from'=>['xxxxx@163.com'=>'admin']  
       ],  
],  
  1. controller控制器中 代码:
<?php
$mail= Yii::$app->mailer->compose();   
$mail->setTo('***********@qq.com');  
$mail->setSubject("邮件测试");  
//$mail->setTextBody('xxxxx');   //发布纯文字文本
$mail->setHtmlBody("<br>xxxxxxx");    //发布可以带html标签的文本
if($mail->send())  
    echo "success";  
else  
    echo "fail";   
die(); 
?>

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

如需加载模板 把 $mail= Yii::$app->mailer->compose();
修改成 $mail= Yii::$app->mailer->compose('moban',['aa'=>222]);
注:aa是想xiaoma.php里面传递的参数。

邮件模板 moban.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('moban',['aa'=>222]);  
       $mail->setTo('xxxx@qq.com');  
       $mail->setSubject("邮件测试");  
       $mail->setTextBody('xxxxx');  
       if($mail->send())  
           echo "success";  
       else  
           echo "failse";   
     die();  


--------使用高级模板示例----

Advanced email templates

In some cases you might want to use templates for email rendering, so, in that case you just need to do something like:

Yii::$app->mail->compose('@app/mail-templates/email01', [/*Some params for the view */])
     ->setFrom('from@domain.com')
     ->setTo('someemail@server.com')
     ->setSubject('Advanced email from Yii2-SwiftMailer')
     ->send();

Or, if you want to use one template for HTML rendering and another for text, do something like this:

Yii::$app->mail->compose(['html' => '@app/mail-templates/html-email-01', 'text' => '@app/mail-templates/text-email-01'], [/*Some params for the view */])
     ->setFrom('from@domain.com')
     ->setTo('someemail@server.com')
     ->setSubject('Advanced email from Yii2-SwiftMailer')
     ->send();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值