YII2 Mail Sending

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.