使用python,Flask框架实现懒人精灵客户端发送请求信息在客户端发送邮件.
示例使用的网易的163邮箱需开启邮箱”SMTP服务”
邮箱注册地址:网易邮箱
懒人精灵服务端发送邮件.py 文件源码:
- import json
- import os
- import smtplib
- from email.mime.text import MIMEText
- from flask import Flask, request, make_response,send_from_directory
- app = Flask(__name__)
- def set_Email(userEmail,sendstr,title):
- # 设置服务器所需信息
- # 163邮箱服务器地址
- mail_host = 'smtp.163.com'
- # 163用户名
- mail_user = 'aitlo_e_mail'
- # 密码(部分邮箱为授权码)
- mail_pass = 'TJOBPXXXXXXXXX'
- # 邮件发送方邮箱地址
- sender = 'aitlo_e_mail@163.com'
- # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
- receivers = [userEmail]
- # 设置email信息
- # 邮件内容设置
- message = MIMEText(sendstr, 'plain', 'utf-8')
- # 邮件主题
- message['Subject'] = title
- # 发送方信息
- message['From'] = sender
- # 接受方信息
- message['To'] = receivers[0]
- # 登录并发送邮件
- try:
- smtpObj = smtplib.SMTP()
- # 连接到服务器
- smtpObj.connect(mail_host, 25)
- # 登录到服务器
- smtpObj.login(mail_user, mail_pass)
- # 发送
- smtpObj.sendmail(
- sender, receivers, message.as_string())
- # 退出
- smtpObj.quit()
- return f' {title} 已发送至:{userEmail}'
- except smtplib.SMTPException as e:
- return f'发送错误:error:{e}' # 打印错误
- @app.route("/email",methods=["POST"])
- def email():
- # 接收懒人精灵客户端发送post请求数据 调用set_Email函数执行邮件发送,成功后给客户端返回结果信息.
- print(request.form)
- userEmail = request.form.get("userEmail")
- sendstr = request.form.get("sendstr")
- title = request.form.get("title")
- result = set_Email(userEmail, sendstr, title)
- print(result)
- return result
- if __name__ == '__main__':
- # app.run()
- app.run(host="0.0.0.0", port="5000", debug=True)
复制代码
懒人精灵服务端发送邮件.lua 文件源码:
- function SendData(url,tab)
- -- 客户端发送数据至服务器
- print(tab)
- local ret1,code = httpPost(string.format("%s/email",url),string.format("userEmail=%s&title=%s&sendstr=%s",tab["userEmail"],tab["title"],tab["sendstr"]))
- print(ret1)
- return ret1
-
- end
- -- userEmail 对方的邮箱地址
- -- title 邮件标题
- -- sendstr 需要发送的消息内容
- -- url 服务器地址
- tab = {userEmail="393074304@qq.com",title="懒人精灵邮件",sendstr="懒人精灵发送测试邮件"}
- url = "http://192.168.2.108:5000/"
- SendData(url,tab)
复制代码
运行结果图片:
|