程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Gadget: just send a weekly report email to the boss. Who still writes a weekly report? Dont all use Python Gadgets: just send a weekly email to the boss

編輯:Python

origin : New job hopping to a company , I didn't expect that the first challenge was to send out weekly reports every week . Tell your boss what you have done this week and what you are going to do next week . I have a bad memory , Often forget things .
So many times the weekly newspaper will forget to send . therefore , I decided to write a gadget .

Github: Weekday Gadget

Put forward the target : (https://jq.qq.com/?_wv=1027&k=zLK3I0M5)

  • I want to have a tool that can send e-mail

Target refinement (https://jq.qq.com/?_wv=1027&k=zLK3I0M5)

  • SMTP Send E-mail , use smtplib
  • Read configuration file
  • Sender
  • The recipient
  • To whom
  • My account , password , nickname
  • Parameterized command line , use argparse
  • Support markdown to HTML

How to use brain tonic

Easy to use

cmd -a " Job content " -p # Print success

Complex use

cmd -e -p # Activate vim, then :wq Then go on cmd perform , As to the git commit -a, But print success

I forgot how to use the program I wrote  python Learning exchange group :660193417 ###

cmd -h # Print help

Design procedure (https://jq.qq.com/?_wv=1027&k=zLK3I0M5)

There is a temporary file , Used to store records . After each email sent , Archive temporary documents .


# The temporary file , There is ~/.weekday/current.rp
[global]
editor = vim
[to]
email =
cc =
[from]
email =
password =
nickname =
[SMTP]
host = smtp.exmail.qq.com

Can be in python When the script executes , Read the configuration file python Learning exchange group :660193417 ###


# Get configuration item examples
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open(' file name '))
defaultEditor = confg.get('global', 'editor') # return vim

You can parse the parameters passed in when executing the command


import argparse
parser = argparse.ArgumentParser(
description='Tell your leader what you did this week')
# define command line parameters
parser.add_argument('-v', '--version',
action='store_true',
help='show the version information')
parser.add_argument('-e', '--edit',
action='store_true',
help='open default editor for editing report')
args = parser.parse_args()
print args

Ready to send email , The first markdown Turn into HTML, And then use smtplib Send E-mail .


import smtplib
import mistune
from email.mime.text import MIMEText
content = mistune.markdown(content, escape=True, hard_wrap=True)
msg = MIMEText(content, 'html', 'utf-8')
msg['Subject'] = u' Work week %s\r\n' % date
msg['From'] = hFrom
msg['To'] = hTo
msg['Cc'] = hCc
smtp = smtplib.SMTP(conf['host'])
smtp.login(conf['from_email'], conf['password'])
smtp.sendmail(conf['from_email'],
TO_ADDR + CC_ADDR,
msg.as_string())
smtp.close()

Start writing code

The key codes of the subdivision function implementation have been sorted out . Now we just need to connect them through a series of logic . It looks like we can accomplish our goal .

# main.py
def main():
args = loadArguments()
if args.version:
print DESC
if args.append:
append(args.append, tmpFileName)
// ... other code ...
if __name__ == '__main__':
main()

Let’s go

python main.py -a Hello -p

Finally, we just need to cooperate crontab Tools , You can realize periodic timing transmission .python Learning exchange group :660193417 ###

# crontab -l
SHELL=/bin/zsh
PATH=/usr/local/bin
HOME=/Users/qiuwei
# run tasks
# minute hour day month week command
0 18 * * 5 wp -p


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved