{"id":23996,"date":"2024-11-21T13:07:58","date_gmt":"2024-11-21T05:07:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23996\/"},"modified":"2024-11-21T13:07:58","modified_gmt":"2024-11-21T05:07:58","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8swoole%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91%e7%9a%84swoole_smtp_auth%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23996\/","title":{"rendered":"\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_smtp_auth\u51fd\u6570"},"content":{"rendered":"<p>\u8fd1\u5e74\u6765\uff0c\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u65e5\u76ca\u666e\u53ca\uff0c\u5404\u79cd\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u4e5f\u8d8a\u6765\u8d8a\u5e38\u89c1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u7edf\u7684\u540c\u6b65i\/o\u65b9\u5f0f\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u73b0\u4ee3\u5e94\u7528\u5bf9\u9ad8\u6027\u80fd\u3001\u9ad8\u5e76\u53d1\u7684\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u6210\u4e3a\u4e86\u4e00\u79cd\u88ab\u5e7f\u6cdb\u5e94\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>Swoole\u662f\u4e00\u6b3e\u9762\u5411\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684PHP\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5f02\u6b65\u3001\u534f\u7a0b\u7b49\u7279\u6027\u3002_smtp_auth\u51fd\u6570\u662f\u5176\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5728\u4f7f\u7528SMTP\u534f\u8bae\u8fdb\u884c\u90ae\u4ef6\u53d1\u9001\u65f6\u5bf9\u90ae\u7bb1\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_smtp_auth\u51fd\u6570\u3002<\/p>\n<ol>\n<li>\u5f15\u5165Swoole\u534f\u7a0b\u5e93<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528Swoole\u534f\u7a0b\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5f15\u5165Swoole\u534f\u7a0b\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7composer\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre>composer require swoole\/ide-helper<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728PHP\u4ee3\u7801\u4e2d\u5f15\u5165\u4ee5\u4e0b\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<pre>use SwooleCoroutine;\nuse SwooleCoroutineSocket;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5b9e\u73b0swoole_smtp_auth\u51fd\u6570<\/li>\n<\/ol>\n<p>swoole_smtp_auth\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<pre>function swoole_smtp_auth(Socket $sock, string $username, string $password, string $hostname = '');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c$sock\u662f\u4e00\u4e2a\u5df2\u8fde\u63a5\u7684SMTP\u670d\u52a1\u5668Socket\uff0c$username\u548c$password\u5206\u522b\u4e3a\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u3002\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u662f\u9a8c\u8bc1\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\u5b9e\u73b0swoole_smtp_auth\u51fd\u6570\uff1a<\/p>\n<pre>function swoole_smtp_auth(Socket $sock, string $username, string $password, string $hostname = '')\n{\n    $cmd = \"AUTH LOGIN\n\";\n    $sock-&amp;gt;send($cmd);\n    $resp = $sock-&amp;gt;recv();\n\n    if (strpos($resp, '334') !== 0) {\n        throw new Exception(\"Failed to execute command: $cmd\");\n    }\n\n    $cmd = base64_encode($username) . \"\n\";\n    $sock-&amp;gt;send($cmd);\n    $resp = $sock-&amp;gt;recv();\n\n    if (strpos($resp, '334') !== 0) {\n        throw new Exception(\"Failed to execute command: $cmd\");\n    }\n\n    $cmd = base64_encode($password) . \"\n\";\n    $sock-&amp;gt;send($cmd);\n    $resp = $sock-&amp;gt;recv();\n\n    if (strpos($resp, '235') !== 0) {\n        throw new Exception(\"Authentication failed\");\n    }\n\n    return true;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u51fd\u6570\u901a\u8fc7SMTP\u534f\u8bae\u5411SMTP\u670d\u52a1\u5668\u53d1\u9001\u547d\u4ee4\u9a8c\u8bc1\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u3002\u5176\u4e2d\uff0c$cmd\u53d8\u91cf\u4e3aSMTP\u534f\u8bae\u4e2d\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\uff0c$resp\u53d8\u91cf\u4e3aSMTP\u670d\u52a1\u5668\u8fd4\u56de\u7684\u54cd\u5e94\u5b57\u7b26\u4e32\u3002\u8be5\u51fd\u6570\u5bf9\u54cd\u5e94\u5b57\u7b26\u4e32\u8fdb\u884c\u89e3\u6790\uff0c\u5982\u679c\u9a8c\u8bc1\u6210\u529f\uff0c\u5219\u8fd4\u56detrue\uff1b\u5426\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528Swoole\u534f\u7a0b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u8c03\u5ea6\u5f00\u542f\u591a\u4e2a\u534f\u7a0b\u540c\u65f6\u6267\u884c\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u591a\u4e2aSMTP\u670d\u52a1\u5668\u540c\u65f6\u9a8c\u8bc1\u591a\u4e2a\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u3002<\/p>\n<pre>$hostnames = ['smtp.gmail.com', 'smtp.qq.com', 'smtp.163.com'];\n$usernames = ['user1@gmail.com', 'user2@qq.com', 'user3@163.com'];\n$passwords = ['password1', 'password2', 'password3'];\n\n$socks = [];\nforeach ($hostnames as $host) {\n    $sock = new Socket(AF_INET, SOCK_STREAM, 0);\n\n    if (!$sock-&amp;gt;connect($host, 587)) {\n        throw new Exception(\"Failed to connect to $host:587\");\n    }\n\n    swoole_smtp_auth($sock, $username, $password, $host);\n\n    $socks[] = $sock;\n}\n\n$coros = [];\nfor ($i = 0; $i &lt;p&gt;\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u591a\u4e2aSMTP\u8fde\u63a5\uff0c\u5e76\u5206\u522b\u8fdb\u884c\u9a8c\u8bc1\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u534f\u7a0b\u8c03\u5ea6\u5f00\u542f\u591a\u4e2a\u534f\u7a0b\uff0c\u6bcf\u4e2a\u534f\u7a0b\u90fd\u9a8c\u8bc1\u4e00\u4e2a\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u3002\u901a\u8fc7\u4f7f\u7528\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u540c\u65f6\u8fdb\u884c\u591a\u4e2aSMTP\u8fde\u63a5\u548c\u591a\u4e2a\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u7684\u9a8c\u8bc1\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3002&lt;\/p&gt;&lt;ol start=\"4\"&gt;&lt;li&gt;\u603b\u7ed3&lt;\/li&gt;&lt;\/ol&gt;&lt;p&gt;\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_smtp_auth\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528Swoole\u534f\u7a0b\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u5f02\u6b65\u3001\u534f\u7a0b\u7b49\u7279\u6027\uff0c\u5e76\u5728\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u5e94\u7528\u573a\u666f\u4e2d\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u4f53\u9a8c\u3002&lt;\/p&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684_smtp_auth\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd1\u5e74\u6765\uff0c\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u65e5\u76ca\u666e\u53ca\uff0c\u5404\u79cd\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u4e5f\u8d8a\u6765\u8d8a\u5e38\u89c1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u7edf\u7684\u540c\u6b65i\/o\u65b9\u5f0f\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u73b0\u4ee3\u5e94\u7528\u5bf9\u9ad8\u6027\u80fd\u3001\u9ad8\u5e76\u53d1\u7684\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u6210\u4e3a\u4e86\u4e00\u79cd\u88ab\u5e7f\u6cdb\u5e94\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002 Swoole\u662f\u4e00\u6b3e\u9762\u5411\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684PHP\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5f02\u6b65\u3001\u534f\u7a0b\u7b49\u7279\u6027\u3002_smtp_auth\u51fd\u6570\u662f\u5176\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5728\u4f7f\u7528SMTP\u534f\u8bae\u8fdb\u884c\u90ae\u4ef6\u53d1\u9001\u65f6\u5bf9\u90ae\u7bb1\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_smtp_auth\u51fd\u6570\u3002 \u5f15\u5165Swoole\u534f\u7a0b\u5e93 \u5728\u4f7f\u7528Swoole\u534f\u7a0b\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5f15\u5165Swoole\u534f\u7a0b\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7composer\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a composer require swoole\/ide-helper \u767b\u5f55\u540e\u590d\u5236 \u5728PHP\u4ee3\u7801\u4e2d\u5f15\u5165\u4ee5\u4e0b\u547d\u540d\u7a7a\u95f4\uff1a use SwooleCoroutine; use SwooleCoroutineSocket; \u767b\u5f55\u540e\u590d\u5236 \u5b9e\u73b0swoole_smtp_auth\u51fd\u6570 swoole_smtp_auth\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a function swoole_smtp_auth(Socket $sock, string $username, string $password, string $hostname = &#8221;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$sock\u662f\u4e00\u4e2a\u5df2\u8fde\u63a5\u7684SMTP\u670d\u52a1\u5668Socket\uff0c$username\u548c$password\u5206\u522b\u4e3a\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u3002\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u662f\u9a8c\u8bc1\u90ae\u7bb1\u8d26\u53f7\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\u5b9e\u73b0swoole_smtp_auth\u51fd\u6570\uff1a function swoole_smtp_auth(Socket $sock, string $username, string $password, string $hostname = &#8221;) { $cmd = &#8220;AUTH LOGIN &#8220;; $sock-&amp;gt;send($cmd); $resp = $sock-&amp;gt;recv(); if (strpos($resp, &#8216;334&#8217;) !== [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23996","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23996"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23996\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}