import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from flask import Flask, request, jsonify
app = Flask(__name__)
def send_email(to_emails, subject, html_content):
mail_server = "smtp.qq.com"
port = 587 #邮件服务器端口
from_email = "ops-alarm@qq.com"
password = "eaikq********jdi"
# 创建邮件
message = MIMEMultipart()
message["From"] = from_email
message["To"] = ", ".join(to_emails)
message["Subject"] = subject
html_body = MIMEText(html_content, 'html', 'utf-8')
message.attach(html_body)
with smtplib.SMTP(mail_server, port) as server:
server.login(from_email, password)
server.sendmail(from_email, to_emails, message.as_string())
@app.route('/', methods=['POST'])
def send_email_from_url():
to_emails = request.form.get('mail')
subject = request.form.get('subject')
html_content = request.form.get('html_content')
if to_emails and subject and html_content:
to_emails_list = to_emails.split(',')
send_email(to_emails_list, subject, html_content)
return jsonify({"message": "Email sent successfully"})
else:
return jsonify({"error": "Missing required parameters"}), 400
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
使用方法
curl -X POST "https://tomail.netwebs.top/" -d "mail=test1@163.com,test2@qq.com,test...&subject=邮件标题&html_content=邮件内容(支持HTML文本)"
1、支持多个邮箱地址,用英文逗号分隔
2、支持HTML文本内容
3、将tomail.netwebs.top改为自己服务器IP/demon_name