Before sending a message, you need to introduce it in the file header
From Sgmllib import Sgmlparser
Import Smtplib
From Email.mime.text import Mimetext
From email. Mimemultipart Import Mimemultipart
Import datetime
#定义发送邮件的函数
def send_mail (message):
today = Datetime.date.today ()
currentday=today.strftime ('%y-%m-%d ')
msg = Mimemultipart ()
'
att = Mimetext (open (R ' D:\report\aa.xlsx ', ' RB '). Read (), ' Base64 ', ' gb2312 ') #设置附件的目录
att[' content-type '] = ' application/octet-stream '
att[' content-disposition '] = ' attachment;filename= ' imd_ebm.xlsx ' #设置附件的名称
msg.attach (att)
"
content = str (message) #正文内容
body = mimetext (content, ' plain ', ' GBK ') #设置字符编码
msg.attach (body )
Msgto = [' 123@126.com '] # recipient address multiple contacts, format [' aa@163.com '; ' Bb@163.com ']
Msgfrom = ' 321@126.com ' # return address,
msg[' subject ' = ' Finish at ' currentday #主题
msg[' Date ']=time.ctime () #时间
#msg [' Cc ']= ' bb@junbao.net ' #抄送人地址 multiple addresses do not work
Mailuser = ' 321 ' # User name
mailpwd = ' 321 ' #密码
try:
smtp = Smtplib. SMTP ()
smtp.connect (R ' smtp.126.com ') # SMTP settings
smtp.login (Mailuser, mailpwd) #登录
smtp.sendmail (Msgfrom, Msgto, msg.as_string ()) #发送
smtp.close ()
print "Success Mail"
except Exception, E:
print e