1. Configure in config file main-local.php components=>[]
' Mailer ' = [ ' class ' = ' Yii\swiftmailer\mailer ', ' usefiletransport ' =>false,//This sentence must have, false send mail, True just generate mail in the runtime folder, do not send mail ' transport ' = [ ' class ' = ' Swift_smtptransport ', ' host ' = ' Smtp.163.com ', //each mailbox has a different host configuration ' username ' = ' [email protected] ', ' password ' = ' ******* ', ' Port ' = ' + ', ' encryption ' = ' TLS ', ' messageconfig ' =>[' charset ' = ' UTF-8 ', ' from ' =>[' [email protected] ' = ' admin '],] ,
2, controller controllers in the code:
<?php$mail= Yii:: $app->mailer->compose (); $mail->setto (' *********** @qq. com '); $mail->setsubject ("Mail Test"); $mail->settextbody (' Zheshisha '); Publish plaintext Text $mail->sethtmlbody ("<br> ask me me I am Me"); Publish text if ($mail->send ()) echo "Success" that can be tagged with html; else echo "Failse"; Die ();?>
OK, so you can send mail.
If you need to load the template$mail= Yii::$app->mailer->compose();
Modified into $mail= Yii::$app->mailer->compose(‘xiaoma‘,[‘aa‘=>222]);
Note: AA is a parameter that you want to pass inside the tiandi.php.
Message template tiandi.php inside the code:
<?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>
To load the template's message code:
$mail = Yii:: $app->mailer->compose (' Xiaoma ', [' AA ' =>222]); $mail->setto (' [email protected] '); $mail->setsubject ("Mail Test"); $mail->settextbody (' Zheshisha '); if ($mail->send ()) echo "Success"; else echo "Failse"; Die ();
YII2 Mail Sending