Encoders.encode_base64(part) ## 设置附件头 part.add_header('Content-Disposition', 'attachment; filename="'+file_name+'"') msg.attach(part) # 设置根容器属性 msg['Subject']=str(now.strftime("%Y%m%d")) + '数据' msg['From']=mail_from msg['To']=';'.join(mail_to) msg['date']...
message.attach(MIMEText('这是菜鸟教程Python 邮件发送测试……','plain','utf-8')) # 构造附件1 att1 = MIMEText(open('文件名','rb').read(),'base64','utf-8') att1["Content-Type"] ='application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 ...
print "Error: unable to send email" Windows: #!C:\Python27\python.exe # -*- coding: utf-8 -*- import smtplib import base64 filename = r"D:\python\code\123.txt" # 读取文件内容并使用 base64 编码 fo = open(filename, "rb") filecontent = fo.read() encodedcontent = base64.b64en...
msg.attach(msgImage) 5 加入PDF附件 # 将一个pdf附件放入邮件中 filepath = r"C:UsersadminDesktop运营python发送邮件yte-of-python-chinese-edition.pdf" filename = "byte-of-python-chinese-edition.pdf" with open(filepath,'rb') as f: #读取pdf文件赋值一个对象 attachfile = MIMEApplication(f.rea...
msg['To']=receiver_email msg['Subject']="这是一封带附件的邮件"# 邮件正文内容body="你好,这是一封带有附件的邮件。"msg.attach(MIMEText(body,'plain')) 1. 2. 3. 4. 5. 6. 7. 8. 9. MIMEMultipart创建一个可以包含多个部分的邮件。
msg['to'] = ','.join(msg_to) # 邮件正文内容 msg.attach(MIMEText('今日日报:1 xxx', 'plain', 'utf-8')) # 添加附件1 # 构造附件1,传送当前目录下的 ribao.txt 文件,注意文件要以rb的形式读取 file = open('ribao.txt', 'rb').read() ...
attach(MIMEText(message, 'html')) # message的类型也可以是text server.login(smtp_username, smtp_password) server.sendmail(sender_email, receiver_email, msg.as_string()) server.quit() 以上就是使用python发送邮件的全部记录。 编辑于 2025-02-24 16:04・IP 属地湖南 赞同2添加评论 ...
attach(MIMEText('邮件附件测试内容', 'plain', 'utf-8')) # 构造附件1,传送当前目录下的 test.txt 文件 att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 ...
msg_root.attach(MIMEText或者MIMEImage对象),因为MIMEMultipart对象代表邮件本身,其他连个是代表邮件正文,所以这个方法还是很强大的,把其他的构造内容添加到MIMEMultipart对象中就可以把文本,html,附件等一起发送了。 5.发送各种内容的具体代码实现: 所有代码合到一块,发送文本,html,图片,txt内容,用的时候你可以把需要...
email["Subject"] = subject# Add body and attachment toemailemail.attach(MIMEText(body, "plain"))attach_file = open(file,"rb") # open the file report = MIMEBase("application", "octate-stream") report.set_payload((attach_file).read()) encoders.encode_base64(report) #add report header...