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