### 发送邮件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# 邮件信息配置
smtp_server = 'smtp.exmail.qq.com' # SMTP 服务器地址
smtp_port = 465 # SMTP 服务器端口号
username = 'xx' # 你的邮箱账号
password = 'xx' # 你的邮箱密码
from_addr = "xx"
to_addr = 'xx' # 收件人邮箱
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = (f'客户网络信息检 - {public_ip_address} 带附件')
# 添加邮件正文
body += '这是邮件的正文内容。'
msg.attach(MIMEText(body, 'plain'))
# 附件1
filename1 = 'http.txt' # 附件1文件名
attachment1 = open(filename1, 'rb') # 附件1文件路径
part1 = MIMEBase('application', 'octet-stream')
part1.set_payload(attachment1.read())
encoders.encode_base64(part1)
part1.add_header('Content-Disposition', f'attachment; filename= {filename1}')
msg.attach(part1)
# 附件2
filename2 = 'https.txt' # 附件2文件名
attachment2 = open(filename2, 'rb') # 附件2文件路径
part2 = MIMEBase('application', 'octet-stream')
part2.set_payload(attachment2.read())
encoders.encode_base64(part2)
part2.add_header('Content-Disposition', f'attachment; filename= {filename2}')
msg.attach(part2)
# 附件3
with open("check.txt", 'w', encoding='utf-8') as file:
file.write(body)
filename3 = 'check.txt' # 附件2文件名
attachment3 = open(filename3, 'rb') # 附件2文件路径
part3 = MIMEBase('application', 'octet-stream')
part3.set_payload(attachment3.read())
encoders.encode_base64(part3)
part3.add_header('Content-Disposition', f'attachment; filename= {filename3}')
msg.attach(part3)
# 发送邮件
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
#server.starttls()
server.login(username, password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
body+="\r\n"
print('邮件发送成功!')
最后修改:2024 年 05 月 11 日
© 允许规范转载