QQ邮箱
发送文本
import os.path
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_text(subject, body, from_addr, to_addr):
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = ';'.join(to_addr)
msg['Subject'] = subject
att = MIMEText(body, 'plain', 'utf-8')
msg.attach(att)
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
smtp.login(from_addr, password)
# 开始发送
smtp.sendmail(from_addr, to_addr, msg.as_string())
smtp.quit()
发送html
import os.path
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_html(subject, body, from_addr, to_addr):
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = ';'.join(to_addr)
msg['Subject'] = subject
att = MIMEText(body, 'html', 'utf-8')
msg.attach(att)
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
smtp.login(from_addr, password)
# 开始发送
smtp.sendmail(from_addr, to_addr, msg.as_string())
smtp.quit()
发送文件
import os.path
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_file(subject, body, from_addr, to_addr, file_path):
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = ';'.join(to_addr)
msg['Subject'] = subject
text_apart = MIMEText(body, 'plain', 'utf-8')
excel_apart = MIMEApplication(open(file_path, 'rb').read())
excel_apart.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_path))
msg.attach(text_apart)
msg.attach(excel_apart)
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
smtp.login(from_addr, password)
# 开始发送
smtp.sendmail(from_addr, to_addr, msg.as_string())
smtp.quit()