directly on the code.
Copy Code code 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 name to write, what name to show in the 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)
Careful readers will find that there is a phrase in the code: msg[' to ']= ', '. Join (Strto), but msg[[' to ' is not used in the back, it is obviously unreasonable to write, but this is the Stmplib bug. This is the only way you can send mass mail. Identify the following reasons:
The problem is that smtp.sendmail and email. Mimetext need two different things.
Email. Mimetext sets up the ' to: ' Header for the ' body ' of the e-mail. It is only used for displaying a of the human being at the "other", and like all e-mail headers, must be a single String. (Note this is the it does not actually have to have anything to doing with the people receive the message.)
Smtp.sendmail, on the other hand, sets up the "envelope" to the "SMTP protocol." It needs a Python list of strings, each of which has a.
So, what your need to do are COMBINE the two replies you received. Set msg ' to ' to a single string, but pass the raw list to SendMail.
Well, that's it for today.