directly on the code.
The code is as follows:
Import Smtplib
msg = Mimemultipart ()
#构造附件1
ATT1 = Mimetext (open ('/home/a2bgeek/develop/python/hello.py ', ' RB '). Read (), ' base64 ', ' gb2312 ')
att1["Content-type"] = ' application/octet-stream '
att1["content-disposition"] = ' attachment; Filename= "Hello.txt" ' #这里的filename可以任意写, what's the name, what's the name in the e-mail?
Msg.attach (ATT1)
#构造附件2
#att2 = Mimetext (open ('/home/a2bgeek/develop/python/mail.py ', ' RB '). Read (), ' base64 ', ' gb2312 ')
#att2 ["content-type"] = ' application/octet-stream '
#att2 ["content-disposition"] = ' attachment; Filename= "123.txt"
#msg. Attach (ATT2)
#加邮件头
Strto = [' XXX1@139.com ', ' XXX2@163.com ', ' XXX3@126.com ']
msg[' to ']= ', '. Join (Strto)
Msg[' from '] = ' YYY@163.com '
msg[' subject ' = ' mail subject '
#发送邮件
Try
Server = Smtplib. SMTP ()
Server.connect (' smtp.163.com ')
Server.login (' YYY@163.com ', ' yourpasswd ')
Server.sendmail (msg[' from '], Strto, msg.as_string ())
Server.quit ()
print ' Send Success '
Except Exception, E:
Print str (e)
The careful reader will find that there is a sentence in the code: msg[' to ']= ', '. Join (Strto), but msg[[' to ' is not used in the back, so it is obviously unreasonable to write, but this is the Stmplib bug. This is the only way you can send bulk mail. The reasons for the identification are as follows:
The problem is that smtp.sendmail and email. Mimetext need, different things.
Email. Mimetext sets up the ' to: ' Header for the body of the e-mail. It is only used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single String. (Note that it does not actually has to has anything to does with the people who actually receive the message.)
Smtp.sendmail, on the other hand, sets up the ' envelope ' of the message for the SMTP protocol. It needs a Python list of strings, each of which have a single address.
So, what are COMBINE the need to does is the replies of you received. Set msg "to" a single string, but pass the raw list to SendMail.
All right, here we go today.