ThinkPHP6ÖÐÔõÑù¾ÙÐÐÓʼþ·¢ËͲÙ×÷£¿
½üÄêÀ´£¬Óʼþ×÷ΪһÖÖ×îΪ³£¼ûµÄͨѶ·½·¨£¬±»ÆÕ±éÓ¦ÓÃÓÚÖÖÖÖÓ¦Óó¡¾°ÖС£ÔÚ²î±ðµÄwebÓ¦ÓÃÖУ¬Ò²¾³£ÐèҪͨ¹ý·¢ËÍÓʼþµÄ·½·¨À´¾ÙÐÐ֪ͨ¡¢ÑéÖ¤µÈ¹¦Ð§¡£¶øÔÚʹÓÃthinkphp6¿ò¼Ü¿ª·¢webÓ¦ÓõÄÀú³ÌÖУ¬ÎÒÃÇÐèÒªÏàʶÔõÑù¾ÙÐÐÓʼþ·¢ËͲÙ×÷£¬ÒÔ±ã¸üºÃµØʵÏÖÖÖÖÖ¹¦Ð§¡£ÏÂÃæÎÒÃǽ«ÏÈÈÝÔõÑùÔÚthinkphp6ÖоÙÐÐÓʼþ·¢ËͲÙ×÷¡£
ÉèÖÃÓʼþ
ÔÚThinkPHP6ÖÐÉèÖÃÓʼþºÜÊÇÀû±ã¡£Ö»ÐèÒªÔÚÏîÄ¿¸ùĿ¼ÏµÄ.envÎļþÖмÓÈëÈçÏ´úÂë¼´¿É£º
MAIL_DRIVER=smtp MAIL_HOST=smtp.qq.com MAIL_PORT=465 MAIL_USERNAME=xxx@qq.com MAIL_PASSWORD=xxx MAIL_ENCRYPTION=ssl MAIL_FROM_ADDRESS=xxx@qq.com MAIL_FROM_NAME=xxx
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÆäÖÐMAIL_DRIVERÖ¸¶¨ÁËʹÓõÄÇý¶¯£¬ÕâÀïÎÒÃÇʹÓÃsmtp£»MAIL_HOSTÖ¸¶¨ÁËÓʼþЧÀÍÆ÷µÄµØµã£¬ÕâÀïÎÒÃÇʹÓõÄÊÇQQÆóÒµÓÊÏäµÄµØµã£»MAIL_PORTÖ¸¶¨Á˶˿ںţ¬ÕâÀïÎÒÃÇʹÓÃ465¶Ë¿Ú£»MAIL_USERNAMEÖ¸¶¨ÁË·¢¼þÈËÓÊÏäµÄÕ˺ţ»MAIL_PASSWORDÖ¸¶¨ÁË·¢¼þÈËÓÊÏäµÄÃÜÂ룻MAIL_ENCRYPTIONÖ¸¶¨ÁËÓʼþ¼ÓÃÜ·½·¨£¬ÕâÀïÎÒÃÇʹÓÃÁËSSL¼ÓÃÜ·½·¨£»MAIL_FROM_ADDRESSÖ¸¶¨ÁË·¢¼þÈËÓÊÏäµØµã£»MAIL_FROM_NAMEÖ¸¶¨ÁË·¢¼þÈËÐÕÃû¡£
·¢ËÍÓʼþ
ÉèÖúÃÓʼþÖ®ºó£¬ÎÒÃǾͿÉÒÔ×îÏÈ·¢ËÍÓʼþÁË¡£Ê¹ÓÃThinkPHP6ÌṩµÄÓʼþÀàMail£¬¿ÉÒÔºÜÊÇÀû±ãµØʵÏÖÓʼþ·¢ËͲÙ×÷¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÓʼþ·¢ËÍʾÀý£º
use thinkacadeMail; Mail::to('xxx@qq.com')->subject('²âÊÔÓʼþ')->html('<h1>ÕâÊÇÒ»·â²âÊÔÓʼþ</h1>');
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËMailÀàµÄto¡¢subject¡¢htmlÒªÁì¡£ÆäÖÐtoÒªÁìÖ¸¶¨ÁËÊÕ¼þÈËÓÊÏäµØµã£»subjectÒªÁìÖ¸¶¨ÁËÓʼþÖ÷Ì⣻htmlÒªÁìÖ¸¶¨ÁËÓʼþÕýÎÄÄÚÈÝ£¬²¢¿ÉÒÔʹÓÃHTMLÃûÌá£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
×¢ÖØ£ºÔÚ¾ÙÐÐÓʼþ·¢ËͲÙ×÷֮ǰ£¬ÎÒÃÇÐèҪȷ±£ÒѾװÖÃÁËPHPMailerÀà¿â¡£Äã¿ÉÒÔʹÓÃComposer¾ÙÐÐ×°Ö㬿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
composer require phpmailer/phpmailer
µÇ¼ºó¸´ÖÆ
×Ô½ç˵ÓʼþÉèÖÃ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇͨ¹ýÔÚ.envÎļþÖоÙÐÐÉèÖÃÀ´ÊµÏÖÓʼþ·¢Ë͹¦Ð§¡£µ«ÓÐʱ¼ä£¬ÎÒÃÇ¿ÉÄÜÐèÒªÔÚ´úÂëÖÐƾ֤³¡¾°¶¯Ì¬¸ü¸ÄÓʼþÉèÖᣴËʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃConfigÀàÀ´ÊµÏÖ£¬¿ÉÒÔͨ¹ýÒÔÏ´úÂë¾ÙÐÐ×Ô½ç˵ÉèÖãº
use thinkacadeConfig; use thinkacadeMail; $config = [ 'smtp_host' => 'smtp.qq.com', 'smtp_port' => '465', 'smtp_user' => 'xxx@qq.com', 'smtp_pass' => 'xxx', 'smtp_secure' => 'ssl' ]; Config::set('mail', $config); Mail::to('xxx@qq.com')->subject('²âÊÔÓʼþ')->html('<h1>ÕâÊÇÒ»·â²âÊÔÓʼþ</h1>');
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃConfigÀàµÄsetÒªÁìÀ´¾ÙÐÐÓʼþÉèÖÃÉèÖã¬È»ºóÔÙʹÓÃMailÀàÀ´¾ÙÐÐÓʼþ·¢ËͲÙ×÷¡£ÕâÑù£¬ÎÒÃǾͿÉÒÔƾ֤×Ô¼ºµÄÐèÇó¾ÙÐÐÓʼþÉèÖÃÁË¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔÚThinkPHP6ÖоÙÐÐÓʼþ·¢ËͲÙ×÷µÄÏà¹ØÄÚÈÝ¡£Ö»Ðè¼òÆÓµÄÉèÖÃÒ»ÏÂÓʼþ²ÎÊý£¬È»ºóŲÓÃMailÀ༴¿É¿ìËÙµÄʵÏÖÓʼþ·¢Ë͹¦Ð§¡£ÔÚÏÖʵʹÓÃÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇóÀ´¾ÙÐÐ×Ô½ç˵ÉèÖã¬ÒÔÈ·±£Óʼþ·¢Ë͹¦Ð§¿ÉÒÔÔ½·¢ÎÞа¡¢¸ßЧµØÍê³É¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐÐÓʼþ·¢ËͲÙ×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡