yii 框架用swiftmailer 發送郵件

1.先在配置文件中 config/web.php 中修改配置文件

   

'mailer' => [
   'class' => 'yii\swiftmailer\Mailer',
    // send all mails to a file by default. You have to set
    // 'useFileTransport' to false and configure a transport
    // for the mailer to send real emails.
    'useFileTransport' => false,
    //'class' => 'yii\swiftmailer\Mailer',
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.163.com',
        'username' => '[email protected]',
        'password' => 'n1234567890',
        'port' => '25',
        'encryption' => null,

    ],
    'messageConfig'=>[
        'charset'=>'UTF-8',
        'from'=>['[email protected]'=>'admin']
    ],
],

2 。在控制器里加入以下代碼進行測試

 

Yii::$app->mailer->compose()
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setSubject('Message subject')
    ->setTextBody('Plain text content')
    ->setHtmlBody('<b>傻蛋</b>')
    ->send();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章