项目介绍

有时,我们会碰到这几种情况:

  • 手机卡槽插满了,多的卡只能插在备用机里

  • 备用机不方便带在身上

  • 手机单卡,但是有很多卡和备用机

  • 偶尔要接收备用机的短信

于是,在某一天,我发现了“SMSForwarder短信转发器”这个软件(单击前往github)

其内置了丰富的短信转发功能和渠道,但是各种渠道要不是申请困难,要不就是要实名认证,非常不方便。

然而,其WebHook转发功能引起了我的注意。可以向指定的URL发起POST/GET等请求

于是,我作为python的初学者,也萌生了“用python编写一个api,转发手机邮件”的想法。

实践开始:

准备环境

需要准备的开发环境:Windows/Linux电脑, Python >= 3.8,自己的域名,cloudflare,resend账号,魔法上网工具,以及手和脑子

Python 环境

需要以下软件包:

# flask resend dotenv

# pip 安装
pip install resend dotenv flask

创建项目

创建一个 Python 项目,导入这几个包,然后开始写代码

解释一下创建文件及其含义:

 
app.py (自己替换 xxx 的内容)
import flask
from flask import Flask, jsonify, request
import os
import resend
from dotenv import load_dotenv
import htmlFormat

load_dotenv(".env").to_bytes()
ENV_API_KEY = os.getenv("APIKEY")


def send(f, c):
    resend.api_key = ENV_API_KEY
    params: resend.Emails.SendParams = {
      "from": "xxx<xxx@xxx.xxx>",
      "to": ["xxx@xxx.xxx"],
      "subject": "xxxxxxx",
      "html": f"{htmlFormat.Format(f = f, c = c)}"
    }
    email = resend.Emails.send(params)

app = Flask(__name__)
@app.route("/")
def nothing() -> object:
    return "<h1>404 Not Found</h1><hr>nginx"

@app.route("/api/send")
def getContent():
    smsFrom = request.args.get("from")
    smsCont = request.args.get("content")

    if smsFrom and smsCont:
        send(f= smsFrom, c = smsCont)
        return jsonify({"Satus":200, "Message":"Send Email Successfully"})
    else:
        return jsonify({"Status":500, "Message": "Send Failed. Illegal Request Method"})

if __name__ == "__main__":
    app.run(port=30010)
.env (自己替换)
APIKEY=XXXXXXXXXXXXXXXX
htmlFormat.py
def Format(f, c):
    email_content = '''<html>
    <head>
       <meta charset="UTF-8" />
       <title></title>
    </head>
    <body>
       <style type="text/css">
          * {
             margin: 0;
             padding: 0;
             border: 0;
          }

          body {
             width: 1000px;
             margin: 0 auto;
          }

          .mainBox {
             margin: 0 auto;
             display: block;
             border: solid 2px hotpink;
             border-radius: 20px;
             width: 800px;
             height: auto;
          }

          #From {
             color: orange;
             font-size: 0.9em;
             line-height: 1em;
          }

          p {
             text-indent: 2em;
             color: purple;
             font-size: 1.5em;
          }

          p[align="right"] {
             color: gray;
          }
       </style>
       <div class="mainBox">
          <h1>您收到一条来自<span id="From">''' + f"{f}"+ '''</span>的短信</h1>
          <h3>内容为:'''+ f"{c}"+ '''</h3>
          <p></p>
          <p align="right">Summer Redirecting | Ver 0.1.1(1103 | x86_64 Linux/Windows)</p>
       </div>
    </body>
</html>
</body>
</html>'''
    return email_content

测试运行

C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\python.exe E:\doc\PycharmProjects\Email_Api\app.py 
 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:30010
Press CTRL+C to quit
打开 http://127.0.0.1:30010

出现404即表示运行成功

在浏览器打开: http://127.0.0.1:30010/api/send?from=10010&content=hello

image.png

出现这个提示则代表邮件转发成功,回到邮箱查看

image.png

广告&特别鸣谢

雨云 为本项目提供服务器资源

雨云最近上线了“云应用”,到“云应用”中开启一个“宝塔面板”的项目

进入终端重置宝塔用户名和密码,然后进入宝塔,绑定宝塔账号

仅需安装nginx实现网站访问

在网站中添加ip:80的网站,然后配置反向代理到127.0.0.1:30010

即可实现访问域名进行邮件转发