Yii框架怎麼發送郵件?
先使用Composer安裝「yiisoft/yii2-swiftmailer」擴充;
php composer require --prefer-dist yiisoft/yii2-swiftmailer
然後設定mailer中參數;
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', // 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, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', 'username' => '***@163.com', 'password' => '******', 'port' => '25', 'encryption' => 'tls', ], 'messageConfig'=>[ 'charset'=>'UTF-8', 'from'=>['***@163.com'=>'白狼栈'] ], ], ], ];
接著呼叫該程式碼「Yii:: $app->mailer」取得到mailer物件;
$mail= Yii::$app->mailer->compose();
最後設定郵件資訊即可。
$mail= Yii::$app->mailer->compose(); $mail->setTo('***@qq.com'); //要发送给那个人的邮箱 $mail->setSubject("邮件主题"); //邮件主题 $mail->setTextBody('测试text'); //发布纯文字文本 $mail->setHtmlBody("测试html text"); //发送的消息内容 var_dump($mail->send());
推薦教學:《PHP》
以上是Yii框架怎麼發送郵件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!