from time import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def sendmail():
# 发送邮箱服务器
smtpserver = ""
# 发送邮箱用户名密码
user = ""
password = ""
# 发送和接收邮箱
sender = ""
#receive = ""
receive = ""
# 发送邮件主题和内容
subject = "dasfowefe"
content = "<html><h1 style='color:red'>zabbixdjflajdflkajfklasjdfklasjdlf</h1></html>"
msgRoot = MIMEMultipart()
send_file = open(r"/root/{}".format("aa.sh"), "rb").read()
att = MIMEApplication(send_file, )
att['Content-Type'] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment', filename='{}'.format("aa.sh"))
msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
msgRoot['Subject'] = subject
msgRoot['From'] = sender
msgRoot['To'] = ''.join(receive)
msgRoot.attach(att)
# SSL协议端口号要使用465
smtp = smtplib.SMTP_SSL(smtpserver, 465)
# HELO向服务器标志用户身份
smtp.helo(smtpserver)
# 服务器返回结果确认
smtp.ehlo(smtpserver)
# 登录邮箱服务器用户名密码
smtp.login(user, password)
print("Send email start...")
smtp.sendmail(sender, receive, msgRoot.as_string())
smtp.quit()
print("email send end!")
sendmail()
python 邮件
最新推荐文章于 2024-07-27 12:20:46 发布