python 邮件

该博客展示了如何使用Python的smtplib和email库发送带有附件的HTML格式邮件。通过SMTP_SSL建立SSL连接,设置邮件主题、内容,并附加一个名为'aa.sh'的文件。邮件内容包含HTML标签,用于格式化文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值