summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhaokun <[email protected]>2024-09-09 11:41:50 +0800
committerzhaokun <[email protected]>2024-09-09 11:41:50 +0800
commitd2007fb80155a00cf78a8b4bea65186cc350e5aa (patch)
treecbefc5541bc6d1565fd9ef0051161ec53f2f5dab
parentfbf7d2ef51c9ddd962b3637f071333a00994c304 (diff)
modify mail player
-rw-r--r--mail_player.py87
1 files changed, 55 insertions, 32 deletions
diff --git a/mail_player.py b/mail_player.py
index b4fd5c2..e4b806f 100644
--- a/mail_player.py
+++ b/mail_player.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
+import json
import subprocess
from datetime import datetime
import smtplib
@@ -15,44 +16,59 @@ class MailPlayer:
p = subprocess.Popen(traffic_data["command"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
output, error = p.communicate()
result = output + error
+ return result
elif "command" not in traffic_data:
- mail_type = traffic_data["mail_type"]
- mail_port = traffic_data["mail_port"]
- sender = traffic_data["sender"]
- password = traffic_data["password"]
- receiver = traffic_data["receiver"]
- subject = traffic_data["subject"]
- body = traffic_data["body"]
- attach = traffic_data["attach"]
- msg = MIMEMultipart()
- msg['From'] = Header(sender, 'utf-8')
- msg['To'] = Header(receiver, 'utf-8')
- msg['Subject'] = Header(subject, 'utf-8')
- msg['Date'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- # 邮件正文内容,MIMEText是纯文本
- msg.attach(MIMEText(body, 'plain', 'utf-8'))
- # 附件
- with open(attach, "rb") as attachment:
- part = MIMEBase("application", "octet-stream")
- part.set_payload((attachment).read())
- encoders.encode_base64(part)
- part.add_header("Content-Disposition", f"attachment; filename= {attach}")
- msg.attach(part)
try:
+ mail_type = traffic_data["mail_type"]
+ mail_server = traffic_data["mail_server"]
+ mail_port = traffic_data["mail_port"]
+ mail_timeout = traffic_data["mail_timeout"]
+ sender = traffic_data["sender"]
+ password = traffic_data["password"]
+ receiver = traffic_data["receiver"]
+ subject = traffic_data["subject"]
+ body = traffic_data["body"]
+ attach = traffic_data["attach"]
+ msg = MIMEMultipart()
+ msg['From'] = Header(sender, 'utf-8')
+ msg['To'] = Header(receiver, 'utf-8')
+ msg['Subject'] = Header(subject, 'utf-8')
+ msg['Date'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ # 邮件正文内容,MIMEText是纯文本
+ msg.attach(MIMEText(body, 'plain', 'utf-8'))
+ # 附件
+ with open(attach, "rb") as attachment:
+ part = MIMEBase("application", "octet-stream")
+ part.set_payload((attachment).read())
+ encoders.encode_base64(part)
+ part.add_header("Content-Disposition", f"attachment; filename= {attach}")
+ msg.attach(part)
+ server_code = 0
+ server_resp = "None"
if mail_type == "gmail":
- smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", mail_port)
- else:
- smtp_obj = smtplib.SMTP_SSL("192.168.40.206", mail_port)
+ smtp_obj = smtplib.SMTP_SSL(mail_server, mail_port, timeout=mail_timeout)
+ elif mail_type == "smtp_ssl":
+ smtp_obj = smtplib.SMTP_SSL(mail_server, mail_port, timeout=mail_timeout)
+ elif mail_type == "smtp":
+ smtp_obj = smtplib.SMTP(mail_server, mail_port, timeout=mail_timeout)
# 如果服务器不支持STARTTLS,需注释掉下面这行
# smtp_obj.starttls()
+ # 设置debuglevel,会实时打印信息
+ # smtp_obj.set_debuglevel(1)
smtp_obj.login(sender, password)
smtp_obj.sendmail(sender, receiver, msg.as_string())
return "Email sent successfully"
- except smtplib.SMTPException as e:
- return f"SMTPException: {e}"
+ except (Exception, smtplib.SMTPException) as e:
+ if isinstance(smtp_obj.ehlo_resp, bytes):
+ server_resp = smtp_obj.ehlo_resp.decode("utf-8")
+ if "Mail was identified as spam" in server_resp:
+ server_code = 550
+ elif "User not local; please try" in server_resp:
+ server_code = 551
+ return f"smpt_exception: {e}, server_code: {server_code}, server_resp: {server_resp}"
finally:
- smtp_obj.quit()
- return result
+ if server_code != 0 and server_code != 550 and server_code != 551:
+ smtp_obj.quit()
if __name__ == "__main__":
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "test")
@@ -60,15 +76,22 @@ if __name__ == "__main__":
"traffic": {
"protocol": "mail",
"type": "client", # client/curl
- "mail_type": "other", # gmail or other smtp
- "mail_port": 465, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25
+ "mail_type": "smtp", # gmail or smtp or smtp_ssl
+ "mail_server": "192.168.40.206", # gmail: smtp.gmail.com
+ "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25
+ "mail_timeout": 20,
"sender": "[email protected]",
"password": "111111",
"receiver": "[email protected]",
"subject": "aaa",
"body": "123",
- "attach": "d:/test.txt"
+ "attach": "/opt/test.txt"
}
+ # "traffic": {
+ # "protocol": "mail",
+ # "type": "curl",
+ # "command": "curl -kv --connect-timeout 25 -m 25 --url 'smtp://192.168.40.206' --mail-from '[email protected]' --mail-rcpt '[email protected]' --upload-file '/opt/test.txt' --user '[email protected]:111111'"
+ # }
}
traffic_data = test_data["traffic"]
mail_player = MailPlayer()