千锋教育python2104期总结day19

2021/8/5 22:07:19

本文主要是介绍千锋教育python2104期总结day19,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

千锋教育python2104期总结

第十八次总结: 自动化办公

目录

  1. 邮件自动发送基本流程
  2. 发送附件
  3. 超文本邮件内容
  4. 图片批量发送

正文

1.对象属性的增删改

第一步
1.连接邮箱服务器
smtplib.SMTP_SSL(服务器地址,邮箱服务端口)—连接邮箱服务器并返回一个连接对象

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
connect = smtplib.SMTP_SSL('smtp.qq.com', 465)

2.登录邮箱
连接对象.login(账号,密码)

connect.login('1909837076@qq.com', 'bklwpetufvwmfbfd')

第二步

1.创建邮件对象

email = MIMEMultipart()

2.设置邮件主题
主题对象=Header(邮件标题,编码方式).encode()
邮件对象[‘Subject’]=主题对象

zhuti = Header('自动邮件', 'utf-8').encode()
email['Subject'] = zhuti

3.设置邮件发送者,接收者
邮件对象[‘From’]=‘发送人’
邮件对象[‘To’]=‘收件人’

email['From'] = '我叫摩拉托斯,你也可以叫我钟迪'
email['To'] = ''

4.添加正文
1)创建正文内容
MIMEText(‘文字内容’,类型,编码方式)
类型—plain(普通文字),html(超文本)

zhenwen = MIMEText('瓜皮', 'plain', 'utf-8')
email.attach(zhenwen)

第三步:通过连接发送邮件

连接对象.sendmail(发件人,收件人,邮件对象.as_string())

connect.sendmail('1909837076@qq.com', '1337758523@qq.com', email.as_string())
connect.quit()

2. 发送附件

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage

con=smtplib.SMTP_SSL('smtp.qq.com',465)
con.login('1909837076@qq.com','bklwpetufvwmfbfd')

email=MIMEMultipart()
email['Subject']=Header('发送附件','utf-8').encode()
email['To']='guapi'
email['From']='\u4e00'

zhenwen=MIMEText('瓜皮', 'plain', 'utf-8')
email.attach(zhenwen)

图片附件==
准备图片

im_date=open('12.jpg','rb').read()

创建图片对象

im=MIMEImage(im_date)

设置图片为附件

im['Content-Disposition']= 'attachment; filename="12"'

添加图片到邮件

email.attach(im)

其他附件==
准备附件

zz_date=open('zz.zip','rb').read()

创建附件对象

zz=MIMEText(zz_date,'base64','utf-8')

设置为附件

zz['Content-Disposition']= 'attachment; filename="扫雷"'

添加附件到邮件

email.attach(zz)

con.sendmail('1909837076@qq.com', 'zhao_ming_xing@163.com', email.as_string())
con.quit()

3.超文本邮件内容

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage
con=smtplib.SMTP_SSL('smtp.qq.com',465)
con.login('1909837076@qq.com','bklwpetufvwmfbfd')

email=MIMEMultipart()
email['Subject']=Header('HTML邮件','utf-8').encode()
email['To']='1'
email['From']='0'

=正文=

html1=open('send.html','r').read()
html=MIMEText(html1,'html','utf-8')
email.attach(html)

创建图片对象

im_date=open('13.jpeg','rb').read()
im=MIMEImage(im_date)
im.add_header('Content-ID','<im1>')
email.attach(im)

con.sendmail('1909837076@qq.com', 'zhao_ming_xing@163.com', email.as_string())
con.quit()

4. 图片批量发送

import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import os

connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
connect.login('726550822@qq.com', 'ryjjmssnbbjgbcbb')

email = MIMEMultipart()
email['Subject'] = Header('HTML邮件', 'utf-8').encode()
email['To'] = 'y_t209@163.com'
email['From'] = '726550822@qq.com <726550822@qq.com>'

====邮件正文=

all_images = os.listdir('images')
ids = []
for name in all_images:
    image_data = open(f'images/{name}', 'rb').read()
    image = MIMEImage(image_data)
    cid = name.split(".")[0]
    ids.append(cid)
    image.add_header('Content-ID', f'<{cid}>')
    email.attach(image)

send = ''
for cid in ids:
    send += f'<img src="cid:{cid}" alt=""><br>'
text = MIMEText(send, 'html', 'utf-8')
email.attach(text)

connect.sendmail('726550822@qq.com', 'y_t209@163.com', email.as_string())
connect.quit()


这篇关于千锋教育python2104期总结day19的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程