美文网首页
利用 Python 一键推送电子书到 Kindle

利用 Python 一键推送电子书到 Kindle

作者: rEload | 来源:发表于2019-03-25 22:03 被阅读0次
    #!/usr/bin/env python3
    #coding:utf-8
    
    import smtplib
    from email.header import Header
    from email.mime.text import MIMEText
    from email.utils import formataddr
    from email.mime.multipart import MIMEMultipart
    from email.mime.application import MIMEApplication
    
    sender = "xxx@163.com"
    receivers = [sender,'xxx@kindle.cn,xxx@kindle.cn']
    
    def email(filename):
    
        msg = MIMEMultipart()
        msg['From'] = formataddr(["Kindle 管理员", sender])
        msg['To'] = formataddr(["kindle", receivers[0]])
        msg['Subject'] = "电子书同步: %s" % filename
        msg.attach(MIMEText(filename, 'plain', 'utf-8'))
    
        att = MIMEApplication(open(filename, 'rb').read())
        att.add_header(
            'Content-Disposition',
            'attachment',
            filename=Header(filename, "utf-8").encode())
    
        msg.attach(att)
    
        try:
            server = smtplib.SMTP("smtp.163.com")
            server.login(sender, "xxxxx")
            server.sendmail(sender, receivers, msg.as_string())
            print("*邮件* %s 发送成功!" % filename)
        except smtplib.SMTPException:
            print("Error: 无法发送邮件")
    
    
    import os
    if __name__ == '__main__':
        #  email("hello")
        #  for root, dirs, files in os.walk("."):
        files = os.listdir()
        files = filter(lambda x:x.endswith(".mobi") or x.endswith(".epub") or x.endswith((".pdf")),files)
    
        for filename in files:
            email(filename)
    
    

    相关文章

      网友评论

          本文标题:利用 Python 一键推送电子书到 Kindle

          本文链接:https://www.haomeiwen.com/subject/ygeivqtx.html