【问题标题】:How to schedule and send email of python generated report automatically in python如何在python中自动安排和发送python生成报告的电子邮件
【发布时间】:2020-04-09 13:52:06
【问题描述】:

有没有办法使用 windows 10 中的 python 包和代码从特定文件夹中选择报告并在特定时间安排向提及的收件人发送电子邮件?

如果有人可以帮助开始这个项目,我有一个想法并希望有一些鼓舞人心的代码。

【问题讨论】:

    标签: python windows email report scheduled-tasks


    【解决方案1】:

    第 1 部分:

    在python中发送邮件的脚本 您需要在安排脚本的主要 python 文件中访问 send_email() 函数

    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.base import MIMEBase
    from email import encoders
    
    def send_email(mail_body="", subject=""):
        fromaddr = "sender mail address"
        toaddr_list = [
         //recepients address list
        ]
        passwd = "need to create gmail app to get password"
        msg = MIMEMultipart()
    
        msg['From'] = fromaddr
        msg['To'] = ", ".join(toaddr_list)
        if(subject):
            msg['Subject'] = subject
        else:
            msg['Subject'] = "subject of mail."
    
        body = mail_body
    
        msg.attach(MIMEText(body, 'plain'))
        s = smtplib.SMTP('smtp.gmail.com', 587)
        s.starttls()
        s.login(fromaddr, passwd)
        text = msg.as_string()
        s.sendmail(fromaddr, toaddr_list, text)
        s.quit()
    

    第 2 部分:Windows 10 中的计划脚本 对于 windows 中的调度脚本,您可以参考: https://datatofish.com/python-script-windows-scheduler/

    【讨论】:

      【解决方案2】:

      是的,您可以使用不同的 python 包来做到这一点。

      Read the specific file
      Send mail using built-in smtplib module
      Time scheduler using schedule library Or you can use cron or celery in windows. 
      

      【讨论】:

        【解决方案3】:

        阅读自动化无聊的东西 - 第 16 章。

        您将获得代码和如何实现相同的想法 Automate the boring stuff

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-11
          • 2014-12-18
          • 2020-10-13
          • 2011-02-04
          • 2013-02-18
          • 1970-01-01
          • 2023-02-15
          相关资源
          最近更新 更多