Python 2.7 uses Smtplib to send, CC, and send HTML tables

Source: Internet
Author: User

Development language: python2.7 Package: Smtplib


To import a package:

Import Smtplib


Define a function:

Def send_mail (to_list, cc_list, html, sub):     me = mail_ User    msg = mimetext (html, _subtype= ' HTML ',  _charset= ' utf-8 ')    #  formatted message content as HTML, encoded as utf-8    msg[' Subject '] = sub     #  message subject     msg[' from '] = me    #  sender      msg[' to '] =  ";". Join (to_list)   #  recipient, convert list to string     msg[' Cc '] = ; ". Join (cc_list)   #  cc people, convert list to string     try:         send_smtp = smtplib. SMTP ()     #  instantiation         send_smtp.connect (mail _host)     #  connect the SMTP server         send_smtp.login ( Mail_user, mail_pass)     #  Usage definitionsAccount password to login         send_smtp.sendmail (me, to_list+cc_list,  Msg.as_string ())     #  Send mail         send_ Smtp.close ()     #  Close connection         return  true    except exception, e:        #  logging.debug (e)         print e         return false

Where: "To_list" is the recipient list (can be one or more), "Cc_list" is the CC list (also one or more), "HTML" is the message content, you can send HTML-formatted messages, "sub" is the subject of the message.


Call the Send mail function:

if __name__ ==  ' __main__ ':     mail_ host =  ' [email protected] '     mail_user =  ' [email protected] '     mail_pass =  ' password '     mailto_list = [' [ Email protected] ',  ' [email protected] ',  ' [email protected] ']     Mailcc_list = [' [email protected] ']    html =  ' 

During the search for data, it was found that many people said that the CC function could not be implemented, perhaps because of the conversion between the list and the string, or because of the send_smtp.sendmail () function format problem, although no error, but not sent successfully.

It is recommended that if you do not send a successful or CC success, you can

to_list,cc_list,msg[' to '],msg[' CC '

Add print here to see the type and whether it is correct;


Attached Smtplib Source Address:

https://hg.python.org/cpython/file/2.7/Lib/smtplib.py


This article is from the "Thousand Face" blog, please make sure to keep this source http://oslibo.blog.51cto.com/10854638/1908390

Python 2.7 uses Smtplib to send, CC, and send HTML tables

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.