{"id":401,"date":"2022-05-14T22:55:28","date_gmt":"2022-05-14T14:55:28","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=401"},"modified":"2022-05-14T22:55:28","modified_gmt":"2022-05-14T14:55:28","slug":"yii2-swift-mailer-%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6%e4%b8%8d%e6%88%90%e5%8a%9f%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2022\/05\/14\/yii2-swift-mailer-%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6%e4%b8%8d%e6%88%90%e5%8a%9f%e7%9a%84%e9%97%ae%e9%a2%98\/","title":{"rendered":"yii2 swift mailer \u53d1\u9001\u90ae\u4ef6\u4e0d\u6210\u529f\u7684\u95ee\u9898"},"content":{"rendered":"\n<p>\u4eca\u5929\u8c03\u8bd5yii2\u81ea\u5e26\u7684swift mailer\u53d1\u90ae\u4ef6\uff0c\u5f00\u59cb\u8c03\u8bd5\u65f6\uff0c\u53d1\u9001\u7528\u7684send()\u65b9\u6cd5\u59cb\u7ec8\u8fd4\u56detrue\uff0c\u4f46\u662f\u5c31\u662f\u6ca1\u6709\u6536\u5230\u90ae\u4ef6\uff0c\u5f88\u662f\u7eb3\u95f7\uff0c\u4e8e\u662f\u5f00\u59cb\u4e86\u534a\u4e2a\u665a\u4e0a\u7684\u8c03\u8bd5\u4e4b\u65c5\uff0c\u6211\u628a\u8c03\u8bd5\u8fc7\u7a0b\u53d1\u51fa\u6765\uff0c\u5e0c\u671b\u540e\u9762\u7684\u5c0f\u4f19\u4f34\u80fd\u5c11\u8d70\u4e00\u4e9b\u5f2f\u8def\u3002<br>\u9996\u5148\uff0c\u6839\u636e\u70ed\u5fc3\u7f51\u53cb\u7684\u6587\u7ae0\uff0c\u914d\u7f6e\u90ae\u7bb1\u7684\u57fa\u7840\u4fe1\u606f\uff0c\u6211\u7528\u7684base\u9879\u76ee\uff0c\u6240\u4ee5\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u662fweb.php\uff0c\u914d\u7f6e\u5185\u5bb9\u4e3a\uff1a<br>&nbsp; &nbsp; &nbsp; &nbsp; \u2018mailer\u2019 =&gt; [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018class\u2019 =&gt; \u2018yii\\swiftmailer\\Mailer\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018transport\u2019 =&gt; [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018class\u2019 =&gt; \u2018Swift_SmtpTransport\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018host\u2019 =&gt; \u2018smtp.163.com\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018username\u2019 =&gt; \u2018xxx@163.com\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018password\u2019 =&gt; \u2018xxx\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018port\u2019 =&gt; \u201925\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018encryption\u2019 =&gt; \u2018tls\u2019,\/\/ &nbsp; &nbsp;tls | ssl<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018messageConfig\u2019=&gt;[<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018charset\u2019=&gt;\u2019UTF-8\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018from\u2019=&gt;[\u2018xxx@163.com\u2019=&gt;\u2019admin\u2019]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018useFileTransport\u2019 =&gt; false,<br>&nbsp; &nbsp; &nbsp; &nbsp; ],<br>\u7136\u540e\u5f00\u59cb\u5199\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u7528\u7684\u63a7\u5236\u5668\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<br>&nbsp; &nbsp; public function actionMailer()<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail= Yii::$app-&gt;mailer-&gt;compose();<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setTo(\u201810000@qq.com\u2019);<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setSubject(\u201cTest title\u201d);<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setTextBody(\u2018Test content\u2019);<br>&nbsp; &nbsp; &nbsp; &nbsp; \/\/$mail-&gt;setHtmlBody(\u201cTest HTML\u201d);<br>&nbsp; &nbsp; &nbsp; &nbsp; var_dump($mail-&gt;send());<br>&nbsp; &nbsp; }<br>\u597d\u4e86\uff0c\u4fe1\u5fc3\u6ee1\u6ee1\u7684\u5f00\u59cb\u6d4b\u8bd5\u4e86\uff0c\u6267\u884c\u94fe\u63a5\uff0c\u6ee1\u5fc3\u6b22\u559c\u7684\u53bb\u67e5\u770bQQ\u90ae\u7bb1\u4e86\uff0c\u4f46\u662f\u5de6\u7b49\u53f3\u7b49\uff0c\u524d\u7b49\u540e\u7b49\uff0c\u4e2b\u7684\u5c31\u662f\u6ca1\u6709\uff0c\u91cd\u65b0\u53d1\u9001\uff0c\u8fd8\u662f\u6ca1\u6709\uff0c\u4f46\u662fsend()\u7684\u8fd4\u56de\u503c\u660e\u660e\u5c31\u662ftrue\uff01<br>&nbsp;\u4e8e\u662f\u5f00\u59cb\u6392\u9519\uff0c\u603b\u7ed3\u4e86\u4e00\u4e0b\uff0c\u51fa\u9519\u7684\u53ef\u80fd\u4f4d\u7f6e\u5728\u4ee5\u4e0b\u51e0\u70b9\uff1a<br>&nbsp;\u4e0d\u540c\u90ae\u7bb1\u7684host\u662f\u4e0d\u540c\u7684<br>\u5982\u679c\u662f163\u90ae\u7bb1\uff0cpassword\u662f\u6388\u6743\u7801<br>userFileTransport\u8981\u8bbe\u7f6e\u6210false\uff0c\u5426\u5219\u53ea\u4f1a\u5728runtime\u4e0b\u751f\u6210\u7f13\u5b58\u6587\u4ef6\uff0c\u4e0d\u4f1a\u771f\u6b63\u53d1\u9001<br>\u4e00\u4e00\u6392\u67e5\uff0c\u786e\u4fe1\u6ca1\u6709\u9519\u8bef\uff0c\u592a\u5947\u602a\u4e86\uff0c\u4e8e\u662f\u6211\u51b3\u5b9a\u76f4\u63a5\u8c03\u7528swiftmailer\u8bd5\u4e00\u4e0b\uff0c\u4e8e\u662f\u65b0\u5efa\u4e86\u4e0b\u9762\u7684\u63a7\u5236\u5668\uff1a<br>&nbsp; &nbsp; public function actionMailer2()<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer = new \\yii\\swiftmailer\\Mailer();<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer-&gt;transport=[<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018class\u2019 =&gt; \u2018Swift_SmtpTransport\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018host\u2019 =&gt; \u2018smtp.163.com\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018username\u2019 =&gt; \u2018xxxx@163.com\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018password\u2019 =&gt; \u2018xxxx\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018port\u2019 =&gt; \u201925\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018encryption\u2019 =&gt; \u2018tls\u2019,\/\/ &nbsp; &nbsp;tls | ssl<br>&nbsp; &nbsp; &nbsp; &nbsp; ];<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer-&gt;messageConfig=[<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018charset\u2019=&gt;\u2019UTF-8\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018from\u2019=&gt;[\u2018xxxx@163.com\u2019=&gt;\u2019admin\u2019]<br>&nbsp; &nbsp; &nbsp; &nbsp; ];<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer-&gt;useFileTransport = false;<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail= $mailer-&gt;compose();<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setTo(\u201810000@qq.com\u2019);<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setSubject(\u201cTest title\u201d);<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setTextBody(\u2018Test content\u2019);<br>&nbsp; &nbsp; &nbsp; &nbsp; var_dump($mail-&gt;send());<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>\u54ce\u54df\u6211\u53bb\uff01\u53d1\u9001\u6210\u529f\u4e86\uff01\u8fd9\u662f\u4e3a\u5565\u5462\uff1f\u9996\u5148\u8bc1\u660e\u8fd9\u4e2a\u7c7b\u786e\u662f\u662f\u53ef\u4ee5\u53d1\u9001\u90ae\u4ef6\u7684\u4e86\uff0c\u90a3\u4e48\u4e0b\u4e00\u6b65\u8981\u7ec6\u7ec6\u7684\u5206\u6790\u8fd9\u4e24\u6bb5\u4ee3\u7801\u533a\u522b\u5728\u4f55\u5904\u4e86\u3002<br>\u6211\u5206\u522b\u6253\u5370\u4e86\u4e24\u4e2a\u7c7b\u6784\u9020\u5b8c\u6210\u540e\u7684\u7ed3\u679c\uff1a<br>&nbsp; &nbsp; public function actionMailer()<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; echo \u2018&lt;pre&gt;\u2019;<br>&nbsp; &nbsp; &nbsp; &nbsp; var_dump(Yii::$app-&gt;mailer);<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>\u548c<\/p>\n\n\n\n<p>&nbsp; &nbsp; public function actionMailer2()<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer = new \\yii\\swiftmailer\\Mailer();<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer-&gt;transport=[<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018class\u2019 =&gt; \u2018Swift_SmtpTransport\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018host\u2019 =&gt; \u2018smtp.163.com\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018username\u2019 =&gt; \u2018xxxx@163.com\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018password\u2019 =&gt; \u2018xxxx\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018port\u2019 =&gt; \u201925\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018encryption\u2019 =&gt; \u2018tls\u2019,\/\/ &nbsp; &nbsp;tls | ssl<br>&nbsp; &nbsp; &nbsp; &nbsp; ];<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer-&gt;messageConfig=[<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018charset\u2019=&gt;\u2019UTF-8\u2019,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018from\u2019=&gt;[\u2018xxxx@163.com\u2019=&gt;\u2019admin\u2019]<br>&nbsp; &nbsp; &nbsp; &nbsp; ];<br>&nbsp; &nbsp; &nbsp; &nbsp; $mailer-&gt;useFileTransport = false;<br>&nbsp; &nbsp; &nbsp; &nbsp; echo \u2018&lt;pre&gt;\u2019;<br>&nbsp; &nbsp; &nbsp; &nbsp; var_dump($mailer);<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>\u5728\u957f\u957f\u7684\u5185\u5bb9\u4e2d\u6162\u6162\u7684\u5bf9\u6bd4\uff0c\u7ec8\u4e8e\u53d1\u73b0\u4e86\u7aef\u502a\uff0c<br>\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u4e2d\uff1a[\u201cuseFileTransport\u201d]=&gt;bool(true)<br>\u7b2c\u4e8c\u4e2a\u65b9\u6cd5\u4e2d\uff1a[\u201cuseFileTransport\u201d]=&gt;bool(false)<br>\u4e0a\u9762\u4e5f\u8bf4\u8fc7\u4e86\uff0cuseFileTransport\u5fc5\u987b\u8bbe\u7f6e\u6210false\u624d\u80fd\u53d1\u9001\u6210\u529f\uff0c\u90a3\u95ee\u9898\u603b\u7b97\u662f\u627e\u5230\u4e86\uff0c\u90a3\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u95ee\u9898\u5462\uff1f\u6211\u4e0a\u9762\u660e\u660e\u662f\u8bbe\u7f6e\u8fc7\u7684\uff0c\u4e3a\u4ec0\u4e48\u6ca1\u751f\u6548\uff1f\u96be\u9053\u662f\u6d4b\u8bd5\u73af\u5883\u7684\u539f\u56e0\u5417\uff1f<br>\u6211\u6d4b\u8bd5\u4f7f\u7528\u7684url\u662fhttp:\/\/goonwin.com\/index-test.php?r=liyang\/mailer\uff0c\u6ce8\u610f\u90a3\u4e2aindex-test.php\uff0c\u5c31\u662f\u4ed6\u7684\u539f\u56e0\u4e86\uff0c\u4e8e\u662f\u6211\u628aindex-test.php\u6539\u6210index.php\u7acb\u9a6c\u53d1\u9001\u6210\u529f\u4e86\uff0c\u4e2b\u7684\uff0c\u8fd9\u4e48\u9690\u853d\u4e00\u4e2a\u5751\u554a\uff01\uff01\uff01<br>\u4f46\u662f\u6211\u8fd8\u662f\u5e0c\u671b\u6d4b\u8bd5\u73af\u5883\u53ef\u4ee5\u53d1\u9001\u90ae\u4ef6\u7684\uff0c\u4e8e\u662f\uff0c\u7a0d\u5fae\u6539\u52a8\u4e86\u4e00\u4e0b\u8c03\u7528\u7684\u65b9\u6cd5\uff1a<br>&nbsp; &nbsp; public function actionMailer()<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail = Yii::$app-&gt;mailer;<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;useFileTransport = false;<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail= $mail-&gt;compose();<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setTo(\u201810000@qq.com\u2019);<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setSubject(\u201cTest title\u201d);<br>&nbsp; &nbsp; &nbsp; &nbsp; $mail-&gt;setTextBody(\u2018Test content\u2019);<br>&nbsp; &nbsp; &nbsp; &nbsp; var_dump($mail-&gt;send());<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>Oh,\u7ec8\u4e8e\u53ef\u4ee5\u53d1\u9001\u4e86\uff0c\u770b\u5230\u8bba\u575b\u4e2d\u6709\u4e9b\u670b\u53cb\u4e5f\u5728\u95ee\u540c\u6837\u7684\u95ee\u9898\uff0c\u4f30\u8ba1\u73b0\u5728\u4f60\u5df2\u7ecf\u53ef\u4ee5\u89e3\u51b3\u4e86\u3002\u6d0b\u6d0b\u6d12\u6d12\u5199\u4e86\u8fd9\u4e48\u591a\uff0c\u5176\u5b9e\u5c31\u4e00\u4e2a\u76ee\u7684\uff0c\u5e0c\u671b\u80fd\u7ed9\u65b0\u624b\u4e00\u70b9\u8c03\u8bd5\u7684\u7ecf\u9a8c\u5427\uff0c\u8001\u624b\u4f60\u5f53\u7136\u4e50\u5475\u4e50\u5475\u5c31\u5f97\u4e86\u3002<br>\u6d6a\u8d39\u4e00\u665a\u4e0a\u9752\u6625\uff0c\u7761\u89c9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u8c03\u8bd5yii2\u81ea\u5e26\u7684swift mailer\u53d1\u90ae\u4ef6\uff0c\u5f00\u59cb\u8c03\u8bd5\u65f6\uff0c\u53d1\u9001\u7528\u7684send()\u65b9\u6cd5\u59cb\u7ec8\u8fd4\u56detrue\uff0c\u4f46\u662f\u5c31\u662f\u6ca1\u6709\u6536\u5230\u90ae\u4ef6\uff0c\u5f88\u662f\u7eb3\u95f7\uff0c\u4e8e\u662f\u5f00\u59cb\u4e86\u534a\u4e2a\u665a\u4e0a\u7684\u8c03\u8bd5\u4e4b\u65c5\uff0c\u6211\u628a\u8c03\u8bd5\u8fc7\u7a0b\u53d1\u51fa\u6765\uff0c\u5e0c\u671b\u540e\u9762\u7684\u5c0f\u4f19\u4f34\u80fd\u5c11\u8d70\u4e00\u4e9b\u5f2f\u8def\u3002\u9996\u5148\uff0c\u6839\u636e\u70ed\u5fc3\u7f51\u53cb\u7684\u6587\u7ae0\uff0c\u914d\u7f6e\u90ae\u7bb1\u7684\u57fa\u7840\u4fe1\u606f\uff0c\u6211\u7528\u7684base\u9879\u76ee\uff0c\u6240\u4ee5\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u662fweb.php\uff0c\u914d\u7f6e\u5185\u5bb9\u4e3a\uff1a&nbsp; &nbsp; &nbsp; &nbsp; \u2018mailer\u2019 =&gt; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018class\u2019 =&gt; \u2018yii\\swiftmailer\\Mailer\u2019,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018transport\u2019 =&gt; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018class\u2019 =&gt; \u2018Swift_SmtpTransport\u2019,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2018host\u2019 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,27],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-yii2","category-27"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}