Python group email

Source: Internet
Author: User
Tags python list

I tried to send an email via Python today. I suddenly thought I could send an email in a group? Group mail is a bug in smtplib, but it is finally solved.

Code directly

Import smtplibmsg = MIMEMultipart () # construct the attachment 1att1 = 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" '# Here, the filename can be written at will, what name is written, and what name is displayed in the email msg. attach (att1) # construct Attachment 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) # Add the mail header strTo = ['xxx1 @ 139.com ', 'xxx2 @ 163.com', 'xxx3 @ 126.com '] msg ['to'] = ','. join (strTo) msg ['from'] = 'yyy @ 163.com 'msg ['subobject'] = 'email topic' # Send email 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 'sent successfully 'failed t Exception, e: print str (e)


Careful readers will find the following in the Code: msg ['to'] = ','. join (strTo), but msg [['to'] is not used later. It is obviously unreasonable to write this statement, but this is the bug of stmplib. Only in this way can you send emails to the group. Find out the cause as follows: 

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 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 have to have anything to do 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 has a single address.

So, what you need to do is COMBINE the two replies you Have ed. Set msg 'To' to a single string, but pass the raw list to sendmail.

Now, we will be here today.

References:

Http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html

Http://sar4.com/python/2012/12/31/smtplib-list-object-has-no-attribute-lstrip/

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.