使用qq邮箱自动备份,wiki下
使用qq邮箱自动备份
import os
import zipfile
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE
from email import encoders
from datetime import datetime
# 设置参数
source_dir = 'D:\hexo\source' # 源文件夹路径
backup_name = 'backup_' + datetime.now().strftime('%Y-%m-%d') + '.zip' # 备份文件名(加入当前日期)
to_email = 'jlwu@vip.163.com' # 收件人邮箱
from_email = 'aoao@qq.com' # 发件人邮箱
password = 'bbxfxvjutcyibhhjd' # 发件人邮箱密码
smtp_server = 'smtp.qq.com' # SMTP服务器地址
smtp_port = 587 # SMTP服务器端口号
# 打包备份文件
zip_path = os.path.join(source_dir, backup_name)
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as backup_zip:
for root, dirs, files in os.walk(source_dir):
for file in files:
backup_zip.write(os.path.join(root, file))
# 发送电子邮件
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = COMMASPACE.join([to_email])
msg['Subject'] = 'backup_' + datetime.now().strftime('%Y-%m-%d') + '.zip'
msg.attach(MIMEText('Backup of files from D:\hexo\source\_posts'))
with open(zip_path, "rb") as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=backup_name)
msg.attach(part)
smtp = smtplib.SMTP(smtp_server, smtp_port)
smtp.ehlo()
smtp.starttls()
smtp.login(from_email, password)
smtp.sendmail(from_email, to_email, msg.as_string())
smtp.quit()
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xieaoao@qq.com QQ:1296454177