自动化办公
邮件自动发送
import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
connect.login('1823790118@qq.com', 'kvpbssxxdcioedde')
email = MIMEMultipart()
email['Subject'] = Header('第一次发送邮件', 'utf-8').encode()
email['To'] = '1823790118@qq.com'
email['From'] = '1823790118@qq.com <1823790118@qq.com>'
"""
MIMEText(文字内容, 类型, 编码方式)
类型 - plain(普通文字)、html(超文本)
"""
text = MIMEText('你好吗?吃饭了吗?', 'plain', 'utf-8')
email.attach(text)
connect.sendmail('1823790118@qq.com', '1823790118@qq.com', email.as_string())
connect.quit()
发送附件
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
connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
connect.login('1823790118@qq.com', 'kvpbssxxdcioedde')
email = MIMEMultipart()
email['Subject'] = Header('发送附件', 'utf-8').encode()
email['To'] = 'y_t@163.com'
email['From'] = '726550822@qq.com <726550822@qq.com>'
text = MIMEText('照片在附件中,请注意查收!', 'plain', 'utf-8')
email.attach(text)
image_data = open('files/luffy4.jpg', 'rb').read()
image = MIMEImage(image_data)
image['Content-Disposition'] = 'attachment; filename="luffy.jpg"'
email.attach(image)
超文本邮件内容
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
connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
connect.login('1823790118@qq.com', 'kvpbssxxdcioedde')
email = MIMEMultipart()
email['Subject'] = Header('HTML邮件', 'utf-8').encode()
email['To'] = '1823790118@qq.com'
email['From'] = '1823790118@qq.com <1823790118@qq.com>'
content = open('files/send.html', encoding='utf-8').read()
text = MIMEText(content, 'html', 'utf-8')
email.attach(text)
image_data = open('files/luffy4.jpg', 'rb').read()
image = MIMEImage(image_data)
image.add_header('Content-ID', '<image1>')
email.attach(image)
connect.sendmail('1823790118@qq.com', '1823790118@qq.com', email.as_string())
connect.quit()