#!/usr/bin/env python
# Author:yeng
# \# 首先是xlsx类型的附件
# xlsxpart = MIMEApplication(open('test.xlsx', 'rb').read())
# xlsxpart.add_header('Content-Disposition', 'attachment', filename='test.xlsx')
# msg.attach(xlsxpart)
#
# \# jpg类型的附件
# jpgpart = MIMEApplication(open('beauty.jpg', 'rb').read())
# jpgpart.add_header('Content-Disposition', 'attachment', filename='beauty.jpg')
# msg.attach(jpgpart)
#
# \# mp3类型的附件
# mp3part = MIMEApplication(open('kenny.mp3', 'rb').read())
# mp3part.add_header('Content-Disposition', 'attachment', filename='benny.mp3')
# msg.attach(mp3part)
#
import openpyxl,smtplib
import email.mime.multipart
import email.mime.text
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import time
def out(file_path,first_row,last_row,from_email,from_email_passwd,from_attachment_path,attachment_filenames):
wb = openpyxl.load_workbook('%s'%(file_path))
sheet = wb.get_sheet_by_name('Sheet1')
for x in range(first_row, last_row):
names = sheet.cell(row=x, column=1).value
emails = sheet.cell(row=x, column=3).value
msg=email.mime.multipart.MIMEMultipart()
msg['from']='%s'%(from_email)
msg['to']='%s'%(emails)
msg['subject']='培训手册'
content='尊敬的:%s老师\n 你好! 培训手册:附件中,请查收。'%(names)
txt=email.mime.text.MIMEText(content)
msg.attach(txt)
xlsxpart = MIMEApplication(open('%s'%(from_attachment_path), 'rb').read())
xlsxpart.add_header('Content-Disposition', 'attachment', filename='%s'%(attachment_filenames))
msg.attach(xlsxpart)
smtp=smtplib
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com','25')
smtp.login('%s'%(from_email),'%s'%(from_email_passwd))
smtp.sendmail('%s'%(from_email),'%s'%(emails),str(msg))
smtp.quit()
time.sleep(30)
if __name__ == '__main__':
chioses = input('please chioses file path:').strip()
chioses_first_row = input('please chioses file first row:').strip()
chioses_loast_row = input('please chioses file last row:').strip()
from_email = input('please you from email:').strip()
from_meail_password = input('plesase you from email passwd:').strip()
from_attachment = input('attachment_path:').strip()
from_attachment_name = input('attachment_name:').strip()
out(chioses,int(chioses_first_row),int(chioses_loast_row),from_email,from_meail_password,from_attachment,from_attachment_name)