標籤:deb 成功 .text context set inf tab tac att
好開心啊,在旁邊大神的協助下,終於實現了發工資的python指令碼,之前用shell寫的老出錯,剛才測試,發80個人工資詳情,妥妥的,代碼如下:
from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.header import Header#from handle_data_for_salary import main#import handle_data_for_salaryimport smtplibimport xlrdimport xdrlib,sysdef open_excel(file=‘test1.xls‘): try: data = xlrd.open_workbook(file) return data except Exception as e: print(str(e))def excel_table_byname(file=‘test1.xls‘,colnameindex=0,by_name=‘Sheet1‘): data = open_excel(file) table = data.sheet_by_name(by_name) nrows = table.nrows #行數 colnames = table.row_values(colnameindex) #某一行資料 list = [] for rownum in range(2,nrows): row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return listdef get_value(): tables = excel_table_byname() for row in tables: global mail mail = row["郵箱地址"] global name name = row["姓名"] global context authInfo = {} authInfo[‘server‘] = ‘smtp.163.com‘ authInfo[‘user‘] = ‘[email protected]‘ authInfo[‘password‘] = ‘郵箱密碼‘ fromAdd = ‘[email protected]‘ subject = ‘Email Subject‘ context = str(row) toAdd = [mail, ] sendEmail(authInfo, fromAdd, toAdd, subject, context)def sendEmail(authInfo, fromAdd, toAdd, subject, plainText): strFrom = fromAdd strTo = ‘; ‘.join(toAdd) server = authInfo.get(‘server‘) smtpPort = 25 sslPort = 465 user = authInfo.get(‘user‘) passwd = authInfo.get(‘password‘) if not (server and user and passwd) : print(‘incomplete login info, exit now‘) return # 設定root資訊 msgRoot = MIMEMultipart(‘related‘) msgRoot[‘Subject‘] = subject msgRoot[‘From‘] = ‘%s<%s>‘ % (Header(‘工資詳情‘, ‘utf-8‘), strFrom) msgRoot[‘To‘] = strTo # 郵件內文內容 msgText = MIMEText(plainText, ‘plain‘, ‘utf-8‘) msgRoot.attach(msgText) msgAlternative = MIMEMultipart(‘alternative‘) msgRoot.attach(msgAlternative) try: #發送郵件 smtp = smtplib.SMTP() smtp.connect(server, smtpPort) #ssl加密方式,通訊過程加密,郵件資料安全 # smtp = smtplib.SMTP_SSL() # smtp.connect(server, sslPort) #設定調試層級,依情況而定 # smtp.set_debuglevel(1) smtp.login(user, passwd) smtp.sendmail(strFrom, toAdd, msgRoot.as_string()) smtp.quit() print("郵件發送給%s成功!"%(name)) except Exception as e: print("失敗:" + str(e))if __name__ == ‘__main__‘ : get_value()
注意:python環境為3.5,實驗環境在windows,使用工具為pycharm,需要用到的模組:pip3 install xlrd,其中的工資表,如部分所示:後面還有很多列,看自己的需求加不,反正都是一列一列的資料
實驗結果如下:
現在只是實現了需求,發的郵件多了估計有問題,還有郵件的格式太醜了,等下回家再改改,加油加油
python實現發工資指令碼