import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 创建SMTP_SSL对象
smtp_obj = smtplib.SMTP_SSL(host='smtp.qq.com', port=465)
# 1. 授权登录
smtp_obj.login('账号@qq.com', '授权码')
m_part = MIMEMultipart()
# 发件人
m_part['From'] = '账号@qq.com'
# 收件人
m_part['To'] = '账号@qq.com'
m_part['Subject'] = '你的状态'
# 发送内容
content = """
你是头脑有病,污言秽语,殃及无辜,祸害众生,
人模狗样,无可救药,无颜面对江东父老;
你在无中生有,无可救药,胡言乱语,
凭空捏造,无可救药,一路走好
"""
# 附件
resp = requests.get('https://www.sohu.com/index.html')
html_page = MIMEText(resp.text + content, 'html', 'utf-8')
m_part.attach(html_page)
# 添加pdf附件
with open('resources/用Python发送邮件和短信.pdf', 'rb') as file:
pdf_file = MIMEText(file.read(), 'base64', 'utf-8')
pdf_file['content-type'] = 'application/pdf'
pdf_file['content-disposition'] = 'attachment; filename="用Python发送邮件和短信.pdf"'
m_part.attach(pdf_file)
# 添加xlsx附件
with open('resources/阿里巴巴2020年股票数据.xlsx', 'rb') as file:
excel_file = MIMEText(file.read(), 'base64', 'utf-8')
excel_file['content-type'] = 'application/vnd.ms-excel'
excel_file['content-disposition'] = 'attachment; filename="alibaba-stock.xlsx"'
m_part.attach(excel_file)
# 2. 发送邮件
smtp_obj.sendmail(
from_addr='发送账号@qq.com',
to_addrs=['收件账号@qq.com'],
msg=m_part.as_string()
)
# 3. 结束会话
smtp_obj.quit()



