#coding: utf-8  
import sys
import os
import smtplib  
from email.mime.multipart import MIMEMultipart  
from email.mime.text import MIMEText 
from email.header import Header  

def sendmail(subject, content, filename):
	try:
		sender = 'lxz_dev@163.com'  
		receiver = 'lxz_dev@163.com'
		smtpserver = 'smtp.163.com'  
		username = 'lxz_dev@163.com'  
		password = 'develop'  
		

		msgRoot = MIMEMultipart('related')  
		msgRoot['Subject'] = subject

		print(content)
		content=content.replace('\n', '<br/>')
		msgText = MIMEText(content,'html','utf-8') 
		msgRoot.attach(msgText)  

		if(os.path.exists(filename)):
			att = MIMEText(open(filename, 'rb').read(), 'base64', 'utf-8')  
			att["Content-Type"] = 'application/octet-stream'  
			att["Content-Disposition"] = 'attachment; filename="report.txt"'  
			msgRoot.attach(att)
		else:
			print('file not exist')
  
		smtp = smtplib.SMTP()  
		smtp.connect('smtp.163.com')  
		smtp.login(username, password)  
		smtp.sendmail(sender, receiver, msgRoot.as_string())  
		smtp.quit()  
	except Exception as e:
	 	print(e)


def main():
	subject = sys.argv[1]
	content = '<html>' + sys.argv[2] + '</html>'
	attachfile = sys.argv[3]
	sendmail(subject, content, attachfile)

if __name__ == '__main__':
	main()