用python发送邮件
import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
smtp_obj = smtplib.SMTP_SSL(host='smtp.qq.com', port=465)
smtp_obj.login('账号@qq.com', '授权码')
m_part = MIMEMultipart()
m_part['From'] = '账号@qq.com'
m_part['To'] = '账号@qq.com'
m_part['Subject'] = '你的状态'
content = """
<p style="margin: 10px 10px">你是头脑有病,污言秽语,<em>殃及无辜</em>,<strong color: pink>祸害众生,
人模狗样</strong>,<span style="color:red; font-size: 22px">无可救药,无颜面对江东父老;</span>
<p>你在无中生有,无可救药,胡言乱语,</p>
<p>凭空捏造,无可救药,一路走好</p>
"""
resp = requests.get('https://www.sohu.com/index.html')
html_page = MIMEText(resp.text + content, 'html', 'utf-8')
m_part.attach(html_page)
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)
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)
smtp_obj.sendmail(
from_addr='发送账号@qq.com',
to_addrs=['收件账号@qq.com'],
msg=m_part.as_string()
)
smtp_obj.quit()