Python Bulk Message Instance code

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.