1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 defSMTP (file):4 fromEmail.mime.textImportMimetext5 fromEmail.mime.multipartImportMimemultipart6 ImportSmtplib7 8 #Msg=mimemultipart ()9 Ten #Construction Annex 1 One #att1 = mimetext (open (file, ' RB '). Read (), ' base64 ', ' gb2312 ') A #att1["content-type"] = ' application/octet-stream ' - #att1["content-disposition"] = ' attachment; filename= ' +file# here filename can be arbitrarily written, what name to write, what name is displayed in the message - #Msg.attach (ATT1) the - #Construction Annex 2 - #att2 = mimetext (open (' d:\\123.txt ', ' RB '). Read (), ' base64 ', ' gb2312 ') - #att2["content-type"] = ' application/octet-stream ' + #att2["content-disposition"] = ' attachment; filename= "123.txt" - #Msg.attach (ATT2) + A #Add message header atMsg=mimetext (file,_charset='Utf-8')
#to_list =[' [email protected] ', ' [email protected] ', ' [email protected] ' #发送给多个用户 -msg[' to'] ='[email protected]' -msg[' from'] =' Test@163.com' -msg['subject'] ='Backup' - #Send mail - Try: inServer =Smtplib. SMTP () -Server.connect ('smtp.163.com') toServer.login (' Zenghui',' 123456')#xxx for user name, xxxxx for password +Server.sendmail (msg[' from'], msg[' to'],msg.as_string ()) #如果要发送多个用户, replace msg[' to '] with to_list - server.quit () the Print 'sent successfully' * exceptException, E: $ PrintStr (e)Panax Notoginseng if __name__=='__main__': -smtp'Test')
Python Send mail