Python bulk Mail Instance code _python

Source: Internet
Author: User

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.

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.