{"slug":"email-posts-sub","title":"手搓一个电子邮件订阅器！","description":"你可曾想过手搓一个基于邮件形式的文章订阅器？读者只需告诉系统邮箱，之后的文章更新都会发一封邮件来通知他们！","coverImage":"https://b.acofork.com/img/email-posts-sub-email.webp","coverWidth":1200,"coverHeight":900,"date":"2026-04-03","tags":["服务器"],"pinned":false,"wordCount":776,"html":"<h2 id=\"视频\">视频</h2><p><a href=\"https://www.bilibili.com/video/BV1hpDKBbES9/\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.bilibili.com/video/BV1hpDKBbES9/</a></p>\n<h2 id=\"正式开始\">正式开始</h2><p>众所周知，目前如果你想订阅本博客，可以采用以下几种方式</p>\n<ul>\n<li><strong>瞪眼法</strong> 在多次浏览中通过大脑的逻辑中枢来判断有哪些新文章，或有哪些文章更新了</li>\n<li><strong>简易瞪眼法</strong> 在多次浏览中，若文章状态变化，右下角的小铃铛则会显示相应通知，点击后甚至可以看到高级的DIFF\n  <img src=\"https://b.acofork.com/img/email-posts-sub-email-posts-sub.webp\" alt=\"\" width=\"395\" height=\"219\" loading=\"eager\" decoding=\"async\" /></li>\n<li><strong>RSS</strong> 通过RSS订阅器订阅本博客的RSS XML文件，由第三方告知文章更新状态\n  <img src=\"https://b.acofork.com/img/email-posts-sub-email-posts-sub-1.webp\" alt=\"\" width=\"1045\" height=\"1176\" loading=\"lazy\" decoding=\"async\" /></li>\n<li><strong>加入群组</strong> 通过置顶文章加入群组，群内会有Bot在文章更新时广播一条消息\n  <img src=\"https://b.acofork.com/img/email-posts-sub-email-posts-sub-2.webp\" alt=\"\" width=\"496\" height=\"345\" loading=\"lazy\" decoding=\"async\" /></li>\n<li><strong>催更法</strong> 想办法要到我的联系方式，催更，大概率我会在你要求的新文章发布后踹你一脚</li>\n</ul>\n<p>emm，看似很多，的确很多，但是由于多多益善，可得还不够多，所以我们准备再添加一个古法订阅</p>\n<p>那就是 <strong>电子邮件通知</strong> </p>\n<p>实现它在现在其实非常简单，由于我们已经有了一个论坛，可以直接在用户个人中心添加一个选项卡，勾选后即可接收后续的文章通知</p>\n<p>我们甚至无需担心垃圾邮件，因为在注册论坛时已经有了相对严格的验证</p>\n<p>那么接下来就是架构的设置，正如上文提到的，我们有一个提醒文章更新的Bot</p>\n<p>实际上，最终的电子邮件发送的东西和这个东西差不多</p>\n<p>也就是说，我们只需要让Bot在发送消息的同时，将要发送的内容推送给我们</p>\n<p><em>至于Bot是如何做到文章更新推送消息的，请参见 <a href=\"/posts/github-webhook/\">这里</a></em></p>\n<p>那么不难想象，我们只需要在后端创建一个WebHook端点，接受Bot发送的WebHook消息，然后将正文作为邮件正文发送给订阅后的用户</p>\n<p>最终，架构如下</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart TD\n    A[Git Push 特殊事件&lt;br/&gt;posts: / update:] --&gt; B[GitHub WebHook]\n    B --&gt; C[Bot WebHook 接收端点]\n\n    C --&gt; D[延迟队列 / 定时器]\n\n    D --&gt; E[Bot 向群内推送消息]\n    D --&gt; F[二次 WebHook 请求]\n\n    F --&gt; G[论坛后端 WebHook 端点]\n\n    G --&gt; H[封装邮件正文]\n    H --&gt; I[异步邮件发送服务]\n    I --&gt; J[所有订阅用户]</code></pre></div><p>ok！思路清晰了，实践就简单了</p>\n<p>首先前往Bot插件，将 <code>blog_post.py</code> 插件添加一个二次WebHook的功能</p>\n<p>再为论坛后端添加一个接受WebHook的端点，并绑定发邮事件。顺便再加一个API用于控制用户是否为 <strong>订阅者</strong> </p>\n<p>最终在前端对接后端API，以及添加新UI控件支持用户在论坛的个人信息页配置是否要接受新文章推送</p>\n<p>测试！</p>\n<p><img src=\"https://b.acofork.com/img/email-posts-sub-c9bb51ef1f6e3c994f76c55ae7854fb5.webp\" alt=\"\" width=\"1247\" height=\"357\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/email-posts-sub-email-posts-sub-3.webp\" alt=\"\" width=\"1182\" height=\"309\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/email-posts-sub-a570d8792d2823cabbe41834d0a64405.webp\" alt=\"\" width=\"1572\" height=\"1179\" loading=\"lazy\" decoding=\"async\" /></p>\n","toc":[{"id":"视频","text":"视频","depth":2},{"id":"正式开始","text":"正式开始","depth":2}]}