py发邮件~

Source
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage

# 收件人
to_lst = [
    "email_address_01@126.com",
    "email_address_02@126.com",
]
# 发件人
sender = "email_address_007@126.com"

# 准备email
email = MIMEMultipart()
email['Subject'] = "明天星期天"  # 标题
email['From'] = sender
email['To'] = "email_address_03@126.com"
email['Cc'] = ",".join(to_lst)   # 抄送

#  邮件的正文
text = MIMEText("你的老婆是:<img src='cid:jay'/>", _subtype="html", _charset="utf-8")
email.attach(text)

#  附件
fu1 = MIMEApplication(open("sre.zip", mode="rb").read())
fu1.add_header("Content-disposition", "attachment", filename="sre.zip")
email.attach(fu1)

fu2 = MIMEApplication(open("file.md", mode="rb").read())
fu2.add_header("Content-disposition", "attachment", filename="file.md")
email.attach(fu2)

# 加载图片
tu = MIMEImage(open("1.jpg", mode="rb").read())
tu.add_header("Content-ID", "jay")    # 对应cid
email.attach(tu)

# 发送邮件
smtp = smtplib.SMTP()
# 连接smtp服务器
smtp.connect("smtp.126.com")      # 填入你选择的smtp服务器,网易邮箱
# smtp.connect("smtp.qq.com")     # 填入你选择的smtp服务器, qq 邮箱

# 填入用户名和密码. 登录,  密码是授权码. 并不是你的邮箱密码
smtp.login(sender, "auth_code")
smtp.sendmail(sender, to_lst, email.as_string())
print("发送成功!")

在这里插入图片描述