借助Github Action实现通过 HTTP 请求触发邮件通知

Github仓库:Send Mail

Send Mail

通过 HTTP 请求触发 Github Actions 自动发送邮件的 workflow。可用于各种通知场景,如 IP 变更通知、任务完成提醒等。只要可以发起 HTTP 请求的地方,都可以使用此功能发送邮件通知。

如何使用

  • fork 本仓库到你的 Github 账号下。

  • 在你的仓库中,进入 Settings -> Secrets and variables -> Actions,添加以下 Secrets:

    • MAIL_USERNAME:SMTP 服务器用户名(通常是你的邮箱地址)

    • MAIL_PASSWORD:SMTP 服务器密码(通常是授权码)

      名称必填
      MAIL_USERNAMESMTP 服务器用户名(通常是你的邮箱地址)
      MAIL_PASSWORDSMTP 服务器密码(通常是授权码)
  • 发送http请求触发邮件发送

    • 请求格式如下:

      curl --request POST \
       --url https://api.github.com/repos/<owner>/<repo>/dispatches \
       --header 'Accept: application/vnd.github+json' \
       --header 'Authorization: <YOUR_GITHUB_TOKEN>' \
       --header 'Content-Type: application/json' \
       --data '{
                 "event_type": "send_mail",
                 "client_payload": {
                     "to": "example@163.com",
                     "subject": "📧 外部触发邮件通知",
                     "template": "ip_update_template.html",
                     "variables": {
                       "xx": "xx",
                       "ipv4Result": "成功",
                       "ipv4Domains": "example.com,api.example.com",
                       "ipv6Addr": "fe80::abcd",
                       "ipv6Result": "未改变",
                       "ipv6Domains": "v6.example.com",
                       "message": "来自Webhook的提醒111"
                     }
                 }
               }'
      

      其中:

      • <owner>:你的 Github 用户名或组织名

      • <repo>:你的仓库名

      • <YOUR_GITHUB_TOKEN>:你的 Github 个人访问令牌(需要有 repo 权限)

        变量名说明
        <owner>你的 Github 用户名或组织名
        <repo>你的仓库名
        <YOUR_GITHUB_TOKEN>你的 Github 个人访问令牌(需要有 repo 权限)
    • 示例请求:

      curl --request POST \
      --url https://api.github.com/repos/vains-Sofia/send-mail/dispatches \
      --header 'Accept: application/vnd.github+json' \
      --header 'Authorization: <YOUR_GITHUB_TOKEN>' \
      --header 'Content-Type: application/json' \
      --data '{
                "event_type": "send_mail",
                "client_payload": {
                  "to": "example@163.com",
                  "subject": "📧 外部触发邮件通知",
                  "template": "ip_update_template.html",
                  "variables": {
                    "ipv4Addr": "192.168.0.2",
                    "ipv4Result": "成功",
                    "ipv4Domains": "example.com,api.example.com",
                    "ipv6Addr": "fe80::abcd",
                    "ipv6Result": "未改变",
                    "ipv6Domains": "v6.example.com",
                    "message": "来自Webhook的提醒111"
                    }
                  }
              }'
      
    • 入参说明:

      • event_type:固定值,必须为 send_mail
      • client_payload:包含邮件相关信息的对象,包含以下字段:
        • to:收件人邮箱地址

        • subject:邮件主题

        • template:邮件模板文件名(需预先在仓库中准备好对应的 HTML 模板文件,位于 mail-templates/ 目录下)

        • variables:模板变量的键值对,根据模板中的占位符进行替换

          变量名说明
          event_type固定值,必须为 send_mail
          client_payload包含邮件相关信息的对象
          to收件人邮箱地址
          subject邮件主题
          template邮件模板文件名(位于 mail-templates/ 目录下)
          variables模板变量的键值对,根据模板中的占位符进行替

邮件模板示例

模板示例效果

在这里插入图片描述

注意事项

  • 确保用户名和密码(授权码)有效。
  • 确保你的 Github 个人访问令牌具有足够的权限来触发仓库的 repository_dispatch 事件。
  • 邮件模板文件需预先在仓库中准备好,位于 mail-templates/ 目录下。

参考资料

欢迎大家提供反馈和建议!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天玺-vains

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值