大部分框架都有自帶的email郵件發送類,yii的郵件發送也很簡單,代碼如下:
1 修改配置文件(config/web.php) 2 3 'mailer' => [ 4 'class' => 'yii\swiftmailer\Mailer', 5 'useFileTransport' => false, 6 'transport' => [ 7 'class' => 'Swift_SmtpTransport', 8 'host' => 'smtp.163.com', //每種郵箱的host配置不一樣 9 'username' => '[email protected]', //發件人郵箱 10 'password' => 'xgslagfpomsxuseq', //授權碼 11 'port' => '25', 12 'encryption' => 'tls', 13 ], 14 'messageConfig'=>[ 15 'charset'=>'UTF-8', 16 'from'=>['[email protected]'=>'nickname'] //發件人昵稱 17 ], 18 ], 19 20 21 控制器發送郵件(自定義消息) 22 23 24 $mail = Yii::$app->mailer->compose(); 25 $mail->setTo('****@qq.com'); //接收人郵箱 26 $mail->setSubject("test"); //郵件標題 27 $mail->setHtmlBody("發送內容發送內容發送內容"); //發送內容(可寫HTML代碼) 28 if ($mail->send()){ 29 echo "成功"; 30 }else{ 31 echo "失敗"; 32 } 33 34 35 如果發送頁面 36 37 38 $mail = Yii::$app->mailer->compose("email"); //在mail文件夾下創建email頁面文件,在文件內編輯內容 39 $mail->setTo('****@qq.com'); //接收人郵箱 40 $mail->setSubject("test"); //郵件標題 41 if ($mail->send()){ 42 echo "成功"; 43 }else{ 44 echo "失敗"; 45 }
來源: http://www.cnblogs.com/fwqblogs/p/6564463.html